javablogspot

Just another WordPress.com weblog

Struts 2 form with validation and XHTML CSS layout (part 2)

Posted by damuchinni on February 22, 2009

You will be glad to know that having completed part 1, that is the hardest part over with!

The next thing I wanted to ensure was that the code produced by Struts was completely XHTML compliant. This it turns out is very easy. In your struts.xml you need to add the following:

This tells Struts 2 to use the built in XHTML theme. This theme is stored in the struts.jar file under /template/css_xhtml. Have a look inside the jar file and you will see there are several themes available for use. I don’t know anything about Freemarker but that is the technology used internally by Struts 2 and hence the templates in the struts.jar file are done using Freemarker.

he CSS produced by Struts 2 (version 2.0.11.1) was not absolutely perfect as it dropped an extra
into the output. This was easily solved with a css rule of:

form br { /* Set to display non as Struts css_xhtml renders one in the middle of the form! */
display: none;
}

The next thing to do was to build the login page. The hardest part about this was to get the CSS rules done, that is because Struts2 automatically generates the class names used for styling and you can only find out what they are by looking at the generated source code. That isn’t really what I want to cover here though, so if you want to see how the page is laid out, then take a look at the downloadable .war and check out the CSS file in there. In case you are curious the page produced by the .war file after a user has entered some incorrect details looks like this:

Struts2 form layout using XHTML and CSS

The interesting part on the /usr/login.jsp page is as follows:





This is pretty simple, the form submits to the action called login. We saw the action called login!input but this time we actually want our action class to be called so we can check the username and password were acceptable for a login. We’ll come back to this in a second as there is also a tag. This tag is seen if the action called is input and we are redirected back to this page by that action because the information the user entered was not a valid login. Within our action we will set a message that this tag will read and display to our user. OK, back to our forms submit action. When the form submits it looks up the value of the action (which is login) in the struts2login.xml file and is:

/usr/login.jsp
/usr/success.jsp
/usr/login.jsp

It is wrapped in the same /usr namespace as the login!input action we saw previously. When the user submits the form they are send to the class given in the action (struts2you.examplelogin.Login in this case) EXCEPT when there is validation to be done.

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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