程序的结构
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
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: 包,struts2使用package来组织模块
name: 必须有。可以随便起名,用于被其他的包引用
extends:当前包继承哪个包,可以继承其中的所有配置,通常继承struts-default,
struts-default这个包在struts-default.xml文件中定义
namespace: 可选,如果没有给出,则以"/"为默认值,若namespace是一个非默认值,
则要想调用这个包里面的action,就必须把这个属性所定义的命名空间
添加到有关的URI字符串里面
http://localhost:8080/contextPath/namespace/actionName.action
假设namespace="/hihi"
则http://127.0.0.1:8090/struts2_2/product-save.action要变成
http://127.0.0.1:8090/struts2_2/hihi/product-save.action
-->
<package name="helloWorld" extends="struts-default" namespace="/">
<!--
配置一个action:一个struts2的请求就是一个action
name:对应一个struts2的请求的名字(或者对应servletPath,但去除/和扩展名),不包含扩展名
class的默认值为:com.opensymphony.xwork2.ActionSupport
method的默认值为:execute(返回值为字符串SUCCESS)
result:结果。
-->
<action name="product-input" class="com.opensymphony.xwork2.ActionSupport"
method="execute">
<!--
result: 结果,表示action 方法执行后可能返回的结果,所以一个action节点可能会有
多个result子节点,多个result子节点使用name的值来区分
name: 表示一个result,和action的返回值对应。由默认函数execute的返回值,可知result中
name的默认值为success
type: 表示结果的类型,默认值为dispatcher(转发到结果)
-->
<result name="success" type="dispatcher">/WEB-INF/pages/input.jsp</result>
</action>
<action name="product-save" class="wade.app.Product"
method="save">
<result name="details">/WEB-INF/pages/details.jsp</result>
</action>
</package>
</struts>
details.jsp
<body>
ProductName:${productId}<br/>
ProductName:${productName}<br/>
ProductName:${productDesc}<br/>
ProductName:${productPrice}<br/>
</body>
product.java
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 [productName=" + productName + ", productDesc="
+ productDesc + ", productPrice=" + productPrice + "]";
}
//加入了save方法
public String save(){
System.out.println("save:"+this.toString());
return "details";
}
与用自己定义的Filter做控制器的程序的比较:
1.需要搭建Struts2的开发环境
2.搭建Struts2的开发环境
3.details.jsp比之前简单
(requestScope.product.productName)−>
(productName)
4.由product-input.action转到//WEB-INF/pages/input.jsp的方法是在struts.xml中配置一个action
<action name="product-input">
<result>/WEB-INF/pages/input.jsp</result>
</action>
5.由input.jsp页面的action:product-save.action 到Product的save方法,再到/WEB-INF/pages/details.jsp
<action name="product-save" class="wade.app.Product" method="save">
<result name="details">/WEB-INF/pages/details.jsp</result>
</action>
在product中定义一个save方法,且返回值为details
一些默认值的来源都来自于一个struts-default.xml的文件
补充:
请求参数是如何被放到Person对象中去的?
Struts2的Param拦截器帮我们把请求参数赋给了值栈栈顶的bean对象,但这个对象相应的成员变量必须有set方法,否者赋值会失败
为什么${productId}可以得到对象中的值?
因为有确实存在这个属性,使用