Showing posts with label NetBeans. Show all posts
Showing posts with label NetBeans. Show all posts

Saturday, 28 November 2009

NetBeans subversion update issue

For me , NetBeans is the best IDE I tried so far…
And believe me, I tried and used plenty…
I’m not talking about Java IDE only, but IDE in general…

But there’s a bug in it that made me wonder if there is a need for me to look for another IDE to use when developing applications.

At one moment , I considered moving to Eclipse, maybe a community edition of IntelliJ IDEA, or even a JDeveloper.
After a while I decided not to switch to other IDE in the middle of the project, and I doubt I’ll be moving after because I found a solution to a problem I’ll be talking about now…
After all, this is the only serious issue i have with NetBeans so far…

So, what is it about?

I noticed that every now and then , after I update my local project from subversion repository , I have a problem deploying it…
Just for the record: project in the subversion I updated to is a fully legal and working project.

NetBeans manage to update it, but when I try to deploy a project, all I get is an exception saying:


Caused by: Action class […some Action class…] not found - action - file:/C:/Documents%20and%20Settings/Darko/My%20Documents/NetBeansProjects/MyProject/build/web/WEB-INF/classes/struts.xml:714:99
        at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.verifyAction(XmlConfigurationProvider.java:405)
        at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addAction(XmlConfigurationProvider.java:355)
        at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addPackage(XmlConfigurationProvider.java:460)
        at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.loadPackages(XmlConfigurationProvider.java:265)
        at org.apache.struts2.config.StrutsXmlConfigurationProvider.loadPackages(StrutsXmlConfigurationProvider.java:111)
        at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:189)
        at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:55)
        ... 28 more



But there is no message saying what’s wrong…
As I said, this was a quite frustrating, and it still is, but now I know how to solve it and continue my work…

All you need to do is restart Netbeans and try to deploy your project again.
You’ll fail.
But now , you’ll get a message in your output window saying that some of your classes, is missing.
Find which one is “missing”.
It is not really missing. For some reason NetBeans is just saying it cannot “see” it…
Go to source packages and open this class.
Go to the end of it and just enter few new lines in it and click “save”.
Let NetBeans save it, and then do a right-click on the project and click “Build” and hit the “Clean and Build” button…


 





Now try to deploy it…
You’ll probably succed…
I know I do!

If you don’t, do all the process once again…
Not ALL the process, just the one after restarting NetBeans.

I would really like to see this bug fixed.
I don’t know if this is really a NetBeans bug, or a Subversion bug, or Tomcat bug ( I use Tomcat ) but it exists…

As I said, this is the only serious bug I found so far in NetBeans , and would really like to hear from you if there is a way of solving somehow different… Some regular way …


Tuesday, 22 September 2009

Using simple theme and custom CSS in Struts2

Simple theme in Struts2 is great when you want to use your own CSS , and control everything by yourself when it comes to GUI.
Probably many of you (us :-) ) tried using Struts2 with your own CSS without setting default theme to SIMPLE and came up with bunch of unfamiliar tags , like

<table class="wwFormTable">

In your HTML…

Well , if you want to use your own CSS and get your application to look like you wanted to, you need to either create your own new theme ( harder ), or just use Simple theme and use your own CSS ( easier ).

In order to switch to simple theme, just copy/paste this line to your struts.properties file:

struts.ui.theme=simple


Visit
http://devtalks.blogspot.com/2009/07/create-struts2-project-in-netbeans.html
if you are using NetBeans , or

http://devtalks.blogspot.com/2009/07/eclipse-galileo-and-struts2.html
if you are using eclipse

on how to create Struts2 project , and struts.properties file

And that’s it! This way your pages will not contain those annoying
<table class="wwFormTable"> tags…


ISSUES

One thing I noticed when using simple theme is that struts tag <s:fielderror> doesn’t seems to “work” as it should…
You cannot just say, for example,

<s:fielderror />
<s:textfield name=”firstName” />

and expect from struts2 to show errors if your validator doesn’t succeed in validating filed “firstName” .
As a matter of fact, struts2 will indeed dispatch to your INPUT result, but it will not show any errors!
Fortunately , There is a solution to this problem: just use

<s:fielderror> <s:param>filed_name</s:param> </s:fielderror>
instead of
<s:fielderror />

in your .jsp pages and your validation will again act as it should…


ALTERING FieldErrors , ActionMessages and ActionErrors

