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值,百思不得其- -.......