搜档网
当前位置:搜档网 › Struts2结果类型、拦截器、类型转换器

Struts2结果类型、拦截器、类型转换器

Struts2结果类型、拦截器、类型转换器
Struts2结果类型、拦截器、类型转换器

结果类型

Action处理完用户请求后,将返回一个普通的字符串,整个普通字符串就是一个逻辑视图。Struts2通过配置一个逻辑视图和物理视图的映射关系,一旦系统返回某个逻辑视图系统就会把对应的物理视图呈现给用户。

Struts2 在struts.xml中使用来配置结果,根据元素的位置不同可以分为两类:

局部结果:将 作为子元素来配置。

全局结果:将作为元素的子元素配置。

配置 元素时通常需要指定两个属性:

name:该属性指定所需要的逻辑视图名

拦截器

Struts2中提供了很多特性都是通过拦截器实现的,例如异常处理,文件上传,生命周期回调和验证。拦截器从概念上来讲和Servlet过滤器或JDK的Proxy类是一样的。提供了一种对Action进行预处理和事后处理的功能。

自定义拦截器

要自定义拦截器需要实现com.opensymphony.xwork2.interceptor.Interceptor接口:public class PermissionInterceptor implements Interceptor {

private static final long serialVersionUID = -5178310397732210602L;

public void destroy() {

}

public void init() {

}

public String intercept(ActionInvocation invocation) throws Exception {

System.out.println("进入拦截器");

if(session里存在用户){

String result = invocation.invoke();

}else{

return “logon”;

}

//System.out.println("返回值:"+ result);

//return result;

}

}

class="net.xabc.aop.PermissionInterceptor" />

/WEB-INF/page/hello.jsp

因为struts2中如文件上传,数据验证,封装请求参数到action等功能都是由系统默认的defaultStack中的拦截器实现的,所以我们定义的拦截器需要引用系统默认的defaultStack,这样应用才可以使用struts2框架提供的众多功能。

如果希望包下的所有action都使用自定义的拦截器,可以通过

把拦截器定义为默认拦截器。注意:每个包只能指定一个默认拦截器。另外,一旦我们为该包中的某个action 显式指定了某个拦截器,则默认拦截器不会起作用

类型转换器

自定义类型转换器

下面的action中使用到了枚举类型Gender,当需要将请求参数注入到gender属性时,我们必须定义转换器,否则struts2无法自动完成类型转换。

public class HelloWorldAction {

private Gender gender;

public Gender getGender() {

return gender;

}

public void setGender(Gender gender) {

this.gender = gender;

}

}

public enum Gender {

MAN,WOMEN

}

下面定义了一个针对Gender类型的类型转换器:

public class GenderConverter extends DefaultTypeConverter {

@Override

public Object convertValue(Map context, Object value, Class toType) { if(toType == Gender.class){//当字符串向Gender类型转换时

String[] params = (String[]) value;

return Gender.valueOf(params[0]);

}else if(toType == String.class){//当Gender转换成字符串时

Gender gender = (Gender) value;

return gender.toString();

}

return null;

}

}

将上面的类型转换器注册为局部类型转换器:

在Action类所在的包下放置ActionClassName-conversion.properties文件,ActionClassName是Action的类名,后面的-conversion.properties是固定写法,对于本例而言,文件的名称应为HelloWorldAction-conversion.properties 。在properties文件中的内容为:

属性名称=类型转换器的全类名

对于本例而言,HelloWorldAction-conversion.properties文件中的内容为:gender= net.xabc.conversion.GenderConverter

将上面的类型转换器注册为全局类型转换器:

在WEB-INF/classes下放置xwork-conversion.properties文件。在properties文件中的内容为:

待转换的类型=类型转换器的全类名

对于本例而言,xwork-conversion.properties文件中的内容为:

net.xabc.action.Gender= net.xabc.conversion.GenderConverter

相关主题