Struts2开发 基本配置与类型转换

Java教程 2025-08-07

一、Action配置中的各项默认值

< package name="csdn" namespace="/test" extends="struts-default" >

< action name="helloworld" method="execute"  >

< result name="success" >/WEB-INF/page/hello.jsp< /result >

< /action >

< /package >

1 >如果没有为action指定class,默认是ActionSupport。

2 >如果没有为action指定method,默认执行action中的execute() 方法。

3 >如果没有指定result的name属性,默认值为success。

二、Action中result的各种转发类型

< action name="helloworld" >

< result name="success" >/WEB-INF/page/hello.jsp< /result >

< /action >

result配置类似于struts1中的forward,但struts2中提供了多种结果类型,常用的类型有: dispatcher(默认值)、 redirect 、 redirectAction 、 plainText。

下面是redirectAction 结果类型的例子,如果重定向的action中同一个包下:

< result type="redirectAction" >helloworld< /result >

如果重定向的action在别的命名空间下:

< result type="redirectAction" >

< param name="actionName" >helloworld< /param >

< param name="namespace" >/test< /param >

< /result >

plaintext:显示原始文件内容,例如:当我们需要原样显示jsp文件源代码 的时候,我们可以使用此类型。

< result name="source" type="plainText " >

< param name="location" >/xxx.jsp< /param >

< param name="charSet" >UTF-8< /param >< !-- 指定读取文件的编码 -- >

< /result >

在result中还可以使用${属性名}表达式访问action中的属性,表达式里的属性名对应action中的属性。如下:

< result type="redirect" >view.jsp?id=${id}< /result >

三、多个Action共享一个视图--全局result配置

当多个action中都使用到了相同视图,这时我们应该把result定义为全局视图。struts1中提供了全局forward,struts2中也提供了相似功能:

< package .... >

< global-results >

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

< /global-results >

< /package >

四、为Action的属性注入值

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

public class HelloWorldAction{

private String savePath;

public String getSavePath() {

return savePath;

}

public void setSavePath(String savePath) {

this.savePath = savePath;

}

......

}

< package name="csdn" namespace="/test" extends="struts-default" >

< action name="helloworld"  >

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

< result name="success" >/WEB-INF/page/hello.jsp< /result >

< /action >

< /package >

上面通过< param >节点为action的savePath属性注入“/images”

五、指定需要Struts 2处理的请求后缀

前面我们都是默认使用.action后缀访问Action。其实默认后缀是可以通过常量”struts.action.extension“进行修改的,例如:我们可以配置Struts 2只处理以.do为后缀的请求路径:

< ?xml version="1.0" encoding="UTF-8"? >

< !DOCTYPE struts PUBLIC

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

"http://struts.apa*c*h*e.org/dtds/struts-2.0.dtd" >

< struts >

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

< /struts >

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

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

六、细说常量定义

常量可以在struts.xml或struts.properties中配置,建议在struts.xml中配置,两种配置方式如下:

在struts.xml文件中配置常量

< struts >

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

< /struts >

在struts.properties中配置常量

struts.action.extension=do

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

struts-default.xml

struts-plugin.xml

struts.xml

struts.properties

web.xml

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

七、常用的常量介绍

< !-- 指定默认编码集,作用于HttpServletRequest的setCharacterEncoding方法 和freemarker 、velocity的输出 -- >

< 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集成时,指定由spring负责action对象的创建 -- >

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

< !该属性设置Struts 2是否支持动态方法调用,该属性的默认值是true。如果需要关闭动态方法调用,则可设置该属性为false。 -- >

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

< !--上传文件的大小限制-- >

< constant name="struts.multipart.maxSize" value=“10701096"/ >

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

在大部分应用里,随着应用规模的增加,系统中Action的数量也会大量增加,导致struts.xml配置文件变得非常臃肿。为了避免struts.xml文件过于庞大、臃肿,提高struts.xml文件的可读性,我们可以将一个struts.xml配置文件分解成多个配置文件,然后在struts.xml文件中包含其他配置文件。下面的struts.xml通过< include >元素指定多个配置文件:

< ?xml version="1.0" encoding="UTF-8"? >

< !DOCTYPE struts PUBLIC

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

"http://struts.apa*c*h*e.org/dtds/struts-2.0.dtd" >

< struts >

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

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

< /struts >

通过这种方式,我们就可以将Struts 2的Action按模块添加在多个配置文件中。

九、动态方法调用

如果Action中存在多个方法时,我们可以使用!+方法名调用指定方法。如下:

public class HelloWorldAction{

private String message;

....

public String execute() throws Exception{

this.message = "我的第一个struts2应用";

return "success";

}

public String other() throws Exception{

this.message = "第二个方法";

return "success";

}

}

假设访问上面action的URL路径为: /struts/test/helloworld.action

要访问action的other() 方法,我们可以这样调用:

/struts/test/helloworld!other.action

如果不想使用动态方法调用,我们可以通过常量struts.enable.DynamicMethodInvocation关闭动态方法调用。

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

十、使用通配符定义action

