我想实现访问一个acion时候,如果这个action不存在,就是我没配置,我想直接调某个action。效果跟在web.xml设置
错误页面一样。
<error-page>
<error-code>500</error-code>
<location>/common/error/500.jsp</location>
</error-page>
按照上面的设置如果访问的也没或者action出现编译错误就会跳转到web.xml文件中指定的错误页面。
但是我现在要想实现:我随意输入一个action,页面不能提示我出错,只要出错就调用一个叫xu的action。
首先,你确认你用的是WebWork 2.2.1以上版本,因为在配置文件xwork.xml中新增加了了一个元素: default-action-
ref。
参考文档如下: http://wiki.opensymphony.com/display/WW/Action+configuration
源代码文件如下:
<default-action-ref name="simpleViewResultAction">
<!--
An example of a default action that is just a simple class
that has 3 fields: successUrl, errorUrl, and inputUrl. This action
parses the request url to set the result values. In the normal case
it just renders velocity results of the same name as the requested url.
-->
<action name="simpleViewResultAction" class="SimpleViewResultAction">
<result type="velocity">${successUrl}</result>
<result name="error" type="velocity">${errorUrl}</result>
<result name="input" type="velocity">${inputUrl}</result>
</action>
...
</package>
我们用谷歌翻译来翻译下:
这只是一个简单的类的一个默认动作的一个例子 有3个领域:successUrl,errorUrl,并inputUrl。
这个动作解析请求的URL的结果集值。在正常情况下它只是呈现请求的URL的名称相同的速度结果。
我发现最近谷歌翻译有新功能了,鼠标放到翻译出的单词上面呈现黄色区域。
注意:放置 <default-action-ref> 是有顺序的如:
The content of element type "package" must match "(result- types?,interceptors?,default-interceptor-
ref?,default-action- ref?,default-class-ref?,global-results?,global-exception- mappings?,action*)".
部分配置如下:
<? xml version = "1.0" encoding = "UTF-8" ?> |
02 | <!DOCTYPE struts PUBLIC |
03 | "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" |
05 | |
06 | < struts > |
07 | < include file = "user.xml" /> |
08 | < include file = "goods.xml" /> |
09 | < include file = "order.xml" /> |
10 | </ struts > |
user.xml:
01 | <? xml version = "1.0" encoding = "UTF-8" ?> |
02 | <!DOCTYPE struts PUBLIC |
03 | "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" |
05 | |
06 | < struts > |
07 | < package name = "wwfy" extends = "struts-default" > |
08 | < action name = "login" class = "wwfy.user.LoginAction" > |
09 | <!--省略Action其他配置--> |
10 | </ action > |
11 | < action name = "logout" class = "wwfy.user.LogoutAction" > |
12 | <!--省略Action其他配置--> |
13 | </ action > |
14 | </ package > |
15 | </ struts > |
2、<constant>
在之前提到struts.properties配置文件的介绍中,我们曾经提到所有在struts.properties文件中定义的属性,都可以配置在struts.xml文件中。而在struts.xml中,是通过<constant>标签来进行配置的:
01 | <? xml version = "1.0" encoding = "UTF-8" ?> |
02 | <!DOCTYPE struts PUBLIC |
03 | "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" |
05 | |
06 | < struts > |
07 | <!--设置开发模式--> |
08 | < constant name = "struts.devMode" value = "true" /> |
09 | <!--设置编码形式为GB2312--> |
10 | < constant name = "struts.i18n.encoding" value = "GB2312" /> |
11 | <!--省略其他配置信息--> |
12 | </ struts > |
3、<package>
1、包属性介绍
在Struts2框架中是通过包来管理action、result、interceptor、interceptor-stack等配置信息的。包属性如下:
属性 | 是否必需 | 描述 |
name | 是 | 包名,作为其它包应用本包的标记 |
extends | 否 | 设置本包继承其它包 |
namespace | 否 | 设置包的命名空间 |
abstact | 否 | 设置为抽象包 |
2、extends属性的详解
- 当一个包通过配置extends属性继承了另一个包的时候,该包将会继承父包中所有的配置,包括action、result、interceptor等。
- 由于包信息的获取是按照配置文件的先后顺序进行的,所以父包必须在子包之前被定义。
- 通常我们配置struts.xml的时候,都继承一个名为“struts-default.xml”的包,这是struts2中内置的包。
3、namespace的详解
namespace主要是针对大型项目中Action的管理,更重要的是解决Action重名问题,因为不在同一个命名空间的Action可以使用相同的Action名的。
1)如果使用命名空间则URL将改变
比如我们有一下配置文件
1 | < package name = "wwfy" extends = "struts-default" > |
2 | < action name = "login" class = "wwfy.action.LoginAction" > |
3 | < result >/success.jsp</ result > |
4 | </ action > |
5 | </ package > |
则此配置下的Action的URL为http://localhost:8080/login.action
假如为这个包指定了命名空间
1 | < package name = "wwfy" extends = "struts-default" namespace = "/user" > |
2 | < action name = "login" class = "wwfy.action.LoginAction" > |
3 | < result >/success.jsp</ result > |
4 | </ action > |
5 | </ package > |
则此配置下的Action的URL为http://localhost:8080/user/login.action
2)默认命名空间
Struts2中如果没有为某个包指定命名空间,该包使用默认的命名空间,默认的命名空间总是""。
3)指定根命名空间
当设置了命名空间为“/”,即指定了包的命名空间为根命名空间时,此时所有根路径下的Action请求都会去这个包中查找对应的资源信息。
假若前例中路径为http://localhost:8080/login.action则所有http://localhost:8080/*.action都会到设置为根命名空间的包中寻找资源。
4、<action>与<result>
1、<action>属性介绍
属性名称 | 是否必须 | 功能描述 |
name | 是 | 请求的Action名称 |
class | 否 | Action处理类对应具体路径 |
method | 否 | 指定Action中的方法名 |
converter | 否 | 指定Action使用的类型转换器 |
如果没有指定method则默认执行Action中的execute方法。
2、<result>属性介绍
属性名称 | 是否必须 | 功能描述 |
name | 否 | 对应Action返回逻辑视图名称,默认为success |
type | 否 | 返回结果类型,默认为dispatcher |
3、通配符的使用
随着result的增加,struts.xml文件也会随之变得越来越复杂。那么就可以使用通配符来简化配置:
例如下面这个案例:
Action为Test.java
01 | public class Test { |
02 | public String test1(){ |
03 | return "result1" ; |
04 | } |
05 | |
06 | public String test2(){ |
07 | return "result2" ; |
08 | } |
09 | |
10 | public String test3(){ |
11 | return "result3" ; |
12 | } |
13 | } |
struts.xml中配置为
1 | < package name = "wwfy" extends = "struts-default" > |
2 | < action name = "test*" class = "wwfy.action.test{1}" > |
3 | < result name = "result{1}" >/result{1}.jsp</ result > |
4 | </ action > |
5 | </ package > |
4、访问Action方法的另一种实现方式
在Struts2中如果要访问Action中的指定方法,还可以通过改变URL请求来实现,将原本的“Action名称.action”改为“Action名称!方法名称.action”在struts.xml中就不需要指定方法名了。
5、<exception-mapping>与<global-exception-mapping>
这两个标签都是用来配置发生异常时对应的视图信息的,只不过一个是Action范围的,一个是包范围的,当同一类型异常在两个范围都被配置时,Action范围的优先级要高于包范围的优先级.这两个标签包含的属性也是一样的:
属性名称 | 是否必须 | 功能描述 |
name | 否 | 用来表示该异常配置信息 |
result | 是 | 指定发生异常时显示的视图信息,这里要配置为逻辑视图 |
exception | 是 | 指定异常类型 |
两个标签的示例代码为:
01 | <? xml version = "1.0" encoding = "UTF-8" ?> |
02 | <!DOCTYPE struts PUBLIC |
03 | "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" |
05 | |
06 | < struts > |
07 | < package name = "default" extends = "struts-default" > |
08 | < global-exception-mappings > |
09 | < exception-mapping result = "逻辑视图" exception = "异常类型" /> |
10 | </ global-exception-mappings > |
11 | < action name = "Action名称" > |
12 | < exception-mapping result = "逻辑视图" exception = "异常类型" /> |
13 | </ action > |
14 | </ package > |
15 | </ struts > |
6、<default-class-ref>
当我们在配置Action的时候,如果没有为某个Action指定具体的class值时,系统将自动引用<default-class-ref>标签中所指定的类。在Struts2框架中,系统默认的class为ActionSupport,该配置我们可以在xwork的核心包下的xwork-default.xml文件中找到。
有特殊需要时,可以手动指定默认的class
1 | package wwfy.action; |
2 | |
3 | public class DefaultClassRef { |
4 | public void execute(){ |
5 | System.out.println( "默认class开始执行……" ); |
6 | } |
7 | } |
在struts.xml中配置
01 | <? xml version = "1.0" encoding = "UTF-8" ?> |
02 | <!DOCTYPE struts PUBLIC |
03 | "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" |
05 | |
06 | < struts > |
07 | < package name = "wwfy" extends = "struts-default" > |
08 | <!-- 指定默认class为Test --> |
09 | < default-class-ref class = "wwfy.action.DefaultClassRef" /> |
10 | < action name = "test1" > |
11 | < result >/index.jsp</ result > |
12 | </ action > |
13 | </ package > |
14 | </ struts > |
7、<default-action-ref>
如果在请求一个没有定义过的Action资源时,系统就会抛出404错误。这种错误不可避免,但这样的页面并不友好。我们可以使用<default-action-ref>来指定一个默认的Action,如果系统没有找到指定的Action,就会指定来调用这个默认的Action。
01 | <? xml version = "1.0" encoding = "UTF-8" ?> |
02 | <!DOCTYPE struts PUBLIC |
03 | "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" |
05 | |
06 | < struts > |
07 | < package name = "wwfy" extends = "struts-default" > |
08 | |
09 | < default-action-ref name = "acctionError" ></ default-action-ref > |
10 | < action name = "acctionError" > |
11 | < result >/jsp/actionError.jsp</ result > |
12 | </ action > |
13 | </ package > |
14 | </ struts > |
8、<default-interceptor-ref>
该标签用来设置整个包范围内所有Action所要应用的默认拦截器信息。事实上我们的包继承了struts-default包以后,使用的是Struts的默认设置。我们可以在struts-default.xml中找到相关配置:
1 | < default-interceptor-ref name = "defaultStack" /> |
在实际开发过程中,如果我们有特殊的需求是可以改变默认拦截器配置的。当时一旦更改这个配置,“defaultStack”将不再被引用,需要手动最加。
9、<interceptors>
通过该标签可以向Struts2框架中注册拦截器或者拦截器栈,一般多用于自定义拦截器或拦截器栈的注册。该标签使用方法如下:
1 | < interceptors > |
2 | < interceptor name = "拦截器名" class = "拦截器类" /> |
3 | < interceptor-stack name = "拦截器栈名" > |
4 | < interceptor-ref name = "拦截器名" > |
5 | </ interceptor-stack > |
6 | </ interceptors > |
10、<interceptor-ref>
通过该标签可以为其所在的Action添加拦截器功能。当为某个Action单独添加拦截器功能后,<default-interceptor-ref>中所指定的拦截器将不再对这个Action起作用。
11、<global-results>
该标签用于设置包范围内的全局结果集。在多个Action返回相同逻辑视图的情况下,可以通过<global-results>标签统一配置这些物理视图所对应的逻辑视图。
01 | <? xml version = "1.0" encoding = "UTF-8" ?> |
02 | <!DOCTYPE struts PUBLIC |
03 | "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" |
05 | |
06 | < struts > |
07 | < package name = "wwfy" extends = "struts-default" > |
08 | < global-results > |
09 | < result name = "test" >/index.jsp</ result > |
10 | </ global-results > |
11 | </ package > |
12 | </ struts > |
struts2的常量配置:
<!-- 指定Web应用的默认编码集,相当于调用HttpServletRequest的setCharacterEncoding方法 -->
<constant name="struts.i18n.encoding" value="UTF-8"/>
<!-- 该属性指定需要Struts 2处理的请求后缀,该属性的默认值是action,即所有匹配*.action的请求都由Struts2处理。
如果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开。 -->
<constant name="struts.action.extension" value="do"/>
<!-- 设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好关闭 -->
<constant name="struts.serve.static.browserCache" value="false"/>
<!-- 当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生产环境下使用),开发阶段最好打开 -->
<constant name="struts.configuration.xml.reload" value="true"/>
<!-- 开发模式下使用,这样可以打印出更详细的错误信息 -->
<constant name="struts.devMode" value="true" />
<!-- 默认的视图主题 -->
<constant name="struts.ui.theme" value="simple" />
spring 托管
<constant name="struts.objectFactory" value="spring" />
<struts>
=======================以下配置信息在properties文件中=========================
### 指定加载struts2配置文件管理器,默认为org.apache.struts2.config.DefaultConfiguration
### 开发者可以自定义配置文件管理器,该类要实现Configuration接口,可以自动加载struts2配置文件。
#struts.configuration=org.apache.struts2.config.DefaultConfiguration
### 设置默认的locale和字符编码
struts.locale=zh_CN
struts.i18n.encoding=GBK
### 指定struts的工厂类
struts.objectFactory = spring
### 指定spring框架的装配模式
### 装配方式有: name, type, auto, and constructor (name 是默认装配模式)
struts.objectFactory.spring.autoWire = name
### 该属性指定整合spring时,是否对bean进行缓存,值为true or false,默认为true.
struts.objectFactory.spring.useClassCache = true
### 指定类型检查
#struts.objectTypeDeterminer = tiger
#struts.objectTypeDeterminer = notiger
### 该属性指定处理 MIME-type multipart/form-data,文件上传
# struts.multipart.parser=cos
# struts.multipart.parser=pell
struts.multipart.parser=jakarta
# 指定上传文件时的临时目录,默认使用 javax.servlet.context.tempdir
#struts.multipart.saveDir=/tmpuploadfiles
struts.multipart.maxSize=2097152
### 加载自定义属性文件 (不要改写struts.properties!)
# struts.custom.properties=application,org/apache/struts2/extension/custom
### 指定请求url与action映射器,默认为org.apache.struts2.dispatcher.mapper.DefaultActionMapper
#struts.mapper.class=org.apache.struts2.dispatcher.mapper.DefaultActionMapper
### 指定action的后缀,默认为action
struts.action.extension=so
### 被 FilterDispatcher使用
### 如果为 true 则通过jar文件提供静态内容服务.
### 如果为 false 则静态内容必须位于 <context_path>/struts
struts.serve.static=true
### 被 FilterDispatcher使用
### 指定浏览器是否缓存静态内容,测试阶段设置为false,发布阶段设置为true.
struts.serve.static.browserCache=true
### 设置是否支持动态方法调用,true为支持,false不支持.
struts.enable.DynamicMethodInvocation = true
### 设置是否可以在action中使用斜线,默认为false不可以,想使用需设置为true.
struts.enable.SlashesInActionNames = true
### 是否允许使用表达式语法,默认为true.
struts.tag.altSyntax=true
### 设置当struts.xml文件改动时,是否重新加载.
### - struts.configuration.xml.reload = true
### 设置struts是否为开发模式,默认为false,测试阶段一般设为true.
struts.devMode = true
### 设置是否每次请求,都重新加载资源文件,默认值为false.
struts.i18n.reload=false
###标准的UI主题
### 默认的UI主题为xhtml,可以为simple,xhtml或ajax
struts.ui.theme=xhtml
###模板目录
struts.ui.templateDir=template
#设置模板类型. 可以为 ftl, vm, or jsp
struts.ui.templateSuffix=ftl
###定位velocity.properties 文件. 默认 velocity.properties
struts.velocity.configfile = velocity.properties
### 设置velocity的context.
struts.velocity.contexts =
### 定位toolbox.
struts.velocity.toolboxlocation=
### 指定web应用的端口.
struts.url.http.port = 80
### 指定加密端口
struts.url.https.port = 443
### 设置生成url时,是否包含参数.值可以为: none, get or all
struts.url.includeParams = get
### 设置要加载的国际化资源文件,以逗号分隔.
#struts.custom.i18n.resources=application
### 对于一些web应用服务器不能处理HttpServletRequest.getParameterMap()
### 像 WebLogic, Orion, and OC4J等,须设置成true,默认为false.
struts.dispatcher.parametersWorkaround = false
### 指定freemarker管理器
#struts.freemarker.manager.classname=org.apache.struts2.views.freemarker.FreemarkerManager
### 设置是否对freemarker的模板设置缓存
### 效果相当于把template拷贝到 WEB_APP/templates.
struts.freemarker.templatesCache=false
### 通常不需要修改此属性.
struts.freemarker.wrapper.altMap=true
### 指定xslt result是否使用样式表缓存.开发阶段设为true,发布阶段设为false.
struts.xslt.nocache=false
### 设置struts自动加载的文件列表.
struts.configuration.files=struts-default.xml,struts-plugin.xml,struts.xml
### 设定是否一直在最后一个slash之前的任何位置选定namespace.
struts.mapper.alwaysSelectFullNamespace=false