struts2开发时通过interceptor拦截器实现输入数据过滤前后空格的功能
2013-12-15 15:35
513 查看
因为做的项目管理项目居多,有很多查询列表页面,少不了名称查询等功能,但是如果每个逻辑中都验证过滤前后空格会比较麻烦,就像用struts的拦截器实现全部输入的字符串过滤来实现,效果不错,但是在实现过程中有几个地方耽误了点时间,也温故知新了些知识,这里总结一下,互相学习一下
介绍下结构,项目采用SSH框架
首先在拦截器注册文件interceptorContext.xml中声明拦截器:
[html] view
plaincopyprint?
<strong><?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></strong>
然后配置struts.xml文件,加入拦截队列
[html] view
plaincopyprint?
<strong><?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>
</strong>
这里注意一下,在拦截器栈中,trimInterceptor的位置要在defaultStack之上,否则不会生效甚至报错
下一步就是实现拦截器代码了
这里在开发中犯了一次二,就是在取参数值的时候,一开始得到value后直接.toString(),然后trim()了,再塞回去,可是报错,参数违法什么的;之后输出了class类型:
[java] view
plaincopyprint?
<strong>System.out.println(value.getClass());</strong>
输出为:
[html] view
plaincopyprint?
<strong>class [Ljava.lang.String;
class [Ljava.lang.String;
class [Ljava.lang.String;
class [Ljava.lang.String;
class [Ljava.lang.String;</strong>
当时看成了String类型了,一看没错啊,怎么不对呢,找了会问题,才哗然大悟,form里面的参数其实是数组的,相同name提交后得到是参数是数组的数据,而且显示的class显示为String[]的,然后修改输出一看OK了
[java] view
plaincopyprint?
<strong>System.out.println(value.getClass()+"--"+JsonUtil.toJson(value));</strong>
[html] view
plaincopyprint?
<strong>class [Ljava.lang.String;--["22222"]
class [Ljava.lang.String;--[""]
class [Ljava.lang.String;--[""]
class [Ljava.lang.String;--["11111"]
class [Ljava.lang.String;--["3333"]</strong>
下面是详细代码,也可以在这里下载:
http://download.csdn.net/detail/songylwq/4896548
[java] view
plaincopyprint?
<strong>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() {
}
}
</strong>
介绍下结构,项目采用SSH框架
首先在拦截器注册文件interceptorContext.xml中声明拦截器:
[html] view
plaincopyprint?
<strong><?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></strong>
然后配置struts.xml文件,加入拦截队列
[html] view
plaincopyprint?
<strong><?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>
</strong>
这里注意一下,在拦截器栈中,trimInterceptor的位置要在defaultStack之上,否则不会生效甚至报错
下一步就是实现拦截器代码了
这里在开发中犯了一次二,就是在取参数值的时候,一开始得到value后直接.toString(),然后trim()了,再塞回去,可是报错,参数违法什么的;之后输出了class类型:
[java] view
plaincopyprint?
<strong>System.out.println(value.getClass());</strong>
输出为:
[html] view
plaincopyprint?
<strong>class [Ljava.lang.String;
class [Ljava.lang.String;
class [Ljava.lang.String;
class [Ljava.lang.String;
class [Ljava.lang.String;</strong>
当时看成了String类型了,一看没错啊,怎么不对呢,找了会问题,才哗然大悟,form里面的参数其实是数组的,相同name提交后得到是参数是数组的数据,而且显示的class显示为String[]的,然后修改输出一看OK了
[java] view
plaincopyprint?
<strong>System.out.println(value.getClass()+"--"+JsonUtil.toJson(value));</strong>
[html] view
plaincopyprint?
<strong>class [Ljava.lang.String;--["22222"]
class [Ljava.lang.String;--[""]
class [Ljava.lang.String;--[""]
class [Ljava.lang.String;--["11111"]
class [Ljava.lang.String;--["3333"]</strong>
下面是详细代码,也可以在这里下载:
http://download.csdn.net/detail/songylwq/4896548
[java] view
plaincopyprint?
<strong>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() {
}
}
</strong>
相关文章推荐
- struts2开发时通过interceptor拦截器实现输入数据过滤前后空格的功能
- struts2开发时通过interceptor拦截器实现输入数据过滤前后空格的功能
- struts2开发时通过interceptor拦截器实现输入数据过滤前后空格的功能
- struts2:数据校验,通过XWork校验框架实现(validation.xml)
- 通过百度echarts实现数据图表展示功能
- asp.net mvc通过预处理实现数据过滤和数据篡改。
- iOS开发标记——各个ViewController页面数据交互,通过NSUserDefaults实现。
- jQuery实现手机号正则验证输入及自动填充空格功能
- 通过 spring 容器内建的 profile 功能实现开发环境、测试环境、生产环境配置自动切换
- [Unity3D]手机3D游戏开发:如何实现最高分的存储与显示(七)----使用Game ID避免数据重复输入
- jquery+ashx无刷新GridView数据显示插件(实现分页、排序、过滤功能)
- 微信小程序简单实现form表单获取输入数据功能示例
- C语言实现密码输入显示星号 VS2010 亲测通过 功能加强版
- DataView数据视图实现数据过滤,排序(代码调试通过)
- 使用C1TrueDBGrid表格控件实现数据绑定、分组和过滤功能
- Vuforia SDK---- AR开发vuforia 相机前后摄像头动态切换功能实现
- Javascript 5种方法实现过滤删除前后所有空格
- 通过 spring 容器内建的 profile 功能实现开发环境、测试环境、生产环境配置自动切换
- struts2--实现文字过滤功能
- 通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串过滤程序,若字符串中出现多个相同的字符,将非首次出现的字符过滤掉。 比如字符串“abacacde”过滤结果为“abcde”。 要求实现函