Speaking of fielderrors, I often see people asking how to change the way Struts is showing field errors action messages and action errors…

You do this by extending the theme you are using. When using simple theme, all you need to do is:

- Create subfolders “template” and “simple” inside folder “classes”



- Find actionerror.ftl , actionmessage.ftl and fielerror.ftl ( or just those which you would like to extend ) in struts2-core-2.x.x.jar , inside “template.simple” package. You can find this .JAR inside your “libraries” folder.




- Copy these files to your new template -> simple folder created in earlier step.



If you open one of these files, you’ll see how these messages are generated.
You can change all that just by editing that file.

Lets say you don’t want your application to show fielderrors as <ul> <li> </li> <ul> elements.
You just need to open the fielderror.ftl and change the part that does this:

So, instead of having

like it is in original file, we can just remove <ul> <li> </li> <ul> elements, and change it with, for example, simple <span> </span> element. That will do the trick. ;-)



Or let’s, for example,change the way your Action message is shown in an application.
Let’s say we need a green box with all the action messages shown in it…
Something like this:


To do this, you could just open the actionmessage.ftl file, and change the original

<#if (actionMessages?? && actionMessages?size > 0)>
<ul<#rt/>
<#if parameters.cssClass??>
class="${parameters.cssClass?html}"<#rt/>
<#else>
class="actionMessage"<#rt/>
</#if>
<#if parameters.cssStyle??>
style="${parameters.cssStyle?html}"<#rt/>
</#if>
>
<#list actionMessages as message>
<li><span>${message!}</span></li>
</#list>
</ul>
</#if>


with something like this:


<#if (actionMessages?? && actionMessages?size > 0)>
<div class="mymessage"><br><span<#rt/>
<#if parameters.cssClass??>
class="${parameters.cssClass?html}"<#rt/>
<#else>
class="actionMessage"<#rt/>
</#if>
<#if parameters.cssStyle??>
style="${parameters.cssStyle?html}"<#rt/>
</#if>
>
<#list actionMessages as message>
<span>- ${message!}</span>
</#list>
</span><br><br></div>
</#if>

And for now on, every Action message in your application will look like a green box containing messages.

Here is the css I used:

div.mymessage
{
right: 0;
bottom: 0;
left: 0;
width:300px;
margin: auto;
color: #33CC33;
border-color:#009900;
border-style:solid ;
border-width:1px;
border-collapse: collapse;
font-weight: bold;
text-align: center;
background-color:#DDFFDD;
}


This is just a simple example what you can do with extending a theme...


Thursday, 9 July 2009

How to create Struts2 project in NetBeans

If you decide to start learning and using struts2, probably one of the first things you think about is which IDE to use, and what you need in order to create and run a Struts2 project...

If you search the web , you'll find many tutorials on how to create a struts2 project, and start developing.
This is what I did when I was starting, and soon find out that nothing I found on the web worked for me in proper manner...

So, I decided to create a small tutorial on how to create an empty struts2 project , in order to help others, and save their time.
Not to teach you Struts2, but to help you create a working Struts2 project!

Ok, I will use NetBeans 6.5, and a Tomcat 6 that comes with it...

First of all, go to http://struts.apache.org/2.x/index.html and download the latest version of Struts2.
I downloaded struts-2.1.6-all.zip
You won't be needing all the libraries in it. Just 6 of them. Because of this, you can download essential dependencies only.

When you download one of these files, extract it to some location, for example: c:\Struts2
It is now time to start a new project. Start NetBeans.

Click File -> New project -> Java web -> Web application



Let's name our project "Struts2Example". Click "next".
Choose which server you wish to use ( I selected Tomcat 6 ), set JavaEE to version 5, and leave ContextPath to be /Struts2Example

You can click "finish" now.
If you click "next" DO NOT, I repeat DO NOT choose available ( older version ) of struts under "frameworks".

Your project tree will look this:




Now, in order to use Struts2 libraries, let's add those essential dependencies to project:

Right click on "Libraries" folder and then click "Add JAR/folder". A dialog will open.
Go to folder where you extracted Struts2 libraries and select ( add to project ) following files:

- struts2-core-2.1.6.jar
- freemarker-2.3.13.jar
- ognl-2.6.11.jar
- commons-fileupload-1.2.1.jar
- commons-logging-1.0.4.jar
- xwork-2.1.2.jar


