Struts2-配置struts.xml
来源:互联网 发布:json key 排序 编辑:程序博客网 时间:2024/05/23 19:19
前奏:
在我们使用struts时,很多时候是在编写struts.xml配置文件,里边很多的配置需要我们结合框架源代码进行详细了解
使用源代码为2.3.15
详解:
1:关于包的配置
<package name="helloworld" extends="struts-default"></package>
我们一般是继承struts-default,该配置文件在核心代码中有struts-default.xml,可自行查看元素详细说明:
package :包 struts2使用package来组织模块
name属性:必须,用于其他的包引用当前的包
exdends:当前包继承的包, 即可以继承其中所有的配置,通常情况下继承struts-default
namespace属性是可选的,如果它没有给出,则以"/"为默认值,若namespace有个
非默认值,则要想调用这个包里的action,就必须把这个属性,所定义的命名空间,添加到有关的uri字符串里
加入namespace是/cgc 则在servlet前边加上,http://cuigaochong-pc:8080/Web_Struts2_Test/cgc /product-input.action
2:关于action的配置
例如:<action name="product-input">
<result>/WEB-INF/pages/input.jsp</result>
</action>
配置一个action:一个struts2的请求就是一个action
name 对应一个struts2的请求的名字,不包含扩展名
result表示结果,result表示action方法执行后,可能返回的一个值,因此,一个action节点可能会有多个result子节点 多个result使用name来区分
name:标识一个result和action方法返回值对应.默认值为success
通过源代码中的ActionSupport类中的execute方法可以看出
代码如下:
public String execute() throws Exception
{
return SUCCESS;
}
如果没明确指出对应的class,通过查看struts-default.xml,我们可以看出默认配置的class是com.opensymphony.xwork2.ActionSupport
<default-class-ref class="com.opensymphony.xwork2.ActionSupport" />
ActionSupport 是默认的 Action 类: 若某个 action 节点没有配置 class 属性, 则 ActionSupport 即为
待执行的 Action 类. 而 execute 方法即为要默认执行的 action 方法
如果没有指明method的值,则其默认值为execute
type:标示结果类型,默认值为dispatcher(转发到结果);
从配置文件中我们可以查看到struts支持的发送方式:
<package name="struts-default" abstract="true">
<result-types>
<result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>
<result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/>
<result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/>
<result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/>
<result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/>
<result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>
<result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>
<result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/>
<result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/>
<result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" />
</result-types>
其中:dispatcher:转发,redirect:重定向,redirectAction:重定向到一个action,chain:转发到一个action
代码如下:
<action name="result-action" class="com.cgc.struts.test.ResultActionTest" method="execute">
<!-- 转发 -->
<result name="page0" type="dispatcher">/pages/page0.jsp</result>
<!-- 重定向 -->
<result name="page1" type="redirect">/pages/page1.jsp</result>
<!-- 重定向到一个action -->
<result name="page2" type="redirectAction">
<param name="actionName">TestAction</param>
<param name="namespace">/cgc</param>
</result>
<!-- 转发到一个aciont -->
<result name="page3" type="chain">
<param name="actionName">TestAction</param>
<param name="namespace">/cgc</param>
</result>
</action>
因此案例中的配置等同以下配置:
<action name="product-input" class="com.opensymphony.xwork2.ActionSupport" method="execute">
<result name="SUCCESS" type="dispatcher">/WEB-INF/pages/input.jsp</result>
</action>
3:include标签,<include file="example.xml"></include>,可以包含其他的struts配置文件到当前配置文件
4:配置Struts可以受理的请求的扩展名
源码中,在org.apache.struts2 包下的default.properties中配置了Sturts2应用的一些常量
其中默认扩展名配置为:struts.action.extension=action,,
可以在struts.xml中以常量配置的方式修改default.properties中配置的常量修改方式如下:
<constant name="struts.action.extension" value="action,do,"></constant>
5:通配符映射
一个 Web 应用可能有成百上千个 action 声明. 可以利用 struts 提供的通配符映射机制把多个彼此相似的映射关系简化为一个映射关系
通配符映射规则
若找到多个匹配, 没有通配符的那个将胜出
若指定的动作不存在, Struts 将会尝试把这个 URI 与任何一个包含着通配符 * 的动作名及进行匹配
被通配符匹配到的 URI 字符串的子串可以用 {1}, {2} 来引用. {1} 匹配第一个子串, {2} 匹配第二个子串…
{0} 匹配整个 URI
若 Struts 找到的带有通配符的匹配不止一个, 则按先后顺序进行匹配
* 可以匹配零个或多个字符, 但不包括 / 字符. 如果想把 / 字符包括在内, 需要使用 **. 如果需要对某个字符进行转义, 需要使用 \.
案例:
<action name="emp-*"
class="com.atguigu.struts2.app.EmployeeAction"
method="{1}">
<result name="{1}">/emp-{1}.jsp</result>
<result name="success" type="redirectAction">emp-list</result>
</action>
在我们使用struts时,很多时候是在编写struts.xml配置文件,里边很多的配置需要我们结合框架源代码进行详细了解
使用源代码为2.3.15
详解:
1:关于包的配置
<package name="helloworld" extends="struts-default"></package>
我们一般是继承struts-default,该配置文件在核心代码中有struts-default.xml,可自行查看元素详细说明:
package :包 struts2使用package来组织模块
name属性:必须,用于其他的包引用当前的包
exdends:当前包继承的包, 即可以继承其中所有的配置,通常情况下继承struts-default
namespace属性是可选的,如果它没有给出,则以"/"为默认值,若namespace有个
非默认值,则要想调用这个包里的action,就必须把这个属性,所定义的命名空间,添加到有关的uri字符串里
加入namespace是/cgc 则在servlet前边加上,http://cuigaochong-pc:8080/Web_Struts2_Test/cgc /product-input.action
2:关于action的配置
例如:<action name="product-input">
<result>/WEB-INF/pages/input.jsp</result>
</action>
配置一个action:一个struts2的请求就是一个action
name 对应一个struts2的请求的名字,不包含扩展名
result表示结果,result表示action方法执行后,可能返回的一个值,因此,一个action节点可能会有多个result子节点 多个result使用name来区分
name:标识一个result和action方法返回值对应.默认值为success
通过源代码中的ActionSupport类中的execute方法可以看出
代码如下:
public String execute() throws Exception
{
return SUCCESS;
}
如果没明确指出对应的class,通过查看struts-default.xml,我们可以看出默认配置的class是com.opensymphony.xwork2.ActionSupport
<default-class-ref class="com.opensymphony.xwork2.ActionSupport" />
ActionSupport 是默认的 Action 类: 若某个 action 节点没有配置 class 属性, 则 ActionSupport 即为
待执行的 Action 类. 而 execute 方法即为要默认执行的 action 方法
如果没有指明method的值,则其默认值为execute
type:标示结果类型,默认值为dispatcher(转发到结果);
从配置文件中我们可以查看到struts支持的发送方式:
<package name="struts-default" abstract="true">
<result-types>
<result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>
<result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/>
<result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/>
<result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/>
<result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/>
<result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>
<result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>
<result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/>
<result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/>
<result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" />
</result-types>
其中:dispatcher:转发,redirect:重定向,redirectAction:重定向到一个action,chain:转发到一个action
代码如下:
<action name="result-action" class="com.cgc.struts.test.ResultActionTest" method="execute">
<!-- 转发 -->
<result name="page0" type="dispatcher">/pages/page0.jsp</result>
<!-- 重定向 -->
<result name="page1" type="redirect">/pages/page1.jsp</result>
<!-- 重定向到一个action -->
<result name="page2" type="redirectAction">
<param name="actionName">TestAction</param>
<param name="namespace">/cgc</param>
</result>
<!-- 转发到一个aciont -->
<result name="page3" type="chain">
<param name="actionName">TestAction</param>
<param name="namespace">/cgc</param>
</result>
</action>
因此案例中的配置等同以下配置:
<action name="product-input" class="com.opensymphony.xwork2.ActionSupport" method="execute">
<result name="SUCCESS" type="dispatcher">/WEB-INF/pages/input.jsp</result>
</action>
3:include标签,<include file="example.xml"></include>,可以包含其他的struts配置文件到当前配置文件
4:配置Struts可以受理的请求的扩展名
源码中,在org.apache.struts2 包下的default.properties中配置了Sturts2应用的一些常量
其中默认扩展名配置为:struts.action.extension=action,,
可以在struts.xml中以常量配置的方式修改default.properties中配置的常量修改方式如下:
<constant name="struts.action.extension" value="action,do,"></constant>
5:通配符映射
一个 Web 应用可能有成百上千个 action 声明. 可以利用 struts 提供的通配符映射机制把多个彼此相似的映射关系简化为一个映射关系
通配符映射规则
若找到多个匹配, 没有通配符的那个将胜出
若指定的动作不存在, Struts 将会尝试把这个 URI 与任何一个包含着通配符 * 的动作名及进行匹配
被通配符匹配到的 URI 字符串的子串可以用 {1}, {2} 来引用. {1} 匹配第一个子串, {2} 匹配第二个子串…
{0} 匹配整个 URI
若 Struts 找到的带有通配符的匹配不止一个, 则按先后顺序进行匹配
* 可以匹配零个或多个字符, 但不包括 / 字符. 如果想把 / 字符包括在内, 需要使用 **. 如果需要对某个字符进行转义, 需要使用 \.
案例:
<action name="emp-*"
class="com.atguigu.struts2.app.EmployeeAction"
method="{1}">
<result name="{1}">/emp-{1}.jsp</result>
<result name="success" type="redirectAction">emp-list</result>
</action>
0 0
- struts2 struts.xml配置
- Struts2-配置struts.xml
- struts2 的struts.xml配置
- struts2 STRUTS.XML 配置错误
- Struts2如何配置struts.xml
- struts2入门,struts.xml配置
- Struts2 -- struts.xml配置总结
- Struts2基础---struts.xml配置
- struts2的struts.xml配置
- Struts2配置struts.xml文件
- struts2 配置web.xml struts.xml
- Struts2:struts.xml常用配置解析
- Struts2:struts.xml常用配置解析收藏
- Struts2:struts.xml常用配置解析
- struts2 中 struts.xml文件属性配置
- 简单的struts2.0配置struts.xml
- Struts2:struts.xml常用配置解析
- Struts2:struts.xml常用配置解析2
- CGCONTEXTADDCURVETOPOINT 的深入理解
- 四川大学图书馆数据库
- 【黑马程序员】【OC语言】self关键字
- yii2 刷新页面
- spring读写分离 - 事务配置篇(转)
- Struts2-配置struts.xml
- Max Points on a Line
- markdown编辑器使用
- android 的 Testing Support Library 测试支持包(库)
- TCP和UDP
- iOS_开发_工具_mac下加密文件
- Swift学习 类、属性、方法的介绍
- 【特种兵PPT教程】如何巧用SmartArt设计自己想要的图表?
- 【黑马程序员】【OC语言】继承