本文为原创,如需转载,请注明作者和出处,谢谢!
在本文中将详细讲述struts.xml文件的常用配置及注意事项。
1.
使用<include>标签重用配置文件
在Struts2中提供
了一个默认的struts.xml文件,但如果package、action、interceptors等配置比较多时,都放到一个struts.xml
文件不太容易维护。因此,就需要将struts.xml文件分成多个配置文件,然后在struts.xml文件中使用<include>标签
引用这些配置文件。这样做的优点如下:
结构更清晰,更容易维护配置信息。
配置文件可以复用。如果在多个Web程序中都使用类似或相同的配置文件,那么可以使用<include>标签来引用这些配置文件,这样可以减少工作量。
假设有一个配置文件,文件名为newstruts.xml,代码如下:
<!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEstrutsPUBLIC
"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<packagename="demo"extends="struts-default">
<actionname="submit"class="action.MoreSubmitAction">
<resultname="save">
/result.jsp
</result>
<resultname="print">
/result.jsp
</result>
</action>
</package>
</struts>
则struts.xml引用newstruts.xml文件的代码如下:
<!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
-->
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEstrutsPUBLIC
"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<includefile="newstruts.xml"/>
<packagename="test"extends="struts-default">
</package>
</struts>
大家要注意一下,用<include>引用的xml文件也必须是完成的struts2的配置。实际上<include>在引用时是单独解析的xml文件,而不是将被引用的文件插入到struts.xml文件中。
2.
action的别名
在默认情况下,Struts2会调用动作类的execute方法。但有些时候,我们需要在一个动作类中处理不同的动作。也就是用户请求不同的动作时,执行动作类中的不同的方法。为了达到这个目的,可以在<action>标签中通过method方法指定要指行的动作类的方法名,并且需要为不同的动作起不同的名子(也称为别名)。如下面代码所示:
上面代码的两个动作的class属性都指向同一个类,name为这个类起了两个动作别名:test和my。在动作my中,使用了method属性指定要要运行的方法名为my。
在MyAction类中必须要有my方法,代码如下:
除了在struts.xml中配置别名,还可以通过请求参数来描述指定动作(并不需要在struts.xml中配置)。请求参数的格式如下:
http://localhost:8080/contextPath/actionName!method.action
关于通过请求指定动作的详细内容,请参阅笔者写的《Struts2教程2:处理一个form多个submit》。
3.
为action指定参数
在struts2中还可以为action指定一个或多个参数。大家还记着struts1.x是如何设置的action参数不? 在struts1.x中可以使用<action>标签的parameter属性为其指定一个action参数,如果要指定多个,就只能通过逗号(,)或其他的分隔符将不同的参数隔开。而在struts2中可以通过<param>标签指定任意多个参数。代码如下:
<!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
-->
<actionname="submit"class="action.MyAction">
<paramname="param1">value1</param>
<paramname="param2">value2</param>
<resultname="save">
/result.jsp
</result>
</action>
当然,在action中读这些参数也非常简单,只需要象获取请求参数一样在action类中定义相应的setter方法即可(一般不用定义getter方法)。如下面的代码将读取param1和param2参数的值:
<!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
-->
packageaction;
importcom.opensymphony.xwork2.ActionSupport;
publicclassMyActionextendsActionSupport
{
privateStringparam1;
privateStringparam2;
publicStringexecute()throwsException
{
System.out.println(param1+param2);
}
publicvoidsetParam1(Stringparam1)
{
this.param1=param1;
}
publicvoidsetParam2(Stringparam2)
{
this.param2=param2;
}
}
当struts2在调用execute之前,param1和param2的值就已经是相应参数的值了,因此,在execute方法中可以直接使用param1和param2。
4.
选择result类型
在默认时,<result>标签的type属性值是“dispatcher”(实际上就是转发,forward)。开发人员可以根据自己的需要指定不同的类型,如redirect、stream等。如下面代码所示:
<result name="save"type="redirect">
/result.jsp
</result>
这此result-type可以在struts2-core-2.0.11.1.jar包或struts2源代码中的struts-default.xml文件中找到,在这个文件中找到<result-types>标签,所有的result-type都在里面定义了。代码如下:
<!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><result-types>
<result-typename="chain"class="com.opensymphony.xwork2.ActionChainResult"/>
<result-typename="dispatcher"class="org.apache.struts2.dispatcher.ServletDispatcherResult"default="true"/>
<result-typename="freemarker"class="org.apache.struts2.views.freemarker.FreemarkerResult"/>
<result-typename="httpheader"class="org.apache.struts2.dispatcher.HttpHeaderResult"/>
<result-typename="redirect"class="org.apache.struts2.dispatcher.ServletRedirectResult"/>
<result-typename="redirectAction"class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>
<result-typename="stream"class="org.apache.struts2.dispatcher.StreamResult"/>
<result-typename="velocity"class="org.apache.struts2.dispatcher.VelocityResult"/>
<result-typename="xslt"class="org.apache.struts2.views.xslt.XSLTResult"/>
<result-typename="plainText"class="org.apache.struts2.dispatcher.PlainTextResult"/>
<!--DeprecatednameformscheduledforremovalinStruts2.1.0.ThecamelCaseversionsarepreferred.Seeww-1707-->
<result-typename="redirect-action"class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>
<result-typename="plaintext"class="org.apache.struts2.dispatcher.PlainTextResult"/>
</result-types>
5.
全局result
有很多时候一个<result>初很多<action>使用,这时可以使用<global-results>标签来定义全局的<result>,代码如下:
如果<action>中没有相应的<result>,Struts2就会使用全局的<result>。
分享到:
相关推荐
struts.xml常用配置解析 网上转载
struts.xml的错误解决办法,解决错误
Dom4j解析struts2框架的struts.xml,自定义struts框架需要解析配置文件
dom4j解析struts.xml需要,上一次上传忘了这个包装类
专为multipart请求信息使用的org.apache.struts2.dispatcher.multipart.MultiPartRequest解析器接口(文件上传用) struts.multipart.saveDir The directory to use for storing uploaded files 设置存储上传...
NULL 博文链接:https://chaoyi.iteye.com/blog/2156728
struts2学习教程包括:第一个Struts2程序,处理一个form多个submit,struts.xml常用配置解析,使用validate方法验证数据,使用Validation框架验证数据,在Action类中获得HttpServletResponse对象的四种方法,上传...
配置常量,可以改变Struts 2框架的一些行为 name属性表示常量名称,value属性表示常量值 package元素: 包的作用:简化维护工作,提高重用性 包可以“继承”已定义的包,并可以添加自己包的配置 name属性为必须去且...
2 将struts2 整合进去, 这次在struts.xml中我们使用通配符的方式配置action。 a 加入支持 : 添加struts2.3.15 必需包 以及 struts json包(ajax要用到),spring整合struts2包,spring web 包,在src目录下建立...
主要介绍了struts2 validation.xml 验证规则代码解析,具有一定借鉴价值,需要的朋友可以参考下
4.dom4j-1.6.1.jar(对dom4j的封装,是解析xml文件的) 5.javassist-3.9.0.GA.jar(一个开源的分析、编辑和创建Java字节码的类库) 6.jta-1.1.jar(hibernate对事务的处理) 7.slf4j-api-1.6.4.jar(一个日志系统的...
11.4.7 编写Struts的配置文件struts-config.xml 11.4.8 编写Spring的配置文件spring-config.xml 11.4.9 配置web.xml 11.4.10 启动Tomcat运行示例 11.5 小结 第十二章 使用Hibernate快速实现持久层处理 12.1 ...
11.4.7 编写Struts的配置文件struts-config.xml 11.4.8 编写Spring的配置文件spring-config.xml 11.4.9 配置web.xml 11.4.10 启动Tomcat运行示例 11.5 小结 第十二章 使用Hibernate快速实现持久层处理 12.1 ...
11.4.7 编写Struts的配置文件struts-config.xml 11.4.8 编写Spring的配置文件spring-config.xml 11.4.9 配置web.xml 11.4.10 启动Tomcat运行示例 11.5 小结 第十二章 使用Hibernate快速实现持久层处理 12.1 ...
[Android访问Struts2服务器程序]我发现在网上下载这个源码很困难,我一步步配置成功了,还将具体应该注意的事项都列出来了,很详细,能够运行成功! Android+struts2+JSON形式的手机开发 把本机当作服务器,将Android...
SSH(struts+spring+hibernate)的jar包 SSH 通常指的是 Struts2 做前端控制器,Spring ...配置文件加载顺序为:default.properties -> struts-default.xml -> struts-plugins.xml -> struts.xml -> struts.locale。
代码一键自动生成工具 可生成Action、JavaBean实体类、Dao及实现类、service及实现类、spring.xml、struts.xml、mybatis.xml *该工具目前支持3种数据源的生成方式,分别是:JDBC、.table、PDM *JDBC:选择JDBC是只...
可以支持Ajax用的 xml数据结构,也可以支持jsp、jstl、struts等标签,根据配置不同的数据解析器,得到不同结构的数据。 3.集成完整的分页功能。 4.查询页面的查询项和列表项都是动态的,通过xml配置,统一管理。...
第五章 XML解析...................119 Oracle学习笔记...............121 前言....................................121 第一章 Selecting Rows.....................124 第二章 Limiting Selected Rows.......127...
第五章 XML解析.119 Oracle学习笔记...............121 前言....121 第一章 Selecting Rows.....................124 第二章 Limiting Selected Rows.......127 第三章 Single Row Functions..........127 第四章 ...