Most of the tutorials on the web are written for older version of struts2, at the time when commons-fileupload-1.2.1.jar was not mandatory, so even when following all the steps in tutorial , you just couldn't get your project to work ( with newer version of Struts2 ) !

Now, your project will loke like this:



Now, in the root of WEB-INF folder create a new folder , and name it "lib".
Copy these six JARs into this new folder.




These files will be uploaded to App Server , and a deployed application will use it.
We could put this files into main lib folder of application server, but it is most probably better this way...

We need to create one more folder in the root of the WEB-INF , and name it "classes".
This is where you will create struts.xml, and if needed - a struts.properties file.




I'll put all my JSP's into new folder, "jsp" , which I will create in the root of "Web Pages" folder.

All Java code that we write, we will place into some package in the "Source Packages" folder.
I'll create one now, called "Struts2Example" and a "Hello" Class in it.
If you use validaton, put all Validation XMLs inside class's package too...




This is how, at minimum, your Struts2 project structure should look like.


I'll create one small Struts2 application now. Nothing complicated.
User will enter his name, and press "submit" and other page will open , saying Hello to this user.

So, let's first create a Java class extending ActionSupport class , named hello, inside Struts2Example package.
Let's make it to look something like this:

package Struts2Example;

import com.opensymphony.xwork2.ActionSupport;

/**
*
* @author Darko
*/
public class Hello extends ActionSupport
{
private String name, message;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getMessage() {
return "Hello " + getName();
}

public void setMessage(String message) {
this.message = message;
}

}

We now need two JSPs, for example "nameinput.jsp" and a "response.jsp"
Each JSP needs to contain this line, in order to use struts2 custom tags in it:

<%@ taglib prefix="s" uri="/struts-tags" %>

nameinput.jsp:



response.jsp:



Let's create an XML validation file for Hello class, and make a name property mandatory.
Hello-validation.xml:

<!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">

<validators>
<field name="name">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>Please enter name</message>
</field-validator>
</field>
</validators>

There's only 2 things left for us to do:

Edit struts.xml, and web.xml:


struts.xml:


web.xml:


and if you deploy this project to server, and open
http://localhost:8084/Struts2Example/entername.action , you'll get something like this:



If we do not enter name:



and if we do:



So, I hope I helped you to understand how to create an empty struts2 project in NetBeans.
At the end, our project looked like this:





There's more to it. For example, if you wish to override some theme's behaviour ( for example "simple" ) you create folders
template/theme_name ( for example template/simple ) and it you copy all the original files from this theme ( from struts2-core-X.X.X.jar ) and edit them in this folder...



But, some other time about that and some other usefull things...

Enjoy Struts2!

Wednesday, 3 December 2008

How to create and use web services in NetBeans

I think web services are one of the coolest  things in programming. I will now show you how to create and use a web service in NetBeans.

Let’s create a new web application  project.



Call it “WebServices”. Press Finish.
After a new project is created and opened, right click on the project, and choose “New -> Web Service”



Let’s say we want to create a web service that provides us a full name of the country whose national domain we send to the web service.
For example, if we send “it”, the web service return us “Italy, Europe”.
So, let’s give this web service a “CountryDomain” name, inside “countries.tools” package.




When we press “finish”, a new Tab will open offering us to create a web service by typing a code , or to create it with a provided visual designer.

 

We will use a designer.

Click on the “Add operation” button.
A new window will open, asking us how we want to call this new operation, and to define a return type and parameters.
In our example,  the return type is java.lang.String
and the parameter is a String: domain



Press ok.
Now if we look at the visual designer, we will se that there is an operation that we created, and if we look at the code, we’ll see that the NetBeans generated a definition of our new operation.
Now, we have to write it’s implementation.

I won’t complicate things up now and use some database, from which I would collect information about country whose domain I receive, so I’ll just use some HashMap with some countries listed in it …

So, let’s write some code in our web service. In the constructor I will initialize our HashMap with some countries, and in our getCountry operation, I will provide a code that will return a country name.

 

And now, only thing left for us to do now, is to deploy this web service. I will deploy it on glassfish server that comes with netbeans.
To deploy it, right click on the project node, and select  “undeploy and deploy”.





After it’s deployed, it’s time to test it.
Right click on the web service that you made , and  select  “test web service”.

 

A browser will open, and it will look something like this:




