Struts (work with basic coding)

Struts Tags      

As mentioned earlier, to apply struts tag in the page, you got to declare the following line 

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


Resource links

<link href="<s:url value="/css/tutorial.css"/>"

  rel="stylesheet" type="text/css"/>


Direct links

<li><a href="<s:url action="Register"/>">Register</a></li>


Links with parameters

<s:url id="url" action="Welcome">

  <s:param name="request_locale">en</s:param>


<s:a href="%{url}">English</s:a>



Coding and mapping Actions


Let’s start the coding of action with the following example.




import com.opensymphony.xwork2.ActionSupport;

public class Logon extends ActionSupport {


    public String execute() throws Exception {


        if (isInvalid(getUsername())) return INPUT;

        if (isInvalid(getPassword())) return INPUT;

        return SUCCESS;



    private boolean isInvalid(String value) {

        return (value == null || value.length() == 0);



    private String username;

    public String getUsername() {

        return username;


    public void setUsername(String username) {

        this.username = username;



    private String password;

    public String getPassword() {

        return password;


    public void setPassword(String password) {

        this.password = password;





All the action class is extended from com.opensymphony.xwork2.ActionSupport class. Action does not worry about generating the response, only deciding which logical result to present next.


There may be multiple results available to an action mapping. To indicate which one to select, the Action class returns a name corresponding to the appropriate result.


<action name="Logon" class="tutorial.Logon">

  <result type="redirect-action">Menu</result>

  <result name="input">/tutorial/Logon.jsp</result>



  • If we enter a username and password into the form, the Logon Action will return "success". "success" is the default result code, so the framework will use the "Menu" action as response.
  • If we do not enter both credentials, the Logon Action will return "input", and the framework will use the Logon.jsp as the response.
  • SUCCESS and INPUT are predefined keywords in struts and their values are lower case strings, which could be matched in the struts-config.xml file such as <result name="input">/tutorial/Logon.jsp</result>. The list of keywords could be found at



If the page we wrote is nothing but links, we don’t need an Action class. But, we should still add a mapping, so that we can use an action URI. If we link only to actions, and never to pages, then it’s easy to add an Action class later. For example

<action name="Login">




This mapping will only redirect to the page of logon.jsp, but provide the convenience to add action class in the future.


More details about return type




Validation can be described through an XML document, or using annotations. The XML document is named after the Action being validated with a "-validation" suffix. Since we would like to validate the Logon Action class, our document is named Logon-validation.xml.



<!DOCTYPE validators PUBLIC

"-//OpenSymphony Group//XWork Validator 1.0.2//EN"




    <field name="username">

        <field-validator type="requiredstring">

            <message>Username is required</message>



    <field name="password">

        <field-validator type="requiredstring">

            <message>Password is required</message>





Create a file named Logon-validation.xml next to the class, and paste in the code.


The first time a page displays, we wouldn’t want the validation to fire. We should have a chance to enter the input before being told it’s incorrect. One way to bypass validation is to refer to a special "input" method provided by the base ActionSupport class. To do that, we need to edit the Welcome page and the Logon mapping.


To indicate the changes, we’ll use 减 to indicate a line we are removing, and 加 to indicated a line we are adding.



<li><a href="<s:url action="Logon"/>">Sign On</a></li>
<li><a href="<s:url action="Logon_input"/>">Sign On</a></li>



<action name="Logon" class="tutorial.Logon">
<action name="Logon_*" method="{1}" class="tutorial.Logon">


How the Code Works

To open the Logon form, the Welcome page refers to Logon_input.

  • The framework matches this reference with the Logon_* action mapping.
  • The "method={1}" attribute is replaced with "method=input".
  • The framework invokes the input method on the Logon Action class.
  • Since "input" is on a special list of methods that bypass validation, the validation framework is not invoked.
  • The default input method returns the result code "input".
  • The framework renders "Logon.jsp" as the response, without any validation messages.

To submit the Logon form, the Login pages refers to Logon.

  • The framework checks for a validation for the target Action class, Logon.
  • Finding a Logon-validation.xml file, the framework creates a validation object for the class, based on the XML document.
    • Essentially, the validation is a set of Validator objects.
  • The Validators are applied to the incoming properties.
  • If a Validator fails, its message is added to an internal queue.
  • When all the Validators have fired, if the framework sees that errors have been posted, it seeks the "input" result, without invoking the Action class.
  • Otherwise, the default Action method fires. Since the input has already been validated, the "success" result code is returned.

What to Remember


The framework provides a validation framework. A set of validators can be associated with an input field. If validation fails, the framework can return to the input page and display the error messages. To bypass validation, a special "input" Action method can be invoked, instead of the default "execute" method.


More details about validation


This entry was posted in Struts. Bookmark the permalink.

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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s