我是靠谱客的博主 美满冷风,这篇文章主要介绍Struts2(二)struts.xml详解,现在分享给大家,希望可以做个参考。

1 struts.xml 文件

我们已经知道Struts2 作为控制器(Controller)来建立模型与视图的数据交互。采用拦截器的机制来处理用户的请求。其实它是通过struts.xml 这个中间文件来实现的。

2 struts.xml 文件的书写

① 新建一个web项目,目录结构如图所示,在index.jsp 中有一个input.action 请求:

② 在struts.xml 中应答input.action 请求:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?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包: 可以在Web App Libraries的 struts2-core-2.x.x.jar中的 struts-default.xml 看到. namespace: 可选,若没有给出默认值为: / http://localhost:8080/contextPath/namespace/actionName.action --> <package name="helloword" extends="struts-default" namespace="/"> <!-- 配置一个action: 一个struts2 的请求就是一个action name: 对应一个struts2 的请求的名字去除扩展名(也可以说是对应一个servletPath,去除/和扩展名) class: 默认值为 com.opensymphony.xwork2.ActionSupport method: 默认值为 excute result: 结果(响应) name: 标识一个result,默认值为success type:结果类型,默认值 dispatcher(转发) --> <!-- <action name="input" class="com.opensymphony.xwork2.ActionSupport" method="excute"> <result name="success" type="dispatcher">/WEB-INF/pages/input.jsp</result> </action> --> <action name="input"> <result>/WEB-INF/pages/input.jsp</result> </action> </package> </struts>
③ 响应请求,启动tomcat,运行index.xml,点击链接(发送请求),页面就会跳转到 WEB-INF/pages/input.jsp 页面。

3 action 与Action 类

① 在struts.xml 中新增一个action
复制代码
1
2
3
<action name="save" class="com.test.Product" method="save"> <result name="details">/WEB-INF/pages/details.jsp</result> </action>
② input.jsp
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<%@ 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="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>
③ Product类
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.test; 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"; } public Product() { System.out.println("Product's constructor..."); } }
④ detail.jsp 
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<%@ 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> ProductName: ${requestScope.product.productName} //没有值 <br><br> ProductName: ^<%= request.getAttribute("productName") %> <br><br> ProductDesc: ${productDesc } <br><br> ProductPrice: ${productPrice } <br><br> <%= request %> //org.apache.struts2.dispatcher.StrutsRequestWrapper@2a242f84 </body> </html>

⑤ 在上面 Product 类 和ActionSupport 类(默认的 Action 类: 若某个 action 节点没有配置 class 属性, 则 ActionSupport 即为
待执行的 Action 类. 而 execute 方法即为要默认执行的 action 方法)都是Action 类.

⑥ action 与Action 类的区别:

1). action: 代表一个  Struts2 的请求. 

2). Action 类: 能够处理 Struts2 请求的类. 

> 属性的名字必须遵守与 JavaBeans 属性名相同的命名规则. 
   属性的类型可以是任意类型. 从字符串到非字符串(基本数据类型)之间的数据转换可以自动发生

> 必须有一个不带参的构造器: 通过反射创建实例 

> 至少有一个供 struts 在执行这个 action 时调用的方法

> 同一个 Action 类可以包含多个 action 方法. 

> Struts2 会为每一个 HTTP 请求创建一个新的 Action 实例, 即 Action 不是单例的, 是线程安全的. 




最后

以上就是美满冷风最近收集整理的关于Struts2(二)struts.xml详解的全部内容,更多相关Struts2(二)struts内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(57)

评论列表共有 0 条评论

立即
投稿
返回
顶部