If we enter “it” ( as Italy ) , for example, and hit “getCountry” button, we’ll get a result “Italy, Europe”, or if we enter some not-existing domain ( not existing in our HashMap ) we’ll get a "Not a valid national domain" result.


Nice, isn’t it? ?



Now,  how can someone use it in it’s code?

The following example shows us how to do it.

Let’s , once again, create a new Web Application project. let’s call it  “CountryChecker”. Press Finish.

A new project will open.
Now, in order to use some web service, we need to know it’s URL. I’ll use CountryDomain ‘s URL  http://localhost:8080/WebServices/CountryDomainService?wsdl

In order to use some web service in your code, we need to create a new web service client




It will take some time for NetBeans to create it. When it does, you’ll get something like this:

 

And now we come to the best part:

If you want to use this web service in some of your web pages, just drag and drop it’s method onto your page, and NetBeans will generate the entire call.


 

and when we open this web page, we will get:

 








Wednesday, 12 November 2008

God bless debugger

Have you ever wrote a code , and when you looked at it you just felt proud you wrote it.

And then you decide to try it, and at some moment you find out that it has some kind of a bug?!
Fortunately, you are able to fix a code in a short time, like you always do…
Then you take a good look at the code and at some point you say to yourself “No, there’s no way there’s something wrong with it!”
Then you keep on trying , and you test it more and more, you write it again and the damn bug just keep on playing with your brain… “What wrong? Where did I made a mistake?”

So, have you? Yes? No?

Well, I have!

Not very often, but once or twice in a while...

Oh, and yes , I forgot to mention: the worst thing is not the fact that you have a bug in your code!
Noooo: the worst thing is that your application is a network-based and you have several instances of it communicating in VPN so you don’t know is it something a bad network does to it, some kind of a collision, unplugged cable, a firewall, bad code, some bad admin’s setup, is there some older version communicating with the new ones or just some of 1000000 other possible reasons… yes, that’s the worst thing about it!

Fortunately, there’s something called debugger!
If you develop software for living , you will soon find out that a good debugger is your best friend!

There isn’t a problem you can’t solve in a few minutes!

Unfortunately it seems that many developers have forgotten this.

It's really fascinating that debugger isn't used as often as it should...
I think this is one of those must-know-how-to-use things, and every developer should now how to use it, but at the moment I don’t know many people who does.

Even if they know how to use it, they never do…

I use NetBeans IDE . It has a great debugger!

I always use it when there's a need.
I find my self able to spot and fix a bug in a very short time, no matter how big, small, stupid or hidden it is, but few days ago… Man, I wanted to buy my debugger a beer!
That is when I decided to write about it on my blog.

There’s no way I could solve those problems that fast without it!
I would be stuck for hours trying to find a problem…

Now, it all works just fine… ;-)

How often do you use debugger?

Tuesday, 14 October 2008

A joy of working with subversion (in NetBeans)

I didn’t know that subversion is such cool thing until I tried it…

Today, I can’t imagine a project developed without it !

I started using SVN just recently, when it became too hard for a team , that I was a member of , to synchronize itself while working on a project. Everyone had it’s version of source code  and merging all the changes was a nightmare!
That is when Me and  my team college  thought “Why wouldn’t we give subversion a try?“ After all , NetBeans ( that we use ) has integrated subversion support…

So, all we had to do was to install subversion , create a repository and start using it.
Well, not just that: we needed subversion joined with some web server because we wanted to install it on some server so everyone in the team could access it over network.

