Wednesday, February 22, 2012

Struts part 3

Struts part 3

Custom Tags

• html
• Logic
• bean
• nested
Example code using the struts html tag

< %@ taglib uri=“../struts-html.tld” prefix=“html”% >

< html >…

< html:form action=“login.do” >

User: < html:text property=“userId”/ >

Password: < html:password property=“password”/ >

< html:submit value=“Submit”/ >

< /html:form>

< /html >
Struts Configuration File
< struts-config >
… < form-beans >

< form-bean name=”loginForm” type=”com.sample.struts.form.LoginForm”/ >
… < /form-beans > …

< /struts-config >

DynaActionForms

< struts-config >

.< form-beans >

< form-bean name=”loginForm”

type=”org.apache.struts.action.DynaActionForm” >

< form-property name=”userId” type=”java.lang.String”/ >

< form-property name=”password” type=”java.lang.String”/ >

< /form-bean >

< /form-beans >

…< /struts-config >

Login Page

< html:form action=“login.do” >
User: < html:text property=“userId”/ >
Password: < html:password property=“password”/ >
< html:submit value=“Submit”/ >
< /html:form >

ActionForm
public LoginForm extends Action Form{
private String userId; //getter and setter methods for these.
private String password;
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request){
ActionErrors errors = new ActionErrors();
if(userId == null || “”.equals(userId)){
ActionError error = new ActionError(“error.login.userRequired”);
errors.add(error);
} //other validations
return errors;
}
}

Struts-config.xml
< struts-config >
< form-beans >
< form-bean name=”loginForm” type=”com.sample.struts.form.LoginForm”/>
< /form-beans >

< global-exceptions >
< exception key=“error.nodatabase” path=“/error.jsp” type=“com.sample.struts.DatabaseException”/ >
< /global-exceptions >
< global-forwards >



< action-mappings >
< action path=“\login” type=”com.sample.struts.action.LoginAction” scope=”request” name=”loginForm” validate=”true” input=”/login.jsp” >
< forward name=”success” path=“/homepage.jsp”/ >
< forward name=”failure” path=”/login.jsp”/ >
< exception key=”error.invalidlogin” path=”/login.jsp” scope=”request” type=”come.sample.struts.exception.InvalidLoginException”/ >
< /action >
< /action-mappings >

< message-resources name=“application”/ >
< /struts-config >

Action Class

Class LoginAction extends Action{
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){
LoginForm loginForm = ( LoginForm) form;
String userId = loginForm.getUserId
String password = loginForm.getPassword;
if(authenticate(userId, password){
return mapping.findForward(“success”);
}
return mapping.findForward(“failure”)
}
}
More in Struts

Struts tiles
Advanced templating framework, that separates content from the layout

Struts validator framework
Declaratively configure validation routines without having to program validation logic

Multiple module support
Separate configuration files for different modules, allows for better organization of the components in the web application.

Advantages

-Centralized file based configuration

-Form beans automatically populated from request parameters, validation possible
-Struts custom tags
-Supports tiles, validator framework and internationalization(i18n).
-Consistent use of MVC