又是一个session的问题,每次遇到session的问题,总是要纠结很久
1.首先编写jsp页面
<body>
<s:form action="index">
<s:textfield name="name" label="消息"></s:textfield>
<s:submit value="发送" ></s:submit>
</s:form>
你说:<s:property value="name"/>
</body>
2.编写后台的拦截器与Action
(1)拦截器
public class intercept implements Interceptor{
@Override
public void destroy() {
System.out.println("拦截器销毁");
}
@Override
public void init() {
System.out.println("拦截器初始化");
}
@Override
public String intercept(ActionInvocation arg) throws Exception {
HttpServletRequest request = ServletActionContext.getRequest();
String name = request.getParameter("name");
System.out.println("Action调用之前");
if(name.contains("擦")){
name = name.replaceAll("擦", "**");
}
User user = new User();
user.setName(name);
String result = arg.invoke();
System.out.println("Action调用之后");
return result;
}
}
(2)Action
public class indexAction extends ActionSupport {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String execute() throws Exception {
name = User.getName();
return SUCCESS;
}
我中间使用的User,便是一个模拟session的容器,在其中存放你需要的消息
(3)User
public class User {
public static String name;
public static String getName() {
return name;
}
public static void setName(String name) {
User.name = name;
}
}
3.最后就是在struts中配置清楚
<package name="default" extends="struts-default">
<interceptors >
<interceptor name="Test" class="com.insigma.interceptor.test"></interceptor>
<interceptor-stack name="defaultTest">
<interceptor-ref name="Test"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<action name="index" class="com.insigma.action.indexAction">
<interceptor-ref name="defaultTest">
</interceptor-ref>
<result name="success">/jsp/index.jsp </result>
</action>
</package>
4.输出结果,如下图
原文链接:http://hi.baidu.com/cqqxyt/item/f2d37ce4eb5dd1d8eb34c94f