Basic Struts Architecture Client Server Database Request Response Control View Model Server Struts Framework
Struts 2 flow (a bird eye view) source: Jakarta Struts: Processing Requests with Action Objects Struts 1.2 Versionwww.coreservlets.com Request …/blah.action Struts.xml
Struts 2 Lifecycle Source:
Struts 2 – Behind the scenes
Struts 2 – Behind the scenes (Cont…) HttpServletRequest – Request goes to servlet container Filters - various filters are applied FilterDispatcher – required filter dispacher is called ActionMapper –dispatcher consults mapper to invoke action or not ActionProxy – control is passed to proxy and it consults configuration manager for appropriate action and settings and then creates invocation ActionInvocation – calls interceptors before calling the Action Interceptors – interceptors do preActions ActionExecution – Action method is called ResultsPreparation – result is rendered depending upon the action method response Interceptors – interceptors are called in reverse order for postActions Filters – response is passed through filters HttpServletResponse – response is given to client
Hello World! One time configuration Web.xml Three steps process Create view (JSP) Create Action Class Map Action and view
Web.xml Placed in WEB-INF folder Struts Example defaultDispatcher org.apache.struts2.dispatcher.FilterDispatcher defaultDispatcher /*
Welcome.jsp Welcome Creating Welcome Page
Welcome.java package example; import com.opensymphony.xwork2.ActionSupport; public class Welcome extends ActionSupport{ private String message = "default String"; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public String execute() throws Exception { message = “Hello World! My First App is running"; return SUCCESS; // String SUCCESS = “success” } Creating Welcome Page
Welcome.java Either implement ActionSupport 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
Struts.xml <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" " /jsp/welcome.jsp Creating Welcome Page
welcome.jsp Welcome.java private String message = "default String" ; public String getMessage() { return message; } public void setMessage (String message) { this.message = message; } <action name="Welcome" class="example.Welcome"> url= Welcome Page
Behind the scenes Source:
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
Struts Tag Library Welcome Creating Login Page Login.jsp
Struts Tag Library package example; import com.opensymphony.xwork2.*; public class Login extends ActionSupport { private String username = "default"; private String password = ""; private String errorMessage = ""; public String getUsername() { return username; } public void setUsername (String username) { this.username = username;} public String getPassword () { return password;} public String getErrorMessage () { return errorMessage; } public void setErrorMessage (String errorMessage) {this.errorMessage = errorMessage;} public void setPassword (String password) {this.password = password;} public String execute () throws Exception { if (isValid(getUsername()) && isValid(getPassword())){ return SUCCESS;} errorMessage = "invalid input"; return "inputError"; } public boolean isValid(String field){ return field.length()!=0?true:false; } Creating Login Page Login.java
Struts Tag Library <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" " /jsp/success.jsp /jsp/login.jsp /jsp/welcome.jsp Creating Welcome Page Struts.xml
Struts Tag Library Welcome s:property value="message"/> ">Login value1 Welcome Adding links for Login Page welcome.jsp
Wildcard mappings Provides generic mappings GuestLogin and PremierLogin can both be mapped to *Login Calls EditUser & EditRegistration for /editUser & /editRegistration respectively Returns /jsp/User.jsp & /jsp/Registration.jsp respectively
Validation Define configuration file *-validation.xml or use annotations className-validation.xml Place in the directory where.class file is placed example.Login should have Login-validation.xml <!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN" " Username is required Password is required
Localization Get message from resource bundles Generalizes the messages Reusable messages Create.properties file for specific class and place in the class path Create package.properties file for specific package Searches for properties file in order ActionClass.properties BaseClass.properties Interface.properties (every interface and sub-interface) ModelDriven's model (if implements ModelDriven), 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 i18n message key hierarchy itself global resource properties
Localization user.required=User Name is required password.required=Password is required Login.properties Login-validation.xml
Localization cont… Use either of for accessing properties from JSP or Java file The Default Message That Will Be Displayed
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