Struts 2 introduction Struts 2 framework Struts 2 A full-featured web application framework for the Java EE platform The Java Servlet API exposes the HTTP client / server protocol to the Java platform. Struts 2 is built on top of the Java Servlet API layer. A framework makes generalizations about the common tasks and workflow of a specific domain. It then attempts to provide a platform upon which applications of that domain can be quickly built. Struts 2 Java Servlet specification HTTP History of Struts Struts 2 Is from Apache software foundation ASF is a non-profit corporation (decentralized community of developers) supporting Apache software projects including the Apache HTTP server. Struts 2 is a brand new web application framework It is not just a new release of the Struts 1 framework, but a completely new framework based on the OpenSymphony WebWork framework. • Struts 2 implements the popular Model-ViewController (MVC) design pattern. • The MVC pattern provides a separation of concerns that applies well to web applications. • Separation of concerns allows us to manage the complexity of large software systems by dividing them into highlevel components Struts 2 implementation of the MVC Pattern Web browser client HTTP request Controller FilterDispatcher render page invoke action Model Action select result View Result Struts 2 MVC is realized by three core framework components : actions, results, and the FilterDispatcher Struts 2 Request processing invoke action finished interceptors OGNL invoke result Action Result OGNL ActionContext (ThreadLocal) ValueStack request session … Struts 2 request processing uses interceptors that fire before and after the action and result Declarative architecture [1] Declarative architecture Is a type of configuration that allows developers to describe their application architecture (configure) at a higher level than direct programmatic manipulation Configuration occurs at two levels in Struts As the Struts framework is flexible, it allows the developer to tweak its behaviour in many areas. This activity is actually configuring the behaviour of the Struts framework The more important type of configuration (or declarative architecture) refers to defining the Struts 2 components that are used by your application and wiring / linking them together to form your desired workflow paths Workflow paths refer to which action fires when a particular URL is hit, and which results might be chosen by that action to complete processing. Declarative architecture [2] Multiple methods There are two methods of declaring the architecture of the application being developed Through XML based configuration files Through java annotations Whether the declaration is in XML or using java annotations, the framework translates them both into the same run time components With XML, one has the familiar XML configuration document with elements describing the application actions, results and interceptors. struts.xml With java annotations, the XML is gone. The metadata is located as java annotations inside the java source code for the classes that implement the Struts 2 actions. An annotated source file is shown below. HelloWorldAction.java URL mapping to struts actions [helloworldxml project] http:// + localhost:8080 + /helloworldxml + <package name space> + /HelloWorld.action Protocol hostname:portnumber servlet context package name space action name.action The URL combines the servlet context with the package namespace and the action name. Note that the action name takes the .action extension. If the package name space is specified, that should be used as part of the struts action identifier URL, else it should be spaces. Struts 2 data flow trace for the helloworldxml application Form rendered by index.jsp <s:form action="/strutshello/HelloWorld.a ction"> <s:textfield name="userName" label="User Name"/> <s:submit /> </s:form> ValueStack HelloWorld Action Form rendered by success.jsp <h1><s:property value="message" /></h1> String userName String message Struts 2 configuration Configuration files • • The struts-config.xml configuration file is a link between the View and Model components in the Web Client but you would not have to touch these settings for 99.99% of your projects. web.xml Is a J2EE configuration file that determines how the elements of the HTTP request are processed by the servlet container. It is not a Struts 2 configuration file, but it is a file that needs to be configured for Struts to work struts.xml Contains the configuration information that developer will be modifying as actions are developed. This is created under the directory WEBINF/classes. struts-config.xml • struts.properties • This configuration file provides a mechanism to change the default behaviour of the framework. This file should be created in the WEBINF/classes folder.Any line starting with # in this file is treated as comments.