Struts 2 form with validation and XHTML CSS layout (part 2)
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:
<constant name="struts.ui.theme" value="css_xhtml" />
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.
The CSS produced by Struts 2 (version 2.0.11.1) was not absolutely perfect as it dropped an extra <br/> into the <form> 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:
The interesting part on the /usr/login.jsp page is as follows:
<s:form action="login">
<div class="divFieldError">
<s:actionerror cssClass="fieldError"/>
</div>
<s:textfield key="username" label="Username" required="true" requiredposition="left" value="username"/>
<s:password key="password" label="Password" required="true" requiredposition="left"/>
<s:hidden name="submit" value="true"/>
<s:submit/>
</s:form>
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 <s:actionerror> 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:
<action name="login" class="struts2you.examplelogin.Login">
<result name="input">/usr/login.jsp</result>
<result name="success">/usr/success.jsp</result>
<result name="error">/usr/login.jsp</result>
</action>
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.
20.02.2009 01:11 - Posted by doahh - Comments: 0 - Java

Comments: