Basic Struts Architecture Server Struts Framework Request Control

Basic Struts Architecture Server Struts Framework Request Control Server Client Model Database Response View 25. 09. 2020 1

Struts 2 flow (a bird eye view) Struts. xml Request …/blah. action source: www. coreservlets. com; Jakarta Struts: Processing Requests with Action Objects Struts 1. 2 Version 25. 09. 2020 2

Struts 2 Lifecycle Source: http: //static. raibledesigns. com/repository/presentations/Migrating. From. Struts 1 To. Struts 2. pdf 25. 09. 2020 3

Struts 2 – Behind the scenes 25. 09. 2020 4

Struts 2 – Behind the scenes (Cont…) Http. Servlet. Request – Request goes to servlet container Filters - various filters are applied Filter. Dispatcher – required filter dispacher is called Action. Mapper –dispatcher consults mapper to invoke action or not Action. Proxy – control is passed to proxy and it consults configuration for appropriate action and settings and then creates invocation Action. Invocation – calls interceptors before calling the Action Interceptors – interceptors do pre. Actions Action. Execution – Action method is called Results. Preparation – result is rendered depending method response Interceptors – interceptors are called in reverse order for post. Actions Filters – response is passed through filters Http. Servlet. Response – response is given to client 25. 09. 2020 manager upon the action 5

Hello World! One time configuration Web. xml Three steps process Create view (JSP) Create Action Class Map Action and view 25. 09. 2020 6

Web. xml Placed in WEB-INF folder <? xml version="1. 0" encoding="UTF-8"? > <web-app id="Web. App_9" version="2. 4" xmlns="http: //java. sun. com/xml/ns/j 2 ee" xmlns: xsi="http: //www. w 3. org/2001/XMLSchema-instance" xsi: schema. Location="http: //java. sun. com/xml/ns/j 2 ee/web-app_2_4. xsd"> <display-name>Struts Example</display-name> <filter-name>default. Dispatcher</filter-name> <filter-class>org. apache. struts 2. dispatcher. Filter. Dispatcher</filter-class> </filter> <filter-mapping> <filter-name>default. Dispatcher</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app> 25. 09. 2020 7

Welcome. jsp Creating Welcome Page <%@ page content. Type="text/html; charset=UTF-8" %> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>Welcome</title> </head> <body> <s: property value="message"/></b> </body> </html> 25. 09. 2020 8

Welcome. java Creating Welcome Page package example; import com. opensymphony. xwork 2. Action. Support; public class Welcome extends Action. Support{ private String message = "default String"; public String get. Message() { return message; } public void set. Message(String message) { this. message = message; } public String execute() throws Exception { message = “Hello World! My First App is running"; return SUCCESS; // String SUCCESS = “success” } } 25. 09. 2020 9

Welcome. java Either implement Action. Support Or extend Action Or neither Do provide execute method Framework will search for execute method through Reflection Default execute method returns SUCCESS Execute is the default entry point for Action class Some return strings constants are already provided SUCCESS=“success” INPUT=“input” NONE=“none” ERROR=“error” LOGIN=“login” SUCCESS is the default return string Also returned by default execute method 25. 09. 2020 10

Struts. xml Creating Welcome Page <? xml version="1. 0" encoding="UTF-8" ? > <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2. 0//EN" "http: //struts. apache. org/dtds/struts-2. 0. dtd"> <struts> <constant name="struts. enable. Dynamic. Method. Invocation" value=“false" /> <constant name="struts. dev. Mode" value="false" /> <include name=“some. File. xml” /> <package name="Struts. Example" extends="struts-default"> <action name="Welcome" class="example. Welcome"> <result>/jsp/welcome. jsp</result> </action> </package> </struts> 25. 09. 2020 11

Welcome Page url= http: //localhost: 8081/struts 2/Welcome. action Welcome. java welcome. jsp private String message = ; String get. Message() { message; "default String" public <s: property value="message"/> <action name="Welcome" return class="example. Welcome"> } public void set. Message (String message) { this. message = message; } 25. 09. 2020 12

Behind the scenes 25. 09. 2020 Source: http: //struts. apache. org/2. x/docs/big-picture. html 13

Struts 2 features Struts Tag Library Wildcard mappings Validation Localization OGNL expression language Interceptors Dynamic Method Invocation Profiling Debugging Annotations Type Conversion Result Types Dependency Injection Development Mode 25. 09. 2020 14

Struts Tag Library Login. jsp Creating Login Page <%@ page content. Type="text/html; charset=UTF-8" %> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>Welcome</title> </head> <body> <s: form action="Login"> <s: property value="error. Message"/> <s: textfield name="username" label="User Name"/> <s: password name="password" label="Password" /> <s: submit></s: submit> </s: form> </body> </html> 25. 09. 2020 15

Struts Tag Library Login. java Creating Login Page package example; import com. opensymphony. xwork 2. *; public class Login extends Action. Support { private String username = "default"; private String password = ""; private String error. Message = ""; public String get. Username() { return username; } public void set. Username (String username) { this. username = username; } public String get. Password () { return password; } public String get. Error. Message () { return error. Message; } public void set. Error. Message (String error. Message) {this. error. Message = error. Message; } public void set. Password (String password) {this. password = password; } public String execute () throws Exception { if (is. Valid(get. Username()) && is. Valid(get. Password())){ return SUCCESS; } error. Message = "invalid input"; return "input. Error"; } public boolean is. Valid(String field){ return field. length()!=0? true: false; 25. 09. 2020 } } 16

Struts Tag Library Struts. xml Creating Welcome Page <? xml version="1. 0" encoding="UTF-8" ? > <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2. 0//EN" "http: //struts. apache. org/dtds/struts-2. 0. dtd" > <struts> <constant name="struts. enable. Dynamic. Method. Invocation" value=“false" /> <constant name="struts. dev. Mode" value="false" /> <action name="Login" class="example. Login"> <result>/jsp/success. jsp</result> <result name="input. Error">/jsp/login. jsp</result> </action> <package name="Struts. Example" extends="struts-default"> <action name="Welcome" class="example. Welcome"> <result>/jsp/welcome. jsp</result> </action> </package> </struts> 25. 09. 2020 17

Struts Tag Library welcome. jsp Adding links for Login Page <%@ page content. Type="text/html; charset=UTF-8" %> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>Welcome</title> </head> <body> <b>s: property value="message"/></b> <p><a href="<s: url action="Login"/>">Login</a> <s: url id=“some. ID" action="Welcome"> <s: param name="param 1">value 1</s: param> </s: url> <p><s: a href=“%{some. ID}">Welcome</s: a> </body> </html> 25. 09. 2020 18

Wildcard mappings Provides generic mappings Guest. Login and Premier. Login can both be mapped to *Login <action name="edit*" class="example. Edit{1}"> <result name="failure" path="/main. Menu. jsp"/> <result path="/jsp/{1}. jsp"/> </action> Calls Edit. User & Edit. Registration for /edit. User & /edit. Registration respectively Returns /jsp/User. jsp & /jsp/Registration. jsp respectively 25. 09. 2020 19

Validation Define configuration file *-validation. xml or use annotations class. Name-validation. xml Place in the directory where. class file is placed example. Login should have Login-validation. xml <!DOCTYPE validators PUBLIC "-//Open. Symphony Group//XWork Validator 1. 0. 2//EN" "http: //www. opensymphony. com/xwork-validator-1. 0. 2. dtd"> <validators> <field name="username"> <field-validator type="requiredstring"> <message>Username is required</message> </field-validator> </field> <field name="password"> <field-validator type="requiredstring"> <message>Password is required</message> </field-validator> </field> </validators> 25. 09. 2020 20

Localization Get message from resource bundles Generalizes the messages Reusable messages Create <classname>. properties file for specific class and place in the class path Create package. properties file for specific package Searches for properties file in order 25. 09. 2020 Action. Class. properties Base. Class. properties Interface. properties (every interface and sub-interface) Model. Driven's model (if implements Model. Driven), for the model object repeat from 1 package. properties (of the directory where class is located and every parent directory all the way to the root directory) search up the i 18 n message key hierarchy itself global resource properties 21

Localization Login. properties user. required=User Name is required password. required=Password is required Login-validation. xml <validators> <field name="username"> <field-validator type="requiredstring"> <message key="username. required"/> </field-validator> </field> <field name="password"> <field-validator type="requiredstring"> <message key="password. required"/> </field-validator> </field> </validators> 25. 09. 2020 22

Localization cont… Use either of <s: property value="get. Text('some. key')" /> 25. 09. 2020 for accessing properties from JSP or Java file <s: text name="some. key" /> <s: text name="some. invalid. key" > The Default Message That Will Be Displayed </s: text> 23

Interceptors Can execute code before and after execution Are thread-safe Can be used for Validation Pre populating fields Double-submit prevention Session control Authentication Type conversion 25. 09. 2020 24
- Slides: 24