Struts is a widely used open framework for developing Jave EE web applications
- Combines Java Servlets, JSP’s, custom tags and manages resources into a reusable framework
- Based on the Model-View-Controller Design pattern.
- Originally created by Craig McClanahan and donated to the Apache Foundation
In this session -we’ll look on
Model-View-Controller (MVC) Architecture
- How struts implements MVC architecture
- Discuss different components of struts
- A Sample Struts Application
- Advantage of using Struts
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 viewreturn 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
<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 used for view in Struts are
- HTML
- Data Transfer Objects
- Struts Action Forms
- JSP
- Custom Tags
- Resource Bundles

0 Comments.