您的位置:首页 > 编程语言 > Java开发

Struts2初学 Struts.xml详解二

2016-02-05 10:22 537 查看
A、使用继承实现设置全局视图

package节点中还可以设置全局的视图,如:

<global-results>

<result
name="err">/err.jsp</result>

</global-results>

但如果其他包也想实现这样的视图,就需要使用继承的方式实现,如:

<package name="base" namespace=""
extends="struts-default">

<global-results>

<result
name="err">/err.jsp</result>

</global-results>

</package>

然后其他包分别继承当前的基础包,如:

<package name="demo" namespace=""
extends="base">

<action name="hello" method="excute"
class="action.HelloAction">

<result
name="ok">/index.jsp</result>

</action>

</package>

B、依赖注入属性

Struts2为Action中的属性提供了依赖注入功能,在struts2的配置文件中,我们可以很方便地为Action中的属性注入值。注意:属性必须提供setter方法。

public class HelloAction{

private
String savePath;

public
String getSavePath() {

return
savePath;

}

public void
setSavePath(String savePath) {

this.savePath = savePath;

}

}

<action name="hello" method="excute"
class="action.HelloAction“>

<!--这里的param就是提供了依赖注入的功能,其中name的值为savePath,则对应的就是ACTION类中添加的set和get属性的方法名称

<param
name="savePath">/upload</param>

<result
name="ok">/index.jsp</result>

</action>

C、指定struts2处理请求的后缀

struts2可以通过设置常量“struts.action.extension”进行修改所请求的路径后缀,如:

<constant name="struts.action.extension"
value="do,go"/>

以上的value属性指的意思,如果用户请求的文件后缀分别是.do或.go都将进入struts2来处理,设置多个以英文逗号隔开。

D、struts2的常量

1、常量可以在struts.xml或struts.properties两个文件中配置,建议使用struts.xml中配置,

在struts.xml中的配置常量如下:

<constant name="常量名称"
value="值"/>

在struts.properties中配置常量如下:

常量名称=值

因为常量可以在下面多个配置文件中进行定义,所以需要了解struts2加载常量的搜索顺序:

struts-default.xml

struts-plugin.xml

struts.xml

struts.properties

web.xml

如果在多个文件中配置了同一个常量,则后一个文件中配置的常量值会覆盖前面文件中配置的常量值。

2、struts2的常量


常用的常量介绍:

struts.i18n.encoding=utf-8
:指定默认编码集,作用于httpservletrequest的setcharacterencoding方法

struts.action.extension=do,action:指定需要struts2处理的请求后缀,该属性的默认值是action,即所以的匹配.action
的请求都由struts2处理.

如果用户需要指定多个请求后缀,则多个后缀之间用英文逗号隔开.

struts.serve.static.browserCache=true:设置浏览器是否缓存静态内容,默认值是true(生产环境下使用),开发阶段最好关闭

struts.configuration.xml.reload=true:当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false,开发阶段设置为true

struts.devMode=true:开发模式下使用,这样可以打印出更详细的错误信息,

该属性的默认值是false。通常,应用在开发阶段,将该属性设置为true,当进入产品发布阶段后,则该属性设置为false。

struts.ui.theme=simple:

该属性指定视图标签默认的视图主题,该属性的默认值是xhtml。
struts.objectFactory=spring:与spring集成时,指定由spring负责
action对象的创建

struts.enable.DynamicMethodlnvocation=false:该属性设置struts2是否支持动态方法调用,默认值是true

struts.multipart.maxSize=10240000:用于限制上传文件的大小

struts.multipart.allowedTypes=.xls,.zip:用于限制上传文件类型

struts.multipart.parser=pell:解决上传空文件的报错问题

--------------以下都是不常使用的

struts.multipart.parser=jakarta:该属性指定处理multipart/form-data的MIME类型(文件上传)请求的框架,该属性支持cos,pell和jakarta等属性值,
即分别对应使用cos的文件上传框架,pell上传及common-fileupload文件上传框架.该属性的默认值为jakarta.

注意:如果需要使用cos或者pell的文件上传方式,则应该将对应的JAR文件复制到Web应用中.例如,使用cos上传方式,则需要自己下载cos框架的JAR文件,并将该文件放在WEB-INF/lib路径下

struts.multipart.saveDir:该属性指定上传文件的临时保存路径,该属性的默认值是javax.servlet.context.tempdir

如:

//指定默认编码集,作用于
HttpServletRequest的setCharacterEncoding方法和freemarker,velocity的输出

<constant name="struts.i18n.encoding"
value="UTF-8/GBK"/>

//指定需要struts2处理的请求后缀,该属性的默认值是action,即所以的匹配.action
的请求都由struts2处理.

如果用户需要指定多个请求后缀,则多个后缀之间用英文逗号隔开.

<constant name="struts.action.extension"
value="do,action"/>

[b]//
设置浏览器是否缓存静态内容,默认值为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集成时,指定由spring负责action对象的创建

<constant name="struts.objectFactory"
value="spring"/>

//设置struts2是否支持动态方法调用,该属性的默认值是true,如果需要关闭动态方法调用,则可设置该属性为false

<constant
name="struts.enable.DynamicMethodInvocation"
value="false"/>

//上传文件的大小限制

<constant name="struts.multipart.maxSize"
value="10701096"/>最大为5M(上传的文件的总大小,并不是单个文件的大小)[/b]

//上传文件的类型限制
<constant
name="struts.multipart.allowedTypes"
value="'.txt','.xls','.csv','.zip'"
/>

//这个应该可以解决上传空文件的报错问题

<constant
name="struts.multipart.parser" value="pell"
/>


更详细的struts2常量请参看:http://wing123.iteye.com/blog/352596
http://hi.baidu.com/hduzhou/item/8c0c50035cb0c629a1312d6e

E、为应用指定多个struts配置文件

在大部分应用里,随着应用规模的增加,系统中的action的数量也会大量增加,导致struts.xml配置文件非常庞大,可读性及维护性差,所以可以将一个struts.xml文件分解成多个配置文件,然后在struts.xml中包含其他配置文件,

如:

<struts>

<include
file="struts-user.xml"/>

<include
file="struts-order.xml"/>

</struts>

F、动态方法的调用和使用通配符定义action

如果action中存在多个方法时,可以使用“!+方法名”调用指定方法(前提是:D的2中动态方法调用要设为true),如下:

public class HelloAction{

public
String execute(){}

public
String other(){}

}

如果访问上面的action的URL路径为:/abc/hello,如果要访问other()方法,可以这样调用:

/abc/hello!other,
如果不想使用动态方法调用,可以通过设置常量关闭。

当D的2中动态方法调用要设为关闭时,也可以使用通配符定义action,如下:

<action name="hello_*" class="action.helloaction"
method="{1}">

</action>

如果要访问other()方法,可以通过这样的URL访问:/abc/hello_other,其中{1}为占位符
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: