Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

struts 2 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 – newprojectweb 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 capabilitiesadd struts capabilities.

stout2

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.Kreationnext;
  2. public class Product {
  3. private int id;
  4. private String name;
  5. private float price;
  6. public int getId() {
  7.     return id;
  8. }
  9. public void setId(int id) {
  10.     this.id = id;
  11. }
  12. public String getName() {
  13.     return name;
  14. }
  15. public void setName(String name) {
  16.     this.name = name;
  17. }
  18. public float getPrice() {
  19.     return price;
  20. }
  21. public void setPrice(float price) {
  22.     this.price = price;
  23. }
  24. public String execute(){
  25.     return “success”;
  26. }
  27. }

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. <action name=“product” class=“com.Kreationnext.Product”>
  7. <result name=“success”>welcome.jsp</result>
  8. </action>
  9. </package>
  10. </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. Product Id:<s:property value=“id”/><br/>
  3. Product Name:<s:property value=“name”/><br/>
  4. 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.

Comments

comments