第四节是在第三节搭配环境后做的小练习。
1.项目结构如下如,包括必须的包 (导入struts2.jar包,导入到lib目录下)
2..web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>Struts2-2</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 配置过滤器类 -->
<filter>
<filter-name>struts2</filter-name>
<!-- 从struts-2.1.3以后,org.apache.struts2.dispatcher.FileDispatcher值被标注为过时,现在是如下写法 -->
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<!-- 过滤器用来初始化struts2并处理所有web请求 -->
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- struts基本配置内容 -->
<!--
package:包,struts2使用package来组织模块。
package-name:必须,用于区别不同的package;必须是唯一的、可用的变量名;用于其它package来继承;
package-namespace:可选,如果它没有给出,则以/为默认值
若namespace有一个非默认值,则要想调用这个包里的Action,
就必选把这个属性所定义的命名空间添加到有关的URL字符串里
例:http:/localhost:8080/contextPath/namespace/actionName.action
package-extends:当前包继承哪个包,即可以继承其中的所有的配置。通常情况下继承struts-default
struts-default这个包在struts-default.xml文件中定义
-->
<package name="struts" namespace="" extends="struts-default">
<!--
配置一个action:一个struts2的请求就是一个action
action-name:用于在一个package里区别不同的action;必须是唯一的、可用的变量名;是调用action时输入路径的组成部分;对应一个struts2的请求的名字(或对一个servletPath,但去除/和扩展名)不包含扩展名
action-class:action所在的路径(包名+类名);默认值为:com.opensymphony.xwork2.ActionSupport,可不写
action-method:action所调用的方法名; 默认执行execute,所以execute可以不写
result:结果
-->
<action name="product-input" class="com.opensymphony.xwork2.ActionSupport" method="execute">
<!--
result:结果。表示action方法执行后可能返回的一个结果,所以一个action结点可能会有多个result子节点,
多个result子节点使用name来区分
name:标识一个result和action方法的返回值对应,默认值为success(可不写)
type:表示结果的类型。默认值为dispatcher(转发到结果)
-->
<result name="success" type="dispatcher">/WEB-INF/pages/input.jsp</result>
</action>
<action name="product-save" class="actionbao.Product" method="save">
<result name="details">/WEB-INF/pages/details.jsp</result>
</action>
</package> </struts>
4.index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="product-input.action">product input</a>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="product-save.action" method="post">
ProductName:<input type="text" name="productName"/>
<br/>
ProductDesc:<input type="text" name="productDesc"/>
<br/>
ProductPrice:<input type="text" name="productPrice"/>
<br/>
<input type="submit" value="submit"/>
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
ProductId:${productId}<br/>
ProductName:${productName}<br/>
ProductDesc:${productDesc}<br/>
ProductPrice:${productPrice}<br/>
</body>
</html>
7.Product.jave
package actionbao;
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 + "]";
}
public String save() {
System.out.println("save " + this);
return "details";
}
}
在Eclipse ee中运行index.jsp即可得出最后结果
思路步骤:
1.由product-input.action转到/WEB-INF/pages/input.jsp
在struts2中配置一个action
<action name="product-input" method="execute">
<result >/WEB-INF/pages/input.jsp</result>
</action>
2.由input.jsp页面的action:product-save.action到Product's save,再到/WEB-INF/pages/details.jsp
在struts2中配置一个action
<action name="product-save" class="actionbao/Product" method="save">
<result name="details"> /WEB-INF/pages/details.jsp</result>
</action>
3.在Product中定义一个save方法,且返回值为details