我是靠谱客的博主 冷艳小馒头,这篇文章主要介绍Struts2配置详解,现在分享给大家,希望可以做个参考。

1.Namespace

       1)namespace决定action的访问路径,默认为“”,可以接受所有路径的Action;
       2)namespace可以写为/,或者/xxx,或者/xxx/yyy,对应action访问路径为/index.action,/xxx/index.action或者/xxx/yyy/index.action
       3)namespace最好也用模块来进行命名

  当url中输入不存在的Action的时候,定向到默认的Action

1.1 struts2配置详解

1.核心控制器

   需要在web.xml中进行配置

   对框架进行初始化,以及处理所有的请求

2.Action

开发Action

实现Action接口

继承ActionSupport类

配置Action

 

Action的作用

封装工作单元

数据转移的场所

返回结果字符串

 

3.Result

实现对结果的调用

result元素的值指定对应的实际资源位置

name属性表示result逻辑名

4.struts.xml

01.核心配置文件,主要负责管理Action,通常放在WEB-INF/classes目录下,在该目录下的struts.xml文件可以被自动加载

02.constant元素

配置常量,可以改变Struts 2框架的一些行为,name属性表示常量名称,value属性表示常量值

03.package元素

包的作用:简化维护工作,提高重用性,包可以“继承”已定义的包,并可以添加自己包的配置

name属性为必需的且唯一,用于指定包的名称

extends属性指定要扩展的包

namespace属性定义该包中action的命名空间 ,为可选属性

 

2.动态方法调用

 DMI Dynamic Method Invoke(动态方法调用)

解决问题:

      减少了配置文件中Action节点个数,<Result>节点

案例:

DimAction:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
1 public class DmiAction extends ActionSupport{ 2 public String del(){ 3 return "del"; 4 } 5 public String list(){ 6 System.out.println("del"); 7 return "list"; 8 } 9 }

struts.xml:

复制代码
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
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 6 <struts> 7 <!--修改struts.xml文件,无需重启tomcat--> 8 <constant name="struts.devMode" value="true"></constant> 9 <!--动态方法调用开启 默认开启--> 10 <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant> 11 12 <package name="day03" namespace="/" extends="struts-default"> 13 <!--访问规则 http://localhost:8080/dmiAction!del--> 14 <action name="dmiAction" class="cn.happy.day03dmi.DmiAction"> 15 <result name="list">/day02/list.jsp</result> 16 <result name="del">/day02/del.jsp</result> 17 </action> 18 </package> 19 </struts>

访问规则:

3.通配符

类似于正则:正则分为:匹配和限定所用

 

通配符使用案例

PatternAction

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
1 public class PatternAction extends ActionSupport { 2 public String del(){ 3 return "del"; 4 } 5 public String list(){ 6 return "list"; 7 } 8 }

struts.xml:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 6 <struts> 7 <constant name="struts.devMode" value="true"></constant> 8 9 <package name="day04" namespace="/" extends="struts-default"> 10 <action name="*_*" class="cn.happy.day04pattern.{1}" method="{2}"> 11 <result name="{2}">/day02/{2}.jsp</result> 12 </action> 13 </package> 14 </struts>

访问规则:

4.ResultType

chain:转发到Action

dispatcher:默认值:转发到jsp页面

redirect:重定向到页面

redirectAction:重定向到Action

 案例:返回类型为chain

复制代码
1
ResultAction
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
1 public class ResultAction { 2 public String list(){ 3 Map<String,Object> map=(Map<String,Object>)ActionContext.getContext().get("request"); 4 map.put("msg","lalalala~~~~~~~~~~~~~`"); 5 System.out.println("list================="); 6 return "list"; 7 } 8 public String add(){ 9 System.out.println("add====================="); 10 return "add"; 11 } 12 }

struts.xml

复制代码
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
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 6 <struts> 7 <!--修改struts.xml文件不用重启tomcat--> 8 <constant name="struts.devMode" value="true"></constant> 9 10 <package name="day05" namespace="/" extends="struts-default"> 11 <action name="resultActionList" class="cn.happy.day05result.ResultAction" method="list"> 12 <result name="list">/day02/list.jsp</result> 13 </action> 14 15 <action name="resultActionAdd" class="cn.happy.day05result.ResultAction" method="add"> 16 <result name="add" type="chain"> 17 <param name="actionName">resultActionList</param> 18 <param name="namespace">/</param> 19 </result> 20 </action> 21 </package> 22 </struts>

 

 5.默认Action

 

效果:

6.全局结果集

复制代码
1
GlobalAction
复制代码
1
2
3
4
5
6
7
8
1 public class GlobalAction extends ActionSupport { 2 public String list(){ 3 return "error"; 4 } 5 }
复制代码
1
GlobalResultTwo
复制代码
1
2
3
4
5
6
7
8
1 public class GlobalResultTwo extends ActionSupport { 2 public String list(){ 3 return "error"; 4 } 5 }

 

 

struts.xml:

复制代码
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
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 <struts> 6 <constant name="struts.devMode" value="true" /> 7 <package name="day06" namespace="/" extends="struts-default"> 8 <!--全局结果集,当多个action有相同的结果的时候,可以提取到这里,进行复用--> 9 <global-results> 10 <result name="error">/error.jsp</result> 11 </global-results> 12 13 <action name="globalAction" class="cn.happy.day06globalresult.GlobalAction" method="list"> 14 <result name="list">/day02/list.jsp</result> 15 </action> 16 <action name="globalActionTwo" class="cn.happy.day06globalresult.GlobalResultTwo" method="list"> 17 <result name="list">/day02/list.jsp</result> 18 </action> 19 </package> 20 21 </struts>

 

效果:

 

转载于:https://www.cnblogs.com/liutao1122/p/8472527.html

最后

以上就是冷艳小馒头最近收集整理的关于Struts2配置详解的全部内容,更多相关Struts2配置详解内容请搜索靠谱客的其他文章。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部