一、下载和搭建Struts2环境
1、 下载地址:http://struts.apache.org/,该版本为struts-2.3.16.3
2、 搭建Struts2环境
a) 加入jar包:复制struts-2.3.16.3\apps\struts2-blank\WEB-INF\lib 下的包到项目WEB-INF\lib 文件夹下,struts2-blank是由strtus2-blank.war 解压出来的文件夹。包文件列表如下图:
b) 在 web.xml 中配置 struts2:将struts2-blank\WEB-INF\web.xml 文件中关于 filter 的配置复制过来到自己项目的 WEB-INF\web.xml 文件中。完整的web.xml代码如下:
<?xmlversion="1.0"encoding="UTF-8"?> <web-appxmlns: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>struts2Test</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list>
<!-- 配置struts2的Filter --> <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.html</welcome-file> </welcome-file-list> </web-app> |
c) 在当前项目的 src 根目录下添加 struts.xml 文件:将struts-2.3.16.3\apps\struts2-blank\WEB-INF\classes下的struts.xml 文件复制到src 根目录下,然后只保留<struts> 根节点,其他节点内容全部删除。
d) 添加DTD约束(Struts.xml 文件中节点自动提示设置):
复制http://struts.apache.org/dtds/struts-2.3.dtd这句代码
选择struts-2.3.16.3\src\core\src\main\resources\struts-2.3.dt,重新打开struts.xml文件就会出现提示标签。
二、HelloWorld
1、 在 WebContent 目录下创建index.jsp 在index.jsp 中加入超链接
<a href="product-input.action">Product Input</a></html> |
2、 配置 struts.xml文件
<?xmlversion="1.0"encoding="UTF-8"?> <!DOCTYPEstrutsPUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 配置struts2可以响应的扩展名 --> <constantname="struts.action.extension"value="action,do,"></constant>
<!-- struts的请求配置需要放到package下, package:struts2使用package来组织模块 name:包名,用于其他包来继承当前包 extends:当前继承哪个包,通常继承 struts-default包 --> <packagename="helloWorld"extends="struts-default">
<!-- action:一个struts2的请求就是一个action name:struts请求的名字(XXX.action,不包含扩展名) result:请求返回的处理结果 --> <actionname="product-input"> <result>/WEB-INF/helloWorld.jsp</result> </action>
</package>
</struts> |
3、 helloWorld 页面代码如下:
<%@ page language="java"contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPEhtmlPUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <metahttp-equiv="Content-Type"content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body>
Hello World!
</body> </html> |
三、Action HelloWorld
1、 action VS Action
a) action:代表一个struts2 的请求
b) Action类:能处理Struts2 请求的类
i. Action类中属性的命名必须遵循与JavaBean属性命名相同的命名规则
ii. Action类必须带有一个无参的构造方法,通过反射构建对象
iii. 同一个Action 类可以包含多个action 方法
iv. Struts2 会为每一个HTTP 请求创建一个新的Action实例,即Action 不是单例的,是线程安全的。
2、 在 WebContent 目录下创建 pages 文件夹,在 pages 文件夹中创建input.jsp 和 detail.jsp 文件
Input.jsp 中 body 部分代码如下:
<body> <formaction="input.action"method="post"> ProductName:<inputtype="text"name="productName"/><br/><br/> ProductDesc:<inputtype="text"name="productDesc"/><br/><br/> ProductPrice:<inputtype="text"name="productPrice"/><br/><br/> <inputtype="submit"name="submit"value="submit"/> </form> </body> |
detail.jsp 中 body 部分代码如下:
<body> ProductName:${productName }<br/><br/> productDesc:${productDesc }<br/><br/> productPrice:${productPrice } </body> |
3、 配置 struts.xml 文件
<struts> <packagename="helloWorld"extends="struts-default"> <actionname="product-input"> <result>/WEB-INF/pages/input.jsp</result> </action>
<actionname="input"class="helloworld.Product"method="save"> <resultname="detail">/WEB-INF/pages/detail.jsp</result> </action> </package> </struts> </struts> |
4、 Product 文件代码如下:
package helloworld;
public class Product {
private StringproductID; private StringproductName; private StringproductDesc; private StringproductPrice;
public String getProductID() { returnproductID; }
public void setProductID(String productID) { this.productID = productID; }
public String getProductName() { returnproductName; }
public void setProductName(String productName) { this.productName = productName; }
public String getProductDesc() { returnproductDesc; }
public void setProductDesc(String productDesc) { this.productDesc = productDesc; }
public String getProductPrice() { returnproductPrice; }
public void setProductPrice(String productPrice) { this.productPrice = productPrice; }
@Override public String toString() { return"Product [productID=" +productID +", productName=" +productName +", productDesc=" +productDesc + ", productPrice=" +productPrice +"]"; }
public String save() { System.out.println("product:" +this); return"detail"; } }
|