strut.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>
<!-- 调试模式-->
<constant name="struts.devMode" value="true" />
<constant name="struts.ui.theme" value="simple" />
<package name="default" namespace="/" extends="struts-default">
<interceptors>
<!-- 定义拦截器 -->
<interceptor name="myinterceptor" class="com.interceptor.MyInterceptor" />
<interceptor-stack name="myStack">
<!--指定引用的拦截器 -->
<interceptor-ref name="defaultStack" />
<interceptor-ref name="myinterceptor" />
</interceptor-stack>
</interceptors>
<action name="login" class="com.action.Login">
<result name="index">index.jsp</result>
<result name="success">welcome.jsp</result>
<result name="fail">index.jsp</result>
<interceptor-ref name="myStack" />
</action>
</package>
</struts>
实体类user
<pre class="java" name="code">package com.pojo;
public class User {
private String username;
private String password;
private String executeType;
public String getExecuteType() {
return executeType;
}
public String getPassword() {
return password;
}
public String getUsername() {
return username;
}
public void setExecuteType(String executeType) {
this.executeType = executeType;
}
public void setPassword(String password) {
this.password = password;
}
public void setUsername(String username) {
this.username = username;
}
@Override
public String toString() {
return "User [username=" + username + ", password=" + password
+ ", executeType=" + executeType + "]";
}
}
前台页面:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <% 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">
<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script> <script type="text/javascript"> function startEngine() { console.info("-----"); var username = $("input[name='username']").val(); var password=$("input[name='password']").val(); console.info(username); console.info(password); $.ajax({ url: '<%=basePath%>login.action', type: 'post', data: { 'user.username':username, 'user.password':password, 'user.executeType':"手动", 'executeType':"自动" }, success:function(results) { console.info("success"); } }); }
</script>
</head>
<body> <s:actionerror/> <form action="login.action"> <input type="text" name="username"/> <input type="text" name="password"/> <input type="button" οnclick="startEngine()" value="提交"/> </form> <a href="page.action">内容页面</a> </body> </html>
自己写的拦截器
package com.interceptor;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.AbstractInterceptor; import com.pojo.User;
public class MyInterceptor extends AbstractInterceptor {
private User user;
private String executeType;
@Override public String intercept(ActionInvocation invocation) throws Exception { // 获取request对象 HttpServletRequest request = (HttpServletRequest) invocation .getInvocationContext().get(ServletActionContext.HTTP_REQUEST); System.out.println("executeType:1"+request.getAttribute("user.executeType")); System.out.println(user); System.out.println("----------"); return invocation.invoke(); }
public String getExecuteType() { return executeType; }
public User getUser() { return user; } public void setExecuteType(String executeType) { this.executeType = executeType; }
public void setUser(User user) { this.user = user; }
}
action 类
package com.action;
import com.opensymphony.xwork2.ActionSupport; import com.pojo.User;
public class Login extends ActionSupport {
private User user;
private String executeType;
@Override public String execute() throws Exception { System.out.println("Login.execute()"); System.out.println(user); System.out.println(executeType); return SUCCESS; } public String getExecuteType() { return executeType; } public User getUser() { return user; }
public void setExecuteType(String executeType) { this.executeType = executeType; }
public void setUser(User user) { this.user = user; }
}
执行结果
executeType:1手动 null ---------- Login.execute() User [username=name, password=pwd, executeType=手动] 自动
最终结论:
通过ajax异步发送到后台的数据 struts并不会给你填充进interceptor拦截器的属性中,在拦截器中想要获取前台的值只能通过得到request对象,然后获取参数,但是,如果调用invocation.invoke();到action里面的话,struts会给你按照参数名字填充进入对应的属性。有兴趣的可以自己试一下源码