下载struts2
网址:http://struts.apache.org/download.cgi
一般下载Full Distribution完整all版本,该版本目录结构:
- apps里包含struts2示例应用
- docs包含struts2相关文档如API文档
- lib包含struts2框架核心类库,和第三方插件库
- src包含struts2全部源代码
搭建struts2环境
- 添加jar 包: 将struts\apps\struts2-blank\WEB-INF\lib 下的所有 jar 包复制到当前 web 应用的 lib 目录下;
- web.xml 文件中配置 struts2: 将 struts\apps\struts2-blank1\WEB-INF\web.xml 文件中的过滤器的配置代码复制到当前 web 应用的 web.xml 文件中;
- 添加struts2 的配置文件struts.xml: 复制 struts1\apps\struts2-blank\WEB-INF\classes 下的 struts.xml 文件到当前 web 应用的 src 目录下。
注:为了在eclipse下能正常提示struts2语法,要添加DTD约束。
struts-2.3.28\src\core\src\main\resources\struts-2.3.dtd
创建第一个struts2应用
应用效果:
代码:
index.jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="product-input.action">Product Input</a>
</body>
</html>
input.jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="product-save.action" method="post">
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="Submit"/>
<br><br>
</form>
</body>
</html>
struts.xml配置:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.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.afy.struts2.helloworld.Product" method="save">
<result name="details">/WEB-INF/pages/details.jsp</result>
</action>
</package>
</struts>
Product.java类和save方法:
package com.afy.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(){
System.out.println("save:" + this);
return "details";
}
}
details.jsp页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
ProductId: ${productId }
<br><br>
ProductName: ${productName }
<br><br>
ProductDesc: ${productDesc }
<br><br>
ProductPrice: ${productPrice }
<br><br>
</body>
</html>
重点代码块简析
这里主要简析一下struts.xml文件中的配置代码
<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.afy.struts2.helloworld.Product" method="save">
<result name="details">/WEB-INF/pages/details.jsp</result>
</action>
</package>
</struts>
先从< packege>标签及其里面的属性开始,
1.< packege>: 包,用于组织模块。一个项目会有多个模块,可以把每个模块分给一个包,一个struts.xml文件可以有多个< packege>标签;
2.< name>: 包的名字,必须的,以便其他包应用该包;
3.< extends>: 包的继承功能,指定当前包继承其他包,这样当前包便可以继承特定包所有的配置。通常情况要继承struts-default包,struts的核心拦截器都配置在struts-default包中,如果没有继承该包,所有请求都会请求不到。
4.< namespace>: 可选,若未指定则为默认值 / ;若指定其值,则调用包里Action时,就必须把所定义的命名空间值加到相关URI字符串里:http//localhost:8080/contextPath/namespace/actionName.action
再来看< package>之下的子标签< action>及其里面的属性标签
1.< action>: 一个struts2的请求就是一个action,一个< action>标签对应一个action类;
2.< name>: action的名字,即对应struts2请求的名字;
3.< class>:默认值为
com.opensymphony.xwork2.ActionSupport;
4.< method>: 可以指定执行action中某方法,默认值为execute
接着是< action>之下的子标签< result>及其属性标签
1.< result>: 结果,表示action方法可能返回的结果。一个action节点可能会有多个result子节点,多个result子节点由name区分;
2.< name>: result的标识名,对应action中< method>的具体方法返回值。默认是success。
3.< type>: 表示结果的类型,默认值为dispatcher(转发到结果)。