yeluochengshang 2013-03-01 16:22
浏览 278
已采纳

为什么在使用了interceptor之后action中就无法获得form表单中的数据了?求解~~!

1.action

package com.michael.logindemo.action;

import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.michael.logindemo.service.LoginService;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {

    /**
     * 
     */
    private static final long serialVersionUID = -3956302064077854639L;
    
    private String username;
    private String password;
    private String newPassword;
    private String birthday;
    private LoginService loginService;
    

    public String login() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        simpleDateFormat.format(birthday);
        
        loginService = new LoginService();
        boolean isPasswordCorrect = loginService.verifyPassword(username, password);
        if(isPasswordCorrect) {
            HttpServletRequest request = ServletActionContext.getRequest();
            request.getSession().setAttribute("username", username);
            return "loginSuccess";
        }
        return "loginError";
    }
    
    public String modifyPassword() {
        loginService = new LoginService();
        if(loginService.modifyPassword(username, password, newPassword)) {
            return "modifyPasswordSuccess";
        }
        return "modifyPasswordError";
    }
    
    public String getNewPassword() {
        return newPassword;
    }

    public void setNewPassword(String newPassword) {
        this.newPassword = newPassword;
    }

    public LoginService getLoginService() {
        return loginService;
    }
    
    public void setLoginService(LoginService loginService) {
        this.loginService = loginService;
    }
    
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getBirthday() {
        return birthday;
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }
}

 2.struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
    <package name="default" extends="struts-default" namespace="/" >
    <interceptors>
        <interceptor name="helloInterceptor" class="com.michael.logindemo.interceptor.HelloInterceptor"></interceptor>
    </interceptors>
    
        <action name="login_*" class="com.michael.logindemo.action.LoginAction" method="{1}" converter="">
            <interceptor-ref name="helloInterceptor"></interceptor-ref>
            <result name="loginSuccess">login/loginSuccess.jsp</result>
            <result name="loginError">login/loginError.jsp</result>
            <result name="modifyPasswordSuccess">login/modifyPasswordSuccess.jsp</result>
            <result name="modifyPasswordError">login/modifyPasswordError.jsp</result>
        </action>     
    </package>
</struts>    

  3.interceptor

package com.michael.logindemo.interceptor;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class HelloInterceptor implements Interceptor {

    /**
     * 
     */
    private static final long serialVersionUID = -6597833693985491134L;

    public void destroy() {
    }

    public void init() {
    }

    public String intercept(ActionInvocation ai) throws Exception {
        System.out.println("hello interceptor");
        
        return ai.invoke();
    }

}

4.jsp

 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <link rel="stylesheet" type="text/css" href="styles.css">
  </head>
  
  <body>
  <form name="loginform" method="post" action="login_login.action">
    <label>用户名<input type="text" name="username"></label><br>
    <label>密 码<input type="text" name="password"></label><br>
    <label>生 日<input type="text" name="birthday"></label><br>
    <label><input type="submit" name="Submit" value="提交"></label>
  </form>
  </body>
</html>

再没有使用interceptor的时候action中可以获得jsp中form表单的值,但是在使用了一个简单的不能够再简单的interceptor之后,action中就无法获得jsp中form表单内填写的值了,在用myeclipse debug的时候,username,password,birthday都是null值,百思不得其- -.......

  • 写回答

1条回答 默认 最新

  • iteye_2178 2013-03-01 16:40
    关注
            <!-- 注意两个interceptor-ref的顺序, 顺序不同, 执行效果也不同: 先配置的先执行/后配置的先退出(先进后出) -->
            <interceptor-ref name="defaultStack"></interceptor-ref>
            <interceptor-ref name="myInterceptor"></interceptor-ref>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。