Steps to create Struts 2 Application Example

In this model, we are making the struts 2 precedent without IDE. We can just make the struts 2 application by following these straightforward steps:

  1. Create the registry framework
  2. Create input page (index.jsp)
  3. Provide the passage of Controller in (web.xml) file
  4. Create the activity class (
  5. Map the solicitation with the activity in (struts.xml) record and characterize the view components
  6. Create see segments (welcome.jsp)
  7. load the container files
  8. start server and send the project

1) Create the index framework

The index framework of struts 2 is same as servlet/JSP. Here, struts.xml document must be situated in the classes folder.

directory framework of struts 2 application

2) Create input page (index.jsp)

This jsp page makes a framework utilizing struts UI labels. To utilize the struts UI labels, you have to determine uri/struts labels. Here, we have utilized s:form to make a framework, s:textfield to make a content field, s:submit to make a submit button.

  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 passage of Controller in (web.xml) file

In struts 2, StrutsPrepareAndExecuteFilter class fills in as the controller. As we probably am aware well, struts 2 utilizes channel for the controller. It is certainly given by the struts framework.

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app>  
  3.   <filter>  
  4.   <filter-name>struts2</channel name>  
  5.    <filter-class>  
  7.    </channel class>  
  8.   </filter>  
  9.   <filter-mapping>  
  10.    <filter-name>struts2</channel name>  
  11.     <url-pattern>/*</url-pattern>  
  12.   </channel mapping>  
  13. </web-app>  

4) Create the activity class (

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.
  1. package com.javacodegeeks;  
  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. = id;  
  12. }  
  13. public String getName() {  
  14.     return name;  
  15. }  
  16. public void setName(String name) {  
  17. = name;  
  18. }  
  19. public float getPrice() {  
  20.     return price;  
  21. }  
  22. public void setPrice(float price) {  
  23.     this.price = price;  
  24. }  
  26. public String execute(){  
  27.     return "success";  
  28. }  
  29. }  

5) Map the solicitation in (struts.xml) document and characterize the view components

It is the significant document from where struts system gets data about the activity and chooses which result to be conjured. Here, we have utilized numerous components, for example, struts , bundle, activity and result.

struts component is the root components of this document. It speaks to an application.

package component is the sub component of struts . It speaks to a module of the application. It for the most part broadens the struts-default bundle where numerous interceptors and result types are defined.

action component is the sub component of bundle. It speaks to an activity to be conjured for the approaching solicitation. It has name, class and strategy qualities. On the off chance that you don't determine name characteristic as a matter of course execute() strategy will be conjured for the predetermined activity class.

result component is the sub component of activity. It speaks to a view (result) that will be summoned. struts structure checks the string returned by the activity class, in the event that it returns achievement, result page for the activity is conjured whose name is achievement or has no name. It has name and type properties. Both are discretionary. In the event that you don't indicate the outcome name, as a matter of course achievement is accepted as the outcome name. On the off chance that you don't indicate the sort property, as a matter of course dispatcher is considered as the default result type. We will find out about outcome types later.

  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC "- //Apache Software Foundation//DTD Struts  
  3. Configuration 2.1//EN" " 2.1.dtd">  
  4. <struts>  
  5. <package name="default" extends="struts-default">  
  7. <action name="product" class="com.javacodegeeks.Product">  
  8. <result name="success">welcome.jsp</result>  
  9. </action>  
  11. </package>  
  12. </struts>      

6) Create see parts (welcome.jsp)

It is the view part the presentations data of the activity. Here, we are utilizing struts labels to get the information.

The s:property label restores the incentive for the given name, put away in the activity object.

  1. <%@ taglib uri="/struts tags" prefix="s" %>  
  3. Product Id:<s:property value="id"/><br/>  
  4. Product Name:<s:property value="name"/><br/>  
  5. Product Price:<s:property value="price"/><br/>   Product Id:<s:property value="id"/><br/> Product Name:<s:property value="name"/><br/> Product Price:<s:property value="price"/><br/>

start server and deploy the project

Finally, start the server and deploy the project and access it.

struts 2 example output

javacodegeeks is optimized for learning.© javacodegeeks .
All Right Reserved and you agree to have read and accepted our term and condition