Just another weblog

  • Subscribe

  • Recent Posts

  • Pages

  • Archives

  • Top Clicks

    • None
  • Advertisements

Struts 2 form with validation and XHTML CSS layout (Part 1)

Posted by damuchinni on February 22, 2009

Problem summary
I wanted to create a simple login form using Struts 2 for the framework. I wanted the form to do the following:

* To be laid out using CSS;
* To validate the data the user entered;
* To make a decision about the data entered and either return the user to the login form with an error message or redirect them to a login success page.

Setting up the home page

Most web sites don’t have their full login page on their home page so I first wanted a home page that would link to the login page:

Struts 2 you – login example – index page

Struts 2 you – example login


This is a very simple jsp page but it does have the tag with an action attribute that may need some explaining. This tag will create a URL (used by the tag directly below it) that will redirect to a Struts action that is accessible through the URL /usr/login. The action value of login!input means it will hit the action login but the method input rather than the default execute method. In the struts2login.xml file is the following action node wrapped in the usr package:


When the user clicks on the link on the home page they are processed through the above action mapping. If you download the .war file associated with this tutorial and look at the struts2you.examplelogin.Login class you will see it has no method input(). This is because we have defined the method ‘input’ in the struts.xml file; there is a node. This means that all though the user is sent through the Struts 2 framework they do not actually hit our struts2you.examplelogin.Login class. This may seem odd, and it took me a while to work this out, but if we don’t do it this way the user will be processed through our action and be redirected to the login page with the action claiming that the user failed to input valid data. This means that if we do not hit the input method then the user will click the Go to the login page link and will see the login form, complete with the:

* You entered an invalid username;
* You entered an invalid password

validation errors. This is a little harsh as they haven’t been given a chance to get it right so far! By processing them through the login!input URL and defining the input result in the struts2login.xml file we can take them through the Struts framework and not tell them they have entered invalid login information as they have not hit our struts2you.examplelogin.Login class. I think this is quite complicated, but it is also necessary as all URL’s should be processed through the Struts framework, as just using a hyperlink to /usr/login.jsp would not trigger any other Struts processing on the page (if there was any). I hope that all makes sense! If not download the .war file for this tutorial and play around for a while until it does. It took me a good days work to work that bit out! If anyone knows a better way of doing it then please let me know.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s