If you are in the same position and use MS Windows, I advise you to try VisualSVN server (  http://www.visualsvn.com/server/ ) .

“VisualSVN Server is a package that contains everything you need to install, configure and manage Subversion server for your team on Windows platform. It includes Subversion, Apache and a management console.”



It’s much easier than to set the apache yourself!
All you need to do is to install it ( just few easy steps ) and you’ll have preconfigured apache with subversion installed!
And not just that: you’ll even have a GUI manager to manage repositories , users and groups for it!
Cool thing!

As for now, I am very satisfied with it…

It is no longer a problem for several programmers to work on the same project at the same time,  and everyone always have  the latest version of project source code.
Even the fact that the team members are on different locations is no longer a problem…

One advice: before you start using subversion with your copy of  NetBeans, I advise you to use projects groups within NetBeans when creating/sorting  your projects.
It can significantly make things easier for you!
For example: If you are working on some large project “PR1” , I advise you to create a new folder inside “C:\Documents and Settings\...\My Documents\NetBeansProjects”  called, for example, “PR1” and to place all the projects that belong to “PR1”  inside that folder. That way, when you import some of these child projects  to SVN repository, all the other projects that are  in root of “PR1” folder will inherit the same repository path ( URL ) …
This is the way it works.
If you place all of your projects in root of “C:\Documents and Settings\...\My Documents\NetBeansProjects”, then all of them will inherit the same repository path ( URL ) and when you try to import some project into new repository - you’ll find yourself with a problem, and thing are going to become a little more complicated…

So, let’s make a few steps on how to install and use subversion in NetBeans.

First of all, go to the http://www.collab.net/downloads/netbeans/ , and download and install CollabNet subversion client. It is required that you have it installed on your computer.

Install VisualSVN .

When you install VisualSVN, start VisualSVN server manager, and create a repository, for example “PR1”.



Create new user for repository (Right click on:”users” and then click “create new user”).

You can also set who can gain access to some repository. Everyone can access every repository by default. To change that, right click on the repository -> properties-> security.

Let’s create a folder in the  “C:\Documents and Settings\...\My Documents\NetBeansProjects” with a same name as our repository.
Now, let’s start NetBeans and create a project group out of “C:\Documents and Settings\...\My Documents\NetBeansProjects\PR1” folder.






and create a project called JavaApplication1 in it…

 

Now, we have out project created, and we can now import it to the SVN repository.
Right click on the project -> Versioning -> Import into subversion repository

 

A window like this one will open:

 


Let’s enter our repository URL. You can see your repository URL in the VisualSVN server manager , when you click on it:

 


Let’s enter it .




Enter your user name and password and press next.
Give your repository folder a name. I’ll leave it the same as project folder, and enter a custom message:




When you press next you’ll get the last window, which lists all the files that you are about to import:

 

Click finish.

Voila! Now you have your project versioned!

Notice that now, when you do a right click on the project , you’ll have item “subversion” that contains actions as “diff” , “update” , “commit” and others…

Your team can now do a  “checkout…” in order to create a copy of a project on their local PC, do a “commit” when they change some part of code, do an “update” when they want to update a local project with all the changes that were made by other members of a team and so on…

Enjoy working on a project using subversion! 


Monday, 18 August 2008

Visual Web JavaServer Faces issue in NetBeans 6.1 and 6.5

In my personal opinion, NetBeans is the best IDE there is… Not only for Java, but generally the best IDE.
It’s really stable, professional and the best of all: It’s absolutely free!
I use NB version 6.0 and recently replaced my 6.0 with 6.1 and hope to see 6.5 final release very soon…
There is one thing I noticed while working with 6.1: Visual web JavaServer faces has some kind of an issue…
Let’s start from the beginning.
I have two NB IDEs started , 6.0 on my PC, and 6.1 on my laptop.
Let’s create some web application, and let’s use Visual Web JavaServer Faces framework while building it.
The easiest thing to do is to follow steps in this tutorial:
It’s quite easy… Just follow the steps …
So, I created two new projects on my PC and laptop , called HelloWeb just as it says in the tutorial.
I selected Visual Web JavaServer Faces framework, I designed the page , I added all the components to it .
NetBeans 6.0 behavior:
If we look at the Navigator (Window -> Navigation -> Navigator , or Ctrl + F7) in Java view we can see that it shows these new components as private members.



I double-clicked the helloButton and added code provided in this tutorial:



And it went really good too…
Finally, I started my new application:





Now, let’s see how 6.1 is doing:
NetBeans 6.1 behavior:
If we look at the Navigator (Window -> Navigation -> Navigator , or Ctrl + F7) in Java view, we ( I ) can not see any of the added components!??



And if you try to add a code provided in this tutorial, you’ll get this:





NetBeans acts like there aren’t any nameField or helloText, and offers to create them for you…!!!???
If you select that, it will create class/Object with that name , but , off course, it won’t solve any of your problems…
So, NetBeans 6.0 creates all as it should, but 6.1 and 6.5 does not!?
I don’t know if I am missing something here, or this is a bug ( a HUGE BUG ) , but I guess something is not as it should be…
I reported this to SUN and asked them why does this happens in NB 6.1 and 6.5, but didn’t get any answer from them…
Does anybody know why did this happen? If yes, please share it with us… I guess and hope that I forgot to do some small and trivial thing here…