< package name=“csdn” namespace="/test" extends="struts-default" >

< action name="helloworld_*" method="{1}" >

< result name="success" >/WEB-INF/page/hello.jsp< /result >

< /action >

< /package >

public class HelloWorldAction{

private String message;

....

public String execute() throws Exception{

this.message = "我的第一个struts2应用";

return "success";

}

public String other() throws Exception{

this.message = "第二个方法";

return "success";

}

}

要访问other()方法,可以通过这样的URL访问:/test/helloworld_other.action

十一、接收请求参数

采用基本类型接收请求参数(get/post)

在Action类中定义与请求参数同名的属性,struts2便能自动接收请求参数并赋予给同名属性。

请求路径: http://loc*alhos*t*:8080/test/view.action?id=78

public class ProductAction {

private Integer id;

public void setId(Integer id) {//struts2通过反射技术调用与请求参数同名的属性的setter方法来获取请求参数值

this.id = id;

}

public Integer getId() {return id;}

}

采用复合类型接收请求参数

请求路径: http://lo*calho**st:8080/test/view.action?product.id=78

public class ProductAction {

private Product product;

public void setProduct(Product product) { this.product = product; }

public Product getProduct() {return product;}

}

Struts2首先通过反射技术调用Product的默认构造器创建product对象,然后再通过反射技术调用product中与请求参数同名的属性的setter方法来获取请求参数值。

十二、类型转换的意义

对于一个智能的MVC框架而言,不可避免的需要实现类型转换.因为B/S(浏览器/服务器)结构应用的请求参数是通过浏览器发送到服务器的,这些参数不可能有丰富的数据类型,因此必须在服务器端完成数据类型的转换

MVC框架是一个表现层解决方案,理应提供类型转换的支持,Struts2提供了功能非常强大的类型转换支持.

十三、表现层数据的处理

对于web应用而言,表现层主要用于与用户交互,包括收集用户输入数据,向用户呈现服务器的状态。因此表现层的数据的流向主要有两个方向:输入数据和输出数据。

对于输入数据:则需要完成由字符串数据向多种类型数据的转化。程序通常无法自动完成,需要在代码中手动转化

对于输出数据:不管是java或是jsp都支持多种数据类型的直接输出。

表现层另外一个数据处理是:数据校验,分为客户校验和服务器端校验.后边会重点讲解

十四、类型转换

HTTP参数都是字符串类型。 保存的数据可能是字符串、数字、布尔、日期时间等或者JavaBean类型。 手工的类型转换,比如将字符串转换为日期,通过: 通过request.getParameter方法获取字符串; 检查是否为空; 通过DateFormat.parse方法将字符串转换为Date对象

十五、Struts2类型转换

Struts2内置的类型转换

String和boolean 完成字符串与布尔值之间的转换

String和char 往常字符串与字符之间的转换

String和int、Integer 完成字符串与整型之间的转换

String和Long 完成字符串与长整型值之间的转换

String和double、Double 完成字符串与双精度浮点值的转换

String和Float 完成字符串和单精度浮点之间的转换

String和Date 完成字符串和日期类型之间的转换,日期格式使用格式用户请求所在Locale的SHORT格式

String和数组 在默认的情况,数组元素是字符串,如果用户定义类型转换器,也可以是其它复合数据类型

String和Map、List

十六、Struts类型转换的API

Struts2的类型转换器实际上是基于OGNL实现的,在OGNL项目中有一个ognl.TypeConverter接口,这个接口就是实现类型转换器必须实现的接口。该接口定义如下:

public interface TypeConverter {

public Object convertValue(Map arg0, Object arg1, Member arg2, String arg3,

Object arg4, Class arg5) {

return null;

}

实现类型转换器必须实现上面的TypeConverter,不过上面的接口里的方法过于复杂,所以OGNL项目还提供了一个该接口实现类:ognl.DefaultTypeConverter,通过继承该类实现自己类型转换器.该类定义如下:

public class DefaultTypeConverter extends Object implements TypeConverter{

public Object convertValue(Map< String,Object > context, Object value, Class toType) {

}

……//其他的方法

}

ConvertValue方法的作用

该方法完成类型转换,不过这种类型转换是双向的,当需要把字符串转化对象实例时,通过该方法实现,当把对象实例转换成字符串时也通过该方法实现。这种转换是通过toType参数类型是需要转换的目标类型。所以可以根据toType参数来判断转换方向。

ConvertValue方法参数和返回意义

第一个参数:context是类型转换环境的上下文

第二个参数:value是需要转换的参数,根据转换方向的不同value参数的值也是不一样的。

第三个参数:toType是转换后的目标类型

该方法的返回值是类型转换后的值。该值的类型也会随着转换的方向的改变而改变。由此可见转换的convertValue方法接受需要转换的值,需要转换的目标类型为参数,然后返回转换后的目标值

Value为什么是一个字符串数组?

对于DefaultTypeConverter转换器而言,它必须考虑到最通用的情形,因此他把所有请求参数都视为字符串数组而不是字符串。相当于getParameterValues()获取的参数值