Thursday, April 12, 2007

Sample application of struts

Any Struts application comprises of following different components. I have taken Simple Register User application to explain all those components.
  1. struts-config.xml (Configuration file)
  2. ActionServlet
  3. Action Class
  4. Action Form class
  5. Action Forward
  6. web.xml

struts-config.xml is a configuration file which maps requests to Action and Action Form classes depending on URI received in request. And forwards response to a view component depending Action Forward received from Action class.





<forward name="success" path="/success.html"/>
<forward name="failure" path="/failure.html"/>


is a controller component which reads struts-config object and controls whole work flow. Most of the time we use default ActionSErvlet implementation but one can also extend ActionServlet class to customize work flow.

Action class uses helper classes to request parameter to perform any action and returns response to ActionServlet in form of Action Forward.

package app;

import org.apache.struts.action.*;
import javax.servlet.http.*;

public class RegisterAction extends Action{

public ActionForward execute(ActionMapping mapping,ActionForm form,
HttpServletRequest req, HttpServletResponse res){

RegisterForm refisterForm = (RegisterForm)form;

String userName = rf.getUserName();
String password1 = rf.getPassword1();
String password2 = rf.getPAssword2();

if(User exists in database){
Throw exception;

Action Form class defines all properties defined in form and getter and setter methods for those properties.

package app;

import org.apache.struts.action.*;

public class RegisterForm extends ActionForm{

private String userName;
private String password1;
private String password2;

public String getUserName(){ return userName;}
public String getPassword1(){ return password1;}
public String getPassword2(){ return password2;}

public void setUserName(String userName){ this.userName = userName;}
public void setUserName(String password1){ this.password1 = password1;}
public void setUserName(String password2){ this.password2 = password2;}

public ActionWrrors validate(ActionMapping mapping, HttpServletRequest req){
ActionErrors errors = new ActionWrrors();

if((userName= =null) || (userName.length()<1)){> errors.add("userName",new ActionError("error.userName.required")); if((password1= =null) || (password1.length()<1)){>
errors.add("password1",new ActionError("error.password1.required"));
if((password2= =null) || (password2.length()<1)){>
errors.add("password2",new ActionError("error.password2.required"));

return errors;

ActionForward is the object which Action class uses to return ActionResult to ActionServlet and then depending on that result ActionServlet forwards response to any of the view component.

web.xml is a web application deployment descriptor file. User Needs to map request to ActionServlet instance and pass struts-config.xml file as a parameter to ActionServlet instance. Struts also has a tag library which needs to be configured in web.xml file.





Apart from this user needs to define success.html and failure.html pages and file which is HTML form to enter userName, password1 and password2.

Labels: , ,


Blogger Raghavendra said...

Very Helpful Info. Thanks

November 26, 2007 at 9:50 PM  

Post a Comment

Subscribe to Post Comments [Atom]

<< Home