- 浏览: 132960 次
- 性别:
- 来自: 重庆
文章分类
最新评论
-
酱油男js:
不错...学习了。
oracle sql 优化大全 -
浪子秋水:
网页编码就是那点事 -
shenghuorulan:
通俗易懂。好文章,直接想到的问题都提到了。
oracle 全文检索实践 -
飞儿9530:
lucene 3.0.1 学习
来源地址:http://www.blogjava.net/nokiaguy/archive/2008/04/16/193457.html
在本文中将详细讲述 struts.xml文件的常用配置及注意事项。
1. 使用 <include> 标签重用配置文件
在Struts2中提供
了一个默认的struts.xml文件,但如果package、action、interceptors等配置比较多时,都放到一个struts.xml
文件不太容易维护。因此,就需要将struts.xml文件分成多个配置文件,然后在struts.xml文件中使用<include>标签
引用这些配置文件。这样做的优点如下:
结构更清晰,更容易维护配置信息。
配置文件可以复用。如果在多个 Web程序中都使用类似或相同的配置文件,那么可以使用 <include>标签来引用这些配置文件,这样可以减少工作量。
假设有一个配置文件,文件名为 newstruts.xml,代码如下:
<! DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd" >
< struts >
< package name ="demo" extends ="struts-default" >
< action name ="submit" class ="action.MoreSubmitAction" >
< result name ="save" >
/result.jsp
</ result >
< result name ="print" >
/result.jsp
</ result >
</ action >
</ package >
</ struts >
则
struts.xml
引用
newstruts.xml
文件的代码如下:
<! DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd" >
< struts >
< include file ="newstruts.xml" />
< package name ="test" extends ="struts-default" >
</ package >
</ struts >
大家要注意一下,用 <include>引用的 xml文件也必须是完成的 struts2的配置。实际上 <include>在引用时是单独解析的 xml文件,而不是将被引用的文件插入到 struts.xml文件中。
2. action 的别名
在默认情况下,
Struts2
会调用动作类的
execute
方法。但有些时候,我们需要在一个动作类中处理不同的动作。也就是用户请求不同的动作时,执行动作类中的不同的方法。为了达到这个目的,可以在
<action>
标签中通过
method
方法指定要指行的动作类的方法名,并且需要为不同的动作起不同的名子(也称为别名)。如下面代码所示:
<! DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd" >
< struts >
< package name ="demo" extends ="struts-default" >
< action name ="test" class ="action.MyAction" >
</ action >
< action name ="my" class ="action. MyAction" method ="my" >
</ action >
</ package >
</ struts >
上面代码的两个动作的 class属性都指向同一个类, name为这个类起了两个动作别名: test和 my。在动作 my中,使用了 method属性指定要要运行的方法名为 my。
在
MyAction
类中必须要有
my
方法,代码如下:
import com.opensymphony.xwork2.ActionSupport;
public class MyAction extends ActionSupport
{
public String execute() throws Exception
{
// 处理test动作的代码
}
public String my() throws Exception
{
// 处理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>标签指定任意多个参数。代码如下:
< param name ="param1" > value1 </ param >
< param name ="param2" > value2 </ param >
< result name ="save" >
/result.jsp
</ result >
</ action >
当然,在
action
中读这些参数也非常简单,只需要象获取请求参数一样在
action
类中定义相应的
setter
方法即可(一般不用定义
getter
方法)。如下面的代码将读取
param1
和
param2
参数的值:
import com.opensymphony.xwork2.ActionSupport;
public class MyAction extends ActionSupport
{
private String param1;
private String param2;
public String execute() throws Exception
{
System.out.println(param1 + param2);
}
public void setParam1(String param1)
{
this .param1 = param1;
}
public void setParam2(String param2)
{
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都在里面定义了。代码如下:
< 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" />
<!-- Deprecated name form scheduled for removal in Struts 2.1.0. The camelCase versions are preferred. See ww-1707 -->
< result-type name ="redirect-action" class ="org.apache.struts2.dispatcher.ServletActionRedirectResult" />
< result-type name ="plaintext" class ="org.apache.struts2.dispatcher.PlainTextResult" />
</ result-types >
5. 全局 result
有很多时候一个 <result>初很多 <action>使用,这时可以使用 <global-results>标签来定义全局的 <result>,代码如下:
< package name ="demo" extends ="struts-default" >
< global-results >
< result name ="print" > /result.jsp </ result >
</ global-results >
< action name ="submit" class ="action.MoreSubmitAction" >
</ action >
< action name ="my" class ="action.MoreSubmitAction" method ="my" >
</ action >
</ package >
</ struts >
如果
<action>
中没有相应的
<result>
,
Struts2
就会使用全局的
<result>。
发表评论
-
linux重启oracle 各种方法
2013-01-15 09:53 1280来自 : http://blog.sina.com.cn/s/ ... -
SVN服务器配置
2013-01-10 22:49 1224来自:http://air-blue55.blog.163.c ... -
网页编码就是那点事
2012-11-24 16:10 939来自 : http://www.kuqin.com/webpa ... -
Tomcat 配置 CAS 的一个问题。unable to find valid certification path to requested target
2012-11-11 21:40 1534来自: http://blog.csdn.net/cool_ ... -
Tomcat SSL配置大全
2012-11-11 21:02 1211来源: http://www.iteye.com/topi ... -
Html中td自动换行问题
2012-10-25 17:57 1014来自:http://blog.csdn.net/dududu0 ... -
oracle 全角、半角转换,和oracle 的 DUMP函数
2012-06-18 09:56 2082to_single_byte(c)转换成半角 to_multi ... -
tomcat sso 配置
2012-05-29 23:18 1635源: http://www.oecp.cn/hi/single ... -
Hibernate 利用配置文件配置HQL/SQL
2012-05-21 17:14 1803Query.hbm.xml <?xml version= ... -
CAS工作流程
2012-05-19 22:25 1579CAS3.0的工作流程: 0.app将用户转发到CAS处, 并 ... -
jforum 表目录
2012-04-18 18:37 2940目 录 1、附件关系表JFORUM_ATTACH 2 2、附件 ... -
Jfroum集成cas单点登录
2012-04-17 21:35 15181.安装CAS 下载cas server ,设置好服务器端, ... -
JForum登录用例
2012-04-15 22:59 60661,键入用户名、密码,单击“登入”按钮,登录请求发送到/JFo ... -
tinyMCE 详细文档
2012-03-28 10:57 2123http://www.iwms.net/n2065c17.as ... -
JBPM3.1.4 相关表的 意义
2012-03-18 22:54 1000JBPM_ACTION action记录表 JBPM_DEC ... -
JBPM环境配置
2012-03-18 18:56 1820jBPM 需要数据库支持, j ... -
xfire 编写webservice例子
2012-02-23 16:51 1055来自 : http://www.blogjava.net/j ... -
ArrayUtils常用的用法
2012-02-21 10:51 1568// 1.打印数组 ArrayUtils.toString(n ... -
Tomcat自动缓存context.xml,导致在eclipse中修改后,不能立即启作用
2012-02-18 11:56 1772http://blog.csdn.net/shiqidide/ ... -
JavaScript CSS Style属性对照表
2012-02-11 22:36 1092CSS语法 (不区分大小写) JavaScript语法 (区分 ...
相关推荐
struts.xml常用配置解析 网上转载
Dom4j解析struts2框架的struts.xml,自定义struts框架需要解析配置文件
dom4j解析struts.xml需要,上一次上传忘了这个包装类
struts.xml的错误解决办法,解决错误
如果设置为true,Struts2将加载XML配置文件;否则,不加载。 5. struts.continuations.package:这是一个包名,包含使用Rife continuations的actions。 6. struts.custom.i18n.resources:这是一个附加的国际化...
NULL 博文链接:https://chaoyi.iteye.com/blog/2156728
专为multipart请求信息使用的org.apache.struts2.dispatcher.multipart.MultiPartRequest解析器接口(文件上传用) struts.multipart.saveDir The directory to use for storing uploaded files 设置存储上传...
struts2学习教程包括:第一个Struts2程序,处理一个form多个submit,struts.xml常用配置解析,使用validate方法验证数据,使用Validation框架验证数据,在Action类中获得HttpServletResponse对象的四种方法,上传...
2 将struts2 整合进去, 这次在struts.xml中我们使用通配符的方式配置action。 a 加入支持 : 添加struts2.3.15 必需包 以及 struts json包(ajax要用到),spring整合struts2包,spring web 包,在src目录下建立...
S2-016 漏洞是由于 Struts2 的 Ognl 表达式语言解析器存在缺陷,从而导致攻击者可以 inject 恶意代码。这个漏洞影响了 Struts2 的所有版本。 S2-017 漏洞是由于 Struts2 的 DefaultActionMapper 的 ...
主要介绍了struts2 validation.xml 验证规则代码解析,具有一定借鉴价值,需要的朋友可以参考下
配置常量,可以改变Struts 2框架的一些行为 name属性表示常量名称,value属性表示常量值 package元素: 包的作用:简化维护工作,提高重用性 包可以“继承”已定义的包,并可以添加自己包的配置 name属性为必须去且...
SSH(struts+spring+hibernate)的jar包 SSH 通常指的是 Struts2 做前端控制器,Spring ...配置文件加载顺序为:default.properties -> struts-default.xml -> struts-plugins.xml -> struts.xml -> struts.locale。
目前的版本可以为web(web.xml)、ejb、struts(struts-config.xml)、webwork、hibernate (mapping file)、jdo、jmx等等生成描述文件、源码等,XDoclet提供了ant的任务target支持,完全通过ant来完成任务。 我们只须...
弃用了struts,用spring mvc框架做了几个项目,感觉都不错,而且使用了注解方式,可以省掉一大堆配置文件。本文主要介绍使用注解方式配置的spring mvc,之前写的spring3.0 mvc和rest小例子没有介绍到数据层的内容,...
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...
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 ...