Wednesday, February 22, 2012

Struts part 2

Struts part 2
Struts view components
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 >
ActionForm • Used to pass client data between the user and business layer.

• Framework collects the input(parameters) from request and populates the action forms using form beans.

Model – View – Controller

Model-view-controller (MVC) is a design pattern used to separate data (model) and user interface (view), so that changes to the user interface do not impact the data handling and vice versa.
Model
• The domain-specific representation of the information on which the application operates. i.e the data and the business logic.

View
• Renders the model into a form suitable for interaction, typically a user interface element. HTML pages in case of web applications

Controller
• Processes and responds to user actions (request in web apps), and may invoke changes on the model.
• The model has no direct knowledge of the view.

Configuring Struts
•struts.jar in /WEB-INF/lib/
< web-app >
< 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/struts-config.xml< /param-value >< /init-param >
< /servlet >
< servlet-mapping >
< servlet-name >action< /servlet-name >
< url-pattern >*.do< /url-pattern >
< /servlet-mapping >
< taglib >
< !- - taglibs used in the applications – >
< /taglib >
< /web-app >

Struts controller components

ActionServlet
•Extends HttpServlet
•Configured in web.xml, all requests reach the ActionServlet
•Instantiates and passes the request to the RequestProcessor
RequestProcessor
•Delegates the handling of the request to a helper class(Action Class) based on the struts configuration file.
Struts Action Classes
•Acts as a bridge between client side user action and business operation
•Can perform other functions like authorization, logging and session validation before invoking the business operation.
•We usually extend the Action class and override the execute method

Class LoginAction extends Action{
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){
//take request parameters from the form and process them
// return the next view
return mapping.findForward(“nameOfNextView”)
}
}
Struts-config.xml
< struts-config >

< 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 >

< struts-config >
Dispatch Action
•Allows multiple operations normally scattered across multiple action classes to a single class.
• Related functionality can be kept in a DispatchAction subclass
< action path=“\login” type=”com.sample.struts.action.AccountAction” scope=”request” parameter=”method” name=”loginForm” >
< 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 >
class AccountAction extends DispatchAction{
public ActionForward viewAccount(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){
//take request parameters from the form and process them
// return the next view
return mapping.findForward(“nameOfNextView”)
}
}

Struts model components

Does not directly provide components for model since this is a web framework.

The following are the commonly used models.

•Data Transfer objects
•EJBs
•Business Object.
Struts view components
The components user for view in Struts are
•HTML
•Data Transfer Objects
•Struts Action Forms
•JSP
•Custom Tags
•Resource Bundles