Struts2 下载官网:http://struts.apache.org/download.cgi#struts2510
不能怕麻烦,尽量不要复制代码
一、在官网下载最新版本的Struts2
二、将下载好的文件解压到指定文件夹
选择lib中的jar包 全选放到 eclipse中WEB-INF/lib中
三、配置web.xml 文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
<display-name>struts2-2</display-name>
<filter>
<filter-name>action2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<!-- END SNIPPET: filter -->
<filter-mapping>
<filter-name>action2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
注:2.3版本的这句不太一样<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
2.3版本<filter-class>org.apache.struts2.dispatcher..ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
四、创建index.jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="product-input.action">product-input</a>
</body>
</html>
五、配置Struts.xml文件,复制http://struts.apache.org/dtds/struts-2.5.dtd到window-priferenes-add,只有这样Struts.xml 才可以配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="helloWorld" extends="struts-default">
<action name="product-input">
<result>/WEB-INF/pages/input.jsp</result>
</action>
<action name="product-save" class="com.lit.struts2.helloworld.Product"
method="save">
<result name="details">/WEB-INF/pages/details.jsp</result>
</action>
</package>
</struts>
六、创建类product.java
package com.lit.struts2.helloworld;
public class Product {
private Integer productId;
private String productName;
private String productDesc;
private double productPrice;
public Integer getProductId() {
return productId;
}
public void setProductId(Integer productId) {
this.productId = productId;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getProductDesc() {
return productDesc;
}
public void setProductDesc(String productDesc) {
this.productDesc = productDesc;
}
public double getProductPrice() {
return productPrice;
}
public void setProductPrice(double productPrice) {
this.productPrice = productPrice;
}
@Override
public String toString() {
return "Product [productId=" + productId + ", productName=" + productName + ", productDesc=" + productDesc
+ ", productPrice=" + productPrice + "]";
}
public String save() {//与Struts.xml中的method="save">照应
System.out.println("save:" + this);
return "details";// <result name="details">
}
}
创建input.jsp页面,在WEB-INF/pages中创建input.jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="product-save.action" method="post">
productId:<input type="text" name="productId"/>
<br><br>
productName:<input type="text" name="productName"/>
<br><br>
productDesc:<input type="text" name="productDesc"/>
<br><br>
productPrice:<input type="text" name="productPrice"/>
<br><br>
<input type="submit" value="查询"/>
</form>
</body>
</html>
创建details.jsp页面,在WEB-INF/pages中创建details.jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
productId:${productId }
<br><br>
productName:${productName }
<br><br>
productDesc:${productDesc }
<br><br>
productPrice:${productPrice }
</body>
</html>
到此页面即成功!