struts2开发时通过interceptor拦截器实现输入数据过滤前后空格的功能
来源:互联网 发布:婚礼邀请函电子版软件 编辑:程序博客网 时间:2024/06/15 09:32
首先在拦截器注册文件interceptorContext.xml中声明拦截器:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <bean id="onlineInterceptor" class="main.com.eca.interceptor.OnlineInterceptor"> </bean> <!-- 空格过滤拦截器 --> <bean id="trimInterceptor" class="main.com.eca.interceptor.TrimInterceptor"> </bean> </beans>
然后配置struts.xml文件,加入拦截队列
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <include file="config/catalog.xml"/> <package name="default" extends="struts-default"> <interceptors> <interceptor name="onlineInterceptor" class="onlineInterceptor"></interceptor> <interceptor name="trimInterceptor" class="trimInterceptor"></interceptor> <interceptor-stack name="baseInterceptorStack"> <interceptor-ref name="onlineInterceptor"></interceptor-ref> <interceptor-ref name="trimInterceptor"></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </interceptor-stack> </interceptors> <default-interceptor-ref name="baseInterceptorStack"></default-interceptor-ref> <global-results> <result name="user" type="redirect">/index.jsp?url=${url}</result> </global-results> </package> </struts>
这里注意一下,在拦截器栈中,trimInterceptor的位置要在defaultStack之上,否则不会生效甚至报错
下一步就是实现拦截器代码了
这里在开发中犯了一次二,就是在取参数值的时候,一开始得到value后直接.toString(),然后trim()了,再塞回去,可是报错,参数违法什么的;之后输出了class类型:
System.out.println(value.getClass());
输出为:
class [Ljava.lang.String; class [Ljava.lang.String; class [Ljava.lang.String; class [Ljava.lang.String; class [Ljava.lang.String;
当时看成了String类型了,一看没错啊,怎么不对呢,找了会问题,才哗然大悟,form里面的参数其实是数组的,相同name提交后得到是参数是数组的数据,而且显示的class显示为String[]的,然后修改输出一看OK了
System.out.println(value.getClass()+"--"+JsonUtil.toJson(value));
class [Ljava.lang.String;--["22222"] class [Ljava.lang.String;--[""] class [Ljava.lang.String;--[""] class [Ljava.lang.String;--["11111"] class [Ljava.lang.String;--["3333"]
package main.com.eca.interceptor; import java.util.Iterator; import java.util.Map; import java.util.Set; import org.apache.commons.lang.StringUtils; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.Interceptor; public class TrimInterceptor implements Interceptor { private static final long serialVersionUID = -2578561479301489061L; public void destroy() { } /* * @Description:拦截所有参数,去掉参数空格 * @see com.opensymphony.xwork2.interceptor.Interceptor#intercept(com.opensymphony.xwork2.ActionInvocation) */ public String intercept(ActionInvocation invocation) throws Exception { Map map=invocation.getInvocationContext().getParameters(); Set keys = map.keySet(); Iterator iters = keys.iterator(); while(iters.hasNext()){ Object key = iters.next(); Object value = map.get(key); map.put(key, transfer((String[])value)); } return invocation.invoke(); } /** * @Description: 遍历参数数组里面的数据,取出空格 * @param params * @return */ private String[] transfer(String[] params){ for(int i=0;i<params.length;i++){ if(StringUtils.isEmpty(params[i]))continue; params[i]=params[i].trim(); } return params; } public void init() { } }
0 0
- struts2开发时通过interceptor拦截器实现输入数据过滤前后空格的功能
- struts2开发时通过interceptor拦截器实现输入数据过滤前后空格的功能
- struts2开发时通过interceptor拦截器实现输入数据过滤前后空格的功能
- struts2的拦截器(3):通过实现Interceptor接口定义自己的拦截器
- struts2的拦截器(Interceptor)
- Struts2的拦截器interceptor
- struts2的拦截器Interceptor
- Struts2拦截器(Interceptor)
- Struts2拦截器(Interceptor)
- struts2 拦截器 interceptor
- struts2 拦截器 interceptor
- Struts2拦截器(Interceptor)
- Struts2拦截器(Interceptor)
- Struts2拦截器(Interceptor)
- Struts2拦截器(Interceptor)
- Struts2拦截器(Interceptor)
- Struts2拦截器(Interceptor)
- Struts2拦截器(Interceptor)
- ListView中常用Adapter
- Nofication的使用
- windows tcp连接 recv()立即返回 接收字符为空
- c语言中包含math.h的时用gcc编译要加-lm参数
- Binder学习笔记(七)—— ServiceManager如何响应addService请求 ?
- struts2开发时通过interceptor拦截器实现输入数据过滤前后空格的功能
- C还可以这么玩第二期
- Android开发,30行代码集成图片轮播RollViewpager,你说快吗?
- 封装类:即8种基本类型对应8种封装类
- NSLock/NSRecursiveLock/NSConditionLock/@synchronized 详细解释
- iOS开发系列—Objective-C之Foundation框架
- Binder学习笔记(八)—— 客户端如何组织Test()请求 ?
- 蓝桥杯:排序
- 关于Session的保存用户信息的用法。