Struts Framework: Struts Framework Avanish Pandey
Struts History: Struts History Created in 2000 by Craig R. McClanahan Donated to ASF( Apache Software Foundation ) in 2000 Current stable release: 2.3
Model-View-Controller (MVC): Model-View-Controller (MVC) Model – The persistent data (typically in a database) and business logic functionality View – The user interface (typically HTML forms in a web application) Controller – Management software to dispatch form submits to the corresponding business logic, and trigger the display of the appropriate next page
Struts MVC: Struts MVC Struts does not specify how to implement Model part of MVC JSP and Struts Tag Libraries used for View part of MVC Struts Controller implemented as: ActionServlet and ActionMapping 3 Major Components Servlet controller (Controller) Java Server Pages (View) Application Business Logic (Model)
MVC As Implemented In Struts: MVC As Implemented In Struts Browser Controller Business Logic Model Data View (2) Dispatch (3) Update, Get (4) Dispatch (5) Pull (6) Render forward (1) submit
Struts Tag libraries: Struts Tag libraries struts-bean.tld – Fundamental bean manipulation and internationalization. struts-html.tld – “Smart” HTML elements. struts-logic.tld – Basic conditionals and iteration. struts-template.tld – Basic layout management. struts-nested.tld -- “Nested” variants of standard tags that resolve relative references against beans. struts-tiles.tld – Full features layout management library.
Controller Components: Controller Components The action mapping example <action-mappings> <action path="/ addBookAction “ type= " edu.depaul.struts_example.BookInputAction " name=" addBookBean " scope="request" input="/add_book.jsp"> <forward name="Go" path="/result.jsp"/> <forward name="cancel" path="/index.html"/> </action> Form Bean name mapping type Action mapping name forward definition
How to build a Struts application?: How to build a Struts application? Build the Model Components ( ActionForm Class ) Build the Controller Components (Action Class ) Build the View Components (JSPs ) Create the struts-config.xml file Update the web.xml file
Building an ActionForm: Building an ActionForm The ActionForm (also called the form) is a data holder that contains the data entered trough an HTML form. The form should also contain the get & set methods for each property. Override: validate() - optional reset() - optional To create a form, you will need to extend the class org.apache.struts.action.ActionForm Action Forms are mapped to HTML forms – hence the name .
Building an ActionServlet: Building an ActionServlet ActionServlet is used to control the request and send the appropriate response to that request. For every “action” in the application , a custom “ Action ” is defined These are similar to servlets . Short – lived (for the duration of the request).
PowerPoint Presentation: Stateless – do not put any state in the Action classes Package is used org.apache.struts.action.ActionServlet Implementations subclass this class and provide implementation of execute method: ActionForward execute( ActionMapping , ActionForm , HttpServletRequest , HttpServletResponse );
Building a JSP: Building a JSP To create a JSP using Struts you will use the tag libraries provided by the Struts Framework .
Create the struts-config.xml file: Create the struts-config.xml file To allow for better reusability, all components in Struts are loosely coupled. To integrate the components you must create a config file (struts-config.xml ). Within this file you’ll define the action-mappings and form-beans. The action-mappings tell the ActionServlet which action classes to include within the application. The form-beans define the ActionForm classes required within the application.
Login Example: Login Example Struts Framework in
Netbeans directory structure: Netbeans directory structure
Decomposing Struts Application (web.xml): Decomposing Struts Application (web.xml) < servlet > < servlet -name>action</ servlet -name> < servlet -class> org.apache.struts.action.ActionServlet </ servlet -class> </ servlet > < servlet -mapping> < servlet -name>action</ servlet -name> < url -pattern>*.do</ url -pattern> </ servlet -mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list>
Login.jsp: Login.jsp
LoginForm.java: LoginForm.java
LoginAction.java: LoginAction.java
struts-config.xml: struts-config.xml
Request Processing: Request Processing Web Browser displays page Controller ActionForm Action Model struts-config JSP Page 1 2 3 4 5 6 7 8
Struts-Tiles: Struts-Tiles Tool for creating reusable layouts for sites Open Source Project (ASF) Integrated with Struts Not Enabled by default Allows for tiles definitions in XML
home.jsp: home.jsp <%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles"%> <html><body> <div id=“header"></div> <div id=“navigation”></div> <div id=“right-side"></div> < html:link action="/contact">Contact Us<span class=" ui_icon contactus "></span></ html:link ></ li > <div id="content"> < tiles:insert attribute=" userbody " /> </div> <div id=“footer"></div> </body></html>
Struts-Tiles: Struts-Tiles Provide Action Mapping in the Struts-config.xml <action name=" ContactForm " path="/ contactQuery " scope="request" type=" com.domain.selfbook.ContactAction "> <forward name="success" path="body5" /> </action>
Struts-Tiles: Struts-Tiles Provide Tiles Definitions in the tiles-defs.xml file : <definition name="body5" path="/home.jsp"> <put name=" userbody " value="/contact.jsp"/> </definition>
Struts Tutorials: Struts Tutorials http://www.mkyong.com/tutorials/struts-tutorials/ http://www.vaannila.com/struts/struts-tutorial/struts-tutorial.html http://www.roseindia.net/struts/