首页 > 代码库 > 关于有继承modelDriven接口的action的ajax调用传入参数
关于有继承modelDriven接口的action的ajax调用传入参数
如 Model类如下,Teacher,public class Teacher{
private Integer id;
priavte String name;
private School school;
}
而School, public class School{
private Integer id;
priavte String name;
}
上面的Teacher,School省去了getter和setter方法。
action类如下
public class TeacherAction extends ActionSupport implents ModelDriven<Teacher>{
public String saveTeacher(){
if(.teacher.getSchool() != null && teacher.getSchool().getId() != null){
teacher.setSchool(schoolDao.load(teacher.getSchool().getId()));
}
Teacher teacher= teacherDao.save(teacher);
return SUCCESS;
}
}
该action配置如下
<package name="ajax" extends="json-default">
<action name=teacher_"*" class="com.zxt.action.TeacherAction method={1}">
<!-- 将返回类型设置为json -->
<result type="json"></result>
</action>
</package>
jsp格式如下:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>测试</title>
<script type="text/javas cript" src=http://www.mamicode.com/"include/js/jquery-1.4.2.min.js"></script>
<script type="text/javas cript">
$(function() {
$("#tj").click(function() {
//提交的参数,name和inch是和struts action中对应的接收变量
var params = {
“ school.id” : $("#xm").val(),
“ name” : $("#xm1").val(
};
$.ajax({
type: "POST",
url: "teacher_findTeacherByScholl.action",
data: params,
dataType:"text", //ajax返回值设置为text(json格式也可用它返回,可打印出结果,也可设置成json)
success: function(json){
var obj = $.parseJSON(json); //使用这个方法解析json
var state_value = obj.teacher //result是和action中定义的result变量的get方法对应的
alert(state_value);
},
error: function(json){
alert("json=" + json);
return false;
}
});
});
});
</script>
</head>
<body>
<span>学校:</span><input id="xm" type="text">
<br/>
<span>名字:</span><input id="xm1" type="text">
<br/>
<input type="button" value=http://www.mamicode.com/"提交" id="tj">
</body>
</html>