Thursday 7 April 2016

STRUTS 2 EXAMPLE WITHOUT IDE

In this example, we are creating the struts 2 example without IDE. We can simply create the struts 2 application by following these simple steps:
  1. Create the directory structure
  2. Create input page (index.jsp)
  3. Provide the entry of Controller in (web.xml) file
  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. load the jar files
  8. start server and deploy the project

1) Create the directory structure

The directory structure of struts 2 is same as servlet/JSP. Here, struts.xml file must be located in the classes folder.
directory structure of struts 2 application

2) Create input page (index.jsp)

This jsp page creates a form using struts UI tags. To use the struts UI tags, you need to specify uri /struts-tags. Here, we have used s:form to create a form, s:textfield to create a text field, s:submit to create a submit button.
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>  

3) Provide the entry of Controller in (web.xml) file

In struts 2, StrutsPrepareAndExecuteFilter class works as the controller. As we know well, struts 2 uses filter for the controller. It is implicitly provided by the struts framework.
web.xml
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app>  
  3.   <filter>  
  4.   <filter-name>struts2</filter-name>  
  5.    <filter-class>  
  6.     org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter  
  7.    </filter-class>  
  8.   </filter>  
  9.   <filter-mapping>  
  10.    <filter-name>struts2</filter-name>  
  11.     <url-pattern>/*</url-pattern>  
  12.   </filter-mapping>  
  13. </web-app>  

4) Create the action class (Product.java)

This is simple bean class. In struts 2, action is POJO (Plain Old Java Object). It has one extra method execute i.e. invoked by struts framework by default.
Product.java
  1. package com.javatpoint;  
  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

It is the important file from where struts framework gets information about the action and decides which result to be invoked. Here, we have used many elements such as struts, package, action and result.
struts element is the root elements of this file. It represents an application.
package element is the sub element of struts. It represents a module of the application. It generally extends the struts-default package where many interceptors and result types are defined.
action element is the sub element of package. It represents an action to be invoked for the incoming request. It has name, class and method attributes. If you don't specify name attribute by default execute() method will be invoked for the specified action class.
result element is the sub element of action. It represents an view (result) that will be invoked. Struts framework checks the string returned by the action class, if it returns success, result page for the action is invoked whose name is success or has no name. It has name and type attributes. Both are optional. If you don't specify the result name, by default success is assumed as the result name. If you don't specify the type attribute, by default dispatcher is considered as the default result type. We will learn about result types later.
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.javatpoint.Product">  
  8. <result name="success">welcome.jsp</result>  
  9. </action>  
  10.   
  11. </package>  
  12. </struts>      

6) Create view components (welcome.jsp)

It is the view component the displays information of the action. Here, we are using struts tags to get the information.
The s:property tag returns the value for the given name, stored 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) Load the jar files

To run this application, you need to have the struts 2 jar files. Here, we are providing all the necessary jar files for struts 2. Download it and put these jar files in the lib folder of your project.

8) start server and deploy the project

Finally, start the server and deploy the project and access it.
struts 2 example output
struts 2 example
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 name e.g. firststruts -finish.

2) Add struts 2 capabilities

To add struts 2 capabilities, select you project - click on the myeclipse menu - add project capabilities - add 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.javatpoint;  
  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.javatpoint.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.

No comments:

Post a Comment