You are on page 1of 13

Overview

• Introduce Struts 2
• Compare Struts & Struts 2
• Build Struts 2 + Spring 2 + JPA application
Struts 2
• Struts and webwork has joined together to develop
the Struts 2 Framework
• Struts 2 is an elegant, extensible framework for
building enterprise Java web applications
• Strut 2 actions are Spring friendly and so easy to
Spring integration.
• AZAX theme enables to make the application more
dynamic.
• Configuration files are reloadable that allows changes
without restarting a web container.
Struts 2

Client
Struts 2
• Request Lifecycle in Struts 2 applications
1.User Sends request: User sends a request to the server for some resource.

2.FilterDispatcher determines the appropriate action: The FilterDispatcher looks at


the request and then determines the appropriate Action.

3.Interceptors are applied: Interceptors configured for applying the common


functionalities such as workflow, validation, file upload etc. are automatically applied to
the request.

4.Execution of Action: Then the action method is executed to perform the database
related operations like storing or retrieving the data from database.

5.Output rendering: Then the Result render the output.

6.Return of Request: Then the request returns through the interceptors in the reverse
order. The returning request allows us to perform the clean-up or additional processing.

7.Display the result to user: Finally the control is returned to the servlet container,
which sends the output to the user browser.
Struts 2
Struts 2 Tags
• Generic Tags
– Control Tags: if, else , elseIf, iterator,…
– Data Tags: action, bean, param, include, …
• UI Tags
– Form Tags: form. Checkbox, textfield, password,…
– Non-Form UI Tags: table, tree, div,…
• Ajax Tags
• Reference: http://struts.apache.org/2.x/docs/ajax-
tags.html
Struts 2
• Apache Struts 2 requires:
– Servlet API 2.4
– JSP API 2.0
– Java 5
Struts & Struts 2
• Configuring the framework (web.xml)]
– Struts
<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>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
Struts & Struts 2
• Configuring the framework (web.xml)]
– Struts 2
<filter>
<filter-name>webwork</filter-name>
<filter-class>
org.apache.struts.action2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>webwork</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Struts & Struts 2
• Structure of the Struts action
– Struts
public class MyAction extends Action {
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
// do the work
return (mapping.findForward("success"));
}
}
Struts & Struts 2
• Structure of the Struts action
– Struts 2
public class MyAction {
public String execute() throws Exception {
// do the work
return "success";
}
}

Any method that follows the method signature public String


methodName() can be invoked through configuration.

Interface Action available that provides the common results of


"success", "none", "error", "input" and "login" as constants.
Reference Documents
• http://cwiki.apache.org/S2WIKI/home.html
• http://www.roseindia.net/struts/struts2/index.shtml

You might also like