一、Login.jsp
1、<head>引入jquery:
<script type="text/javascript"
src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.1.js"></script>
2、界面控件:
用户名: <input type="text" id="userName">
<input type="button" id="btnAjax" value="Ajax调用" />
3、提交ajax请求
<script type="text/javascript">
$(function() {
$("#btnAjax").click(function() {
var userName = $("#userName").val().trim();
if (userName == '') {
alert("用户名不能为空");
return false;
}
//login1为Action类命名空间名称;AjaxExecute为Action方法名称
$.ajax({
type : "post",
url : '/login1/ajaxExecute',
data : {//设置数据源
userName : userName,
password : "我是密码"
},
dataType : "json",//设置需要返回的数据类型
success : function(d) {
alert(d.sayHi);
},
error : function(d) {
alert(d.responseText);
}
});
});
});
</script>
二、后台类:LoginActionNoNamespce
package action; import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map; import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.validation.SkipValidation; import freemarker.template.SimpleDate; @ParentPackage("json-default")
@Namespace("/login1")
public class LoginActionNoNamespce extends SuperActionSupport {
private static final long serialVersionUID = 1L;
private Map<String, String> dataMap; //会把本类所有getter方法序列化成字符串返回给jsp页面
public Map<String, String> getDataMap() {
return dataMap;
} public void setDataMap(Map<String, String> dataMap) {
this.dataMap = dataMap;
} @SkipValidation
@Action(value = "ajaxExecute", results = { @Result(type = "json", params = { "root", "dataMap" }) })
public String ajaxExecute() throws Exception {
this.dataMap = new HashMap<String, String>(); // 参数名称必须和jsp的空间名称一一对应
String userName = request.getParameter("userName");
String password = request.getParameter("password");
dataMap.put("isSuccess", "true");
SimpleDateFormat now = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
dataMap.put("sayHi", "Hi:" + userName + " 当前时间为:" + now.format(new Date()) + password); return SUCCESS;
}
}
三、注意事项
- 类注解:@ParentPackage("json-default"),比如为json-default,不能为struts-default
json-default是继承于structs-default的,见.m2\repository\org\apache\struts\struts2-json-plugin\2.3.16.3\struts2-json-plugin-2.3.16.3.jar\struts-plugin.xml - Action注解:@Action(value = "ajaxExecute", results = { @Result(type = "json", params = { "root", "dataMap" }) }),这里的type必须为json、root是固定值,去掉params节点会返回undefined
- 这里的dataMap定义为:private Map<String, String> dataMap;必须要添加getter、setter参数,否则返回null
- JSP页面(这里可以用命名空间,如
url : '/login1/ajaxExecute', @ParentPackage("json-default")这样设置就不会报错) - maven管理,需要引用的库
<dependencies><dependency><groupId>org.apache.struts</groupId><artifactId>struts2-core</artifactId><version>2.3.16</version><exclusions><exclusion><groupId>org.javassist</groupId><artifactId>javassist</artifactId></exclusion></exclusions></dependency><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.1.0</version></dependency><dependency><groupId>org.apache.struts</groupId><artifactId>struts2-convention-plugin</artifactId><version>2.3.16.3</version></dependency><dependency><groupId>org.apache.struts</groupId><artifactId>struts2-json-plugin</artifactId><version>2.3.16.3</version></dependency><dependency><groupId>net.sf.json-lib</groupId><artifactId>json-lib</artifactId><version>2.4</version></dependency>
</dependencies>