javablogspot

Just another WordPress.com weblog

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

Posted by damuchinni on February 22, 2009

In our case we are performing a validation. It is actually very easy to do and if you have gotten this far then you will be happy as to how simple it really is. All we need to do is include an xml file in with the class mentioned in our node. Looking at out action node we can see the class that will process this action is struts2you.examplelogin.Login. That is a class that sits in the package struts2you.examplelogin, so that is where we also stick our Login-validation.xml; notice the name of our xml fie is the same as the class that is specified in the action node with an additional -validation.xml This file looks like this:

Username is required

Password is required

That’s it, finished! Struts 2 may be a difficult learning curve but it does have it’s advantages. Now when the user hits submit they will be directed through the action to the Struts 2 validator framework. If the users input matches the requirements in the validation.xml file then the processing will be passed onto the action class. If the users input does not match then they will be redirected back to where ever they came from and the nodes value will be displayed to them. It will be ready marked up in css_xhtml as that is the template we defined in struts.xml – pretty neat.

Our action servlet really doesn’t do anything. It just check for the username and password to be username and password and if they are, it redirects them to the /usr/success.jsp page (The SUCCESS and ERROR values are defined in the struts2login.xml). If the username and password is not correct then it calls the addActionError(String errorMessage) method and redirects to the login page:

package struts2you.examplelogin;

public class Login extends BaseActionSupport {

private String sUsername = “”;
private String sPassword = “”;

@Override()
public String execute(){

if(sUsername.equals(“username”) && sPassword.equals(“password”)){
return SUCCESS;
} else {
addActionError(“Either your username and/or password was incorrect.”);
return ERROR;
}
}

public String getUsername() {
return sUsername;
}

public void setUsername(String sUsername) {
this.sUsername = sUsername;
}

public String getPassword() {
return sPassword;
}

public void setPassword(String sPassword) {
this.sPassword = sPassword;
}
}

the @Override annotation simply tells the jvm that we are overriding the execute method of BaseActionExtension which in turn inherits that method from ActionSupport. As you can see, BaseActionExtension is empty. It just acts as a placeholder for code common to all our servlets. BaseActionSupport is not needed but would make things easier if we were building a full blown web application.:

package struts2you.examplelogin;

import com.opensymphony.xwork2.ActionSupport;
public class BaseActionSupport extends ActionSupport {
}

Finished! A Struts 2, validating XHTML CSS, user login page.

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

 
%d bloggers like this: