Google

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.

<struts-config>
<form-beans>

<form-bean
name="registerForm"
type="app.RegisterForm"/>

</form-beans>

<action-mappings>

<action
path="/register"
type="app.RegisterAction"
name="RegisterForm"
scope="request"
validate="true"
input="/register.jsp">
<forward name="success" path="/success.html"/>
<forward name="failure" path="/failure.html"/>
</action>

</action-mappings>
</struts-config>


ActionServlet
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.*;
import java.io.*;

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(password1.equals(password2)){
if(User exists in database){
Throw exception;
mapping.findForward("failure");
}
mapping.findForward("success");
}
mapping.findForward("failure");
}
}

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.

<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>
/WEB-INF/conf/struts-config.xml

<
/param-value>
</init-param>
</servlet>

<servlet-mapping>
<servlet-nameaction</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

<taglib>
<taglib-uri>/tags/struts-beans</taglib-uri>
<taglib-location>/WEB-INF/lib/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-html</taglib-uri>
<taglib-location>/WEB-INF/lib/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-logic</taglib-uri>
<taglib-location>/WEB-INF/lib/struts-.tld</taglib-location>
</taglib>

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

Labels: , ,

1 Comments:

Blogger Raghavendra said...

Very Helpful Info. Thanks

November 26, 2007 at 9:50 PM  

Post a Comment

Subscribe to Post Comments [Atom]

<< Home