日期:2014-05-20  浏览次数:20648 次

Action经过拦截器后获取不到值
--------structs.xml-------------------------
<package name="default" extends="struts-default" namespace="/">
  <interceptors>
<interceptor name="myinterceptor" class="interceptor.LoginInterceptor"></interceptor>
</interceptors>
<default-interceptor-ref name="myinterceptor"></default-interceptor-ref> <global-results><result name="error">/error.jsp</result></global-results>
  <action name="testaction" class="action.TestAction" method="{1}">
  <interceptor-ref name="myinterceptor"></interceptor-ref> <result name="login">/login.jsp</result>
  <result name="index">/index.jsp</result>
  <result name="workDetail">/workDetail.jsp</result>
  <result name="work">/work.jsp</result>
  <result name="reg">/reg.jsp</result>
  <result name="updatepwd">/updatepwd.jsp</result>
  </action>
  </package>
------------------------Interceptor-------------------------------------

public class LoginInterceptor extends AbstractInterceptor {
@Override
@SuppressWarnings("unchecked")
public String intercept(ActionInvocation invocation) throws Exception {
// TODO Auto-generated method stub
//String actionname=invocation.getProxy().getActionName();
String method=invocation.getProxy().getMethod();
ActionContext ctx=invocation.getInvocationContext();
HttpServletRequest request=ServletActionContext.getRequest();
String username=request.getParameter("user.userName");
String userpwd=request.getParameter("user.userPwd");
System.out.println(username+"----------"+userpwd);
ServletActionContext.getResponse().setCharacterEncoding("gbk");
Map<String,Object> session=ctx.getSession();
User user=(User)session.get("user");
if(!"dologin".equals(method)){
if(user==null){
//request.setAttribute("msg","<script>alert('没有登录!');</script>");
return "login";
}
}
return invocation.invoke();
}

}
-----------------------------------Action-------------------------------
public String dologin(){
if(null==user.getUserName()||null==user.getUserPwd())
return "error"; user=testservice.login(user.getUserName(), user.getUserPwd());
if(null==user){
this.getRequest().put("msg","<script>alert('用户名或密码错误!');</script>");
return "login";
}
List<WorkPlan> workplans=testservice.viewWorkPlanDetail(user,workPlanId,up_workPlanId);
this.getSession().put("user",user);
this.getRequest().put("workplans",workplans);
return "index";
}

--------------------------------jsp-----------------------------------

<form action="testaction!dologin.action" onsubmit="return getRoles();" method="post">
  <div style="background:url(images/bg/bg_right.jpg);width:400px;height:270px;margin:0 auto;">
  <div><span class="span">用户名:</span><input class="username" type="text" id="user.userName" name="user.userName"/></div>
  <div><span class="span2">密码:</span><input class="password" type="password" id="user.userPwd" name="user.userPwd"/></div>