struts

Struts 2 Tutorial

struts 2 framework
The struts 2 framework is used to develop MVC-based web application.
The struts framework was initially created by Craig McClanahan and donated to Apache Foundation in May, 2000 and Struts 1.0 was released in June 2001.
The current stable release of Struts is 2.3.15.1 GA in July 16, 2013.
This struts 2 tutorial covers all the topics of Struts 2 Framework with simplified examples for beginners and experienced persons.

Struts 2 Framework

The Struts 2 framework is used to develop MVC (Model View Controller) based web applications. Struts 2 is the combination of webwork framework of opensymphony and struts 1.
  1. struts2 = webwork + struts1  
The Struts 2 provides supports to POJO based actions, Validation Support, AJAX Support, Integration support to various frameworks such as Hibernate, Spring, Tiles etc, support to various result types such as Freemarker, Velocity, JSP etc.

Struts 2 Features Tutorial

Struts 2 provides many features that were not in struts 1. The important features of struts 2 framework are as follows:
  1. Configurable MVC components
  2. POJO based actions
  3. AJAX support
  4. Integration support
  5. Various Result Types
  6. Various Tag support
  7. Theme and Template support

1) Configurable MVC components

In struts 2 framework, we provide all the components (view components and action) information in struts.xml file. If we need to change any information, we can simply change it in the xml file.

2) POJO based actions

In struts 2, action class is POJO (Plain Old Java Object) i.e. a simple java class. Here, you are not forced to implement any interface or inherit any class.

3) AJAX support

Struts 2 provides support to ajax technology. It is used to make asynchronous request i.e. it doesn't block the user. It sends only required field data to the server side not all. So it makes the performace fast.

4) Integration Support

We can simply integrate the struts 2 application with hibernate, spring, tiles etc. frameworks.

5) Various Result Types

We can use JSP, freemarker, velocity etc. technologies as the result in struts 2.

6) Various Tag support

Struts 2 provides various types of tags such as UI tags, Data tags, control tags etc to ease the developement of struts 2 application.

7) Theme and Template support

Struts 2 provides three types of theme support: xhtml, simple and css_xhtml. The xhtml is default theme of struts 2. Themes and templates can be used for common look and feel.

Example to create struts 2 application in MyEclipse

Here, we are going to create the struts 2 application using myeclipse ide. We don't need to care about the jar files because MyEclipse provides these jar files.
You need to follow these steps to create struts 2 application.
  1. Create a web project
  2. Add struts 2 capabilities
  3. Create input page (index.jsp)
  4. Create the action class (Product.java)
  5. Map the request with the action in (struts.xml) file and define the view components
  6. Create view components (welcome.jsp)
  7. start server and deploy the project

1) Create a web project

To create web project, click on the file menu - new - project - web project - write the project namee.g. firststruts - finish.

2) Add struts 2 capabilities

To add struts 2 capabilities, select you project - click on the myeclipse menu - add project capabilitiesadd struts capabilities.
struts 2 with myeclipse ide
Select the 2.1 and /* as the url pattern - finish.

3) Create input page (index.jsp)

It uses struts core tags to create a form with fields.
index.jsp
  1. <%@ taglib uri="/struts-tags" prefix="s" %>  
  2. <s:form action="product">  
  3. <s:textfield name="id" label="Product Id"></s:textfield>  
  4. <s:textfield name="name" label="Product Name"></s:textfield>  
  5. <s:textfield name="price" label="Product Price"></s:textfield>  
  6. <s:submit value="save"></s:submit>  
  7. </s:form>  

4) Create the action class (Product.java)

It is the simple action class containing properties with setters and getters. It contains the execute method also for defining the business logic.
Product.java
  1. package com.poosan;  
  2.   
  3. public class Product {  
  4. private int id;  
  5. private String name;  
  6. private float price;  
  7. public int getId() {  
  8.     return id;  
  9. }  
  10. public void setId(int id) {  
  11.     this.id = id;  
  12. }  
  13. public String getName() {  
  14.     return name;  
  15. }  
  16. public void setName(String name) {  
  17.     this.name = name;  
  18. }  
  19. public float getPrice() {  
  20.     return price;  
  21. }  
  22. public void setPrice(float price) {  
  23.     this.price = price;  
  24. }  
  25.   
  26. public String execute(){  
  27.     return "success";  
  28. }  
  29. }  

5) Map the request in (struts.xml) file and define the view components

This xml file registers the action and view components.
struts.xml
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts  
  3. Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">  
  4. <struts>  
  5. <package name="default" extends="struts-default">  
  6.   
  7. <action name="product" class="com.poosan.Product">  
  8. <result name="success">welcome.jsp</result>  
  9. </action>  
  10.   
  11. </package>  
  12. </struts>      

6) Create view components (welcome.jsp)

This jsp page displays the information set in the action object.
welcome.jsp
  1. <%@ taglib uri="/struts-tags" prefix="s" %>  
  2.   
  3. Product Id:<s:property value="id"/><br/>  
  4. Product Name:<s:property value="name"/><br/>  
  5. Product Price:<s:property value="price"/><br/>  

7) start server and deploy the project

To start the server and deploy the project, right click on your project - Run As - MyEclipse server application.


Struts 2 Tiles Framework Integration Tutorial Example

       We can customize the layout of the struts 2 application by integrating with tiles framework.
A web page can contain many parts (known as tile) such as header, left pane, right pane, body part, footer etc. In tiles framework, we manage all the tile by our Layout Manager page.

Advantage of tiles framework

There are following advantages of tiles framework:
  • Customization by centralized page We can customize the layout of all the pages by single page (centralized page) only.
  • Code reusability A single part e.g. header or footer can be used in many pages. So it saves coding.
  • Easy to modify If any part (tile) is modified, we don't need to change many pages.
  • Easy to remove If any part (tile) of the page is removed, we don't need to remove the code from all the pages. We can remove the tile from our layout manager page.
struts 2 with tiles example

Steps to create tiles application

The steps are as follows:
  1. Add tiles library in your application
  2. Define Struts2TilesListener in web.xml file
  3. Create the input page (index.jsp)
  4. Create the Action class
  5. Extend the tiles-default package in your package and define all the result type as tiles in struts.xml file
  6. Create the tiles.xml file and define all the tiles definitions
  7. Create the LayoutManager page
  8. Create the View components

1) Add tiles library in your application

If you are using myeclipse IDE, you can add tiles library by right click on the project -> Build Path -> Add Library -> Add Myeclipse Library -> Select the Struts 2 tiles library -> ok.
If you are using eclipse or Netbeans IDE, you need to add the required tiles library in your project.
download the struts2 jar files

2) Define Struts2TilesListener in web.xml file

Provide entry of listener class Struts2TilesListener in the web.xml file.
web.xml
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5"   
  3.     xmlns="http://java.sun.com/xml/ns/javaee"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  6.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  7.   <welcome-file-list>  
  8.     <welcome-file>index.jsp</welcome-file>  
  9.   </welcome-file-list>  
  10.   <filter>  
  11.    <filter-name>struts2</filter-name>  
  12.    <filter-class>  
  13.     org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter  
  14.    </filter-class>  
  15.   </filter>  
  16.   <filter-mapping>  
  17.     <filter-name>struts2</filter-name>  
  18.     <url-pattern>/*</url-pattern>  
  19.   </filter-mapping>  
  20.     
  21.   <listener>  
  22.   <listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class>  
  23.   </listener>  
  24.     
  25.  </web-app>  

3) Create the input page (index.jsp)

index.jsp
  1. <%@ taglib uri="/struts-tags" prefix="s" %>  
  2.   
  3. <s:form action="login">  
  4. <s:textfield name="name" label="Name"></s:textfield>  
  5. <s:password name="password" label="Password"></s:password>  
  6. <s:submit value="login"></s:submit>  
  7. </s:form>  

4) Create the action class

This action class contains one field name and defines the execute method.
Login.java
  1. package com.poosan;  
  2.   
  3. public class Login {  
  4. private String name,password;  
  5.   
  6. //getters and setters  
  7.   
  8. public String execute(){  
  9. if(password.equals("admin")){  
  10.     return "success";  
  11. }  
  12. else{  
  13.     return "error";  
  14. }  
  15. }  
  16. }  

5) Inherit the tiles-default package and define all the result type as tiles in struts.xml

This xml file defines one package with one action and two results.
struts.xml
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"   
  3. "http://struts.apache.org/dtds/struts-2.1.dtd">  
  4. <struts>  
  5.   
  6.   
  7. <package name="abc" extends="tiles-default" >  
  8.   
  9. <action name="login" class="com.poosant.Login">  
  10. <result name="success" type="tiles">login-success</result>  
  11. <result name="error" type="tiles">login-error</result>  
  12. </action>  
  13.    
  14. </package>  
  15. </struts>      

6)Create the tiles.xml file and define all the tiles definitions

The tiles.xml file must be located inside the WEB-INF directory.
tiles.xml
  1. <?xml version="1.0" encoding="UTF-8" ?>   
  2.   
  3. <!DOCTYPE tiles-definitions PUBLIC   
  4. "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"  
  5.  "http://tiles.apache.org/dtds/tiles-config_2_0.dtd">   
  6.   
  7. <tiles-definitions>  
  8.      
  9.    <definition name="login-success" template="/layoutmanager.jsp">   
  10.    <put-attribute name="title" value="Welcome Page"/>   
  11.    <put-attribute name="body" value="/login-success.jsp"/>   
  12.    </definition>  
  13.      
  14.    <definition name="login-error" template="/layoutmanager.jsp">   
  15.    <put-attribute name="title" value="Login Error"/>   
  16.    <put-attribute name="body" value="/login-error.jsp"/>   
  17.    </definition>   
  18.      
  19. </tiles-definitions>  

7) Create the LayoutManager page

It is the layout manager page. It used getAsString tag of tiles to include the string resource and insertAttribute tag of tiles to include the page resource.
layoutmanager.jsp
  1. <%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  
  3.  "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. <title><tiles:getAsString name="title" /></title>  
  7. </head>  
  8. <body>  
  9.   
  10. <%@  include file="header.jsp" %>  
  11. <tiles:insertAttribute name="body" />  
  12. <%@ include file="footer.jsp" %>  
  13.   
  14. </body>  
  15. </html>   

8)Create View components

There are many view components such as header.jsp, footer.jsp, welcome.jsp etc.

header.jsp
  1. <h2 style="background-color:pink;text-align:center;">It is header tile</h2>  
  2. <hr/>  

footer.jsp
  1. <hr>  
  2. <h2 style="background-color:pink;text-align:center;">It is footer tile</h2>  

login-success.jsp
  1. <%@ taglib uri="/struts-tags" prefix="s" %>  
  2.   
  3. Welcome, <s:property value="name"/>  

login-error.jsp
  1. Sorry, username or password error!  
  2. <jsp:include page="index.jsp"></jsp:include>  
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment