Struts

Struts is a widely used framework for developing web applications. Struts is from jakarta open source team. Struts 2 more powerful than struts.

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/>
... </form-beans> ...
 </struts-config>

DynaActionForms

<struts-config>
<form-beans>
<form-bean
type="org.apache.struts.action.DynaActionForm">
<form-property/>
<form-property/>
 </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/>
</form-beans>
<global-exceptions>
<exception key=“error.nodatabase” path=“/error.jsp” type=“com.sample.struts.DatabaseException”/>
</global-exceptions>
<global-forwards>
<forward name=“sessiontimeout” path=“/sessiontimeout.jsp”/>
</global-forwards>
<action-mappings>
<action path=“\login" scope="request" validate="true" input="/login.jsp">
<forward path=“/homepage.jsp"/>
<forward path="/login.jsp"/>
<exception key="error.invalidlogin" path="/login.jsp" scope="request"/>
</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 of Struts

  • 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