Example to create struts 2 application in MyEclipse


Here, we will make the struts 2 application utilizing myeclipse ide. We don't have to think about the container records on the grounds that MyEclipse gives these container documents.

You need to pursue these steps to make struts 2 application.

  1. Create a web project
  2. Add struts 2 capabilities
  3. Create input page (index.jsp)
  4. Create the activity class (Product.java)
  5. Map the solicitation with the activity in (struts.xml) document and characterize the view components
  6. Create see parts (welcome.jsp)
  7. start server and send the project

1) Create a web project

To make web venture, click on the file menu - new - project - web project - compose the undertaking name for example firststruts - finish.

2) Add struts 2 capabilities

To include struts 2 capacities, select you anticipate - click on the myeclipse menu - add venture 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.javacodegeeks;  
  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 document enlists the activity 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.javacodegeeks.Product">  
  8. <result name="success">welcome.jsp</result>  
  9. </action>  
  10.   
  11. </package>  
  12. </struts>      

6) Create see segments (welcome.jsp)

This jsp page shows the data set in the activity 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) begin server and send the project

To begin the server and send the undertaking, right snap on your task - Run As - MyEclipse server application.






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