1.编写代码 实现在jsp页面 写内容 使用拦截器 过滤一些文字
1.先编写jsp显示页面的代码
2.配置struts.xml
<!-- (1)中文乱码处理 表单递交和参数传递适用 数据的乱码不能处理 -->
<constant name="struts.i18n.encoding" value="gb2312"/>
配置拦截器类
<interceptors>
<interceptor name="inter1" class="com.interception.action.MyInterceptor"></interceptor>
</interceptors>
还必须用到defaultStack这个包的方法
<interceptor-ref name="defaultStack"></interceptor-ref>
在这里使用
<interceptor-ref name="inter1"></interceptor-ref>
3.action 端com.interception.action.MyInterceptor 验证类的方法
先继承一个类 并实现方法
//获取当前的action(InterAction)
System.out.println("拦截器在执行");
获取需要验证的action
InterAction action=(InterAction)interceptor.getAction();
if(action!=null) //InterAction 的业务方法正在执行
{
String oldcontent=action.getContent();
System.out.println(oldcontent+"++++++++");
判断是否有这个字符串
if(oldcontent.contains("常信院"))
{
调用replaceFirst替换字符串部分内容
String newcontent=oldcontent.replaceFirst("常信院", "替换的内容");
System.out.println(newcontent+"========");
action.setContent(newcontent);(设置action的新内容)
}
return interceptor.invoke(); (成功需调用这个方法)
}
else {
return Action.LOGIN; (不成功则返回字符串 login)
}