1.正常情况,把对象放入request作用域中,返回一个字符型,跳转页面,是可以取到值的。
ajax.jsp:
<%--
Created by IntelliJ IDEA.
User: 书山有路勤为径
Date: 2019/9/17
Time: 12:40
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>ajax回调时取值问题</title>
</head>
<body>
<a href="${pageContext.request.contextPath}/ajax/value">点我执行</a>
</body>
</html>
AjaxController:
package com.controller;
import com.model.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/ajax")
public class AjaxController {
@RequestMapping("/value")
public String ajaxQuestion(Model model){
System.out.println("进来了");
Student stu = new Student();
stu.setName("小舞");
stu.setAge(18);
stu.setStuNo("35");
//把stu对象放入request作用域
model.addAttribute("student",stu);
return "value";
}
}
value.jsp:
<%--
Created by IntelliJ IDEA.
User: 书山有路勤为径
Date: 2019/9/17
Time: 12:49
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title></title>
</head>
<body>
姓名:${student.name}</br>
学号:${student.stuNo}</br>
年龄:${student.age}
</body>
</html>
1.1执行前
1.2执行后
2.使用ajax后,无论是保存在request作用域,还是保存在session作用域返回时都取不到值。
使用jquery的ajax前 , 引入jQuery库:
<script src="${pageContext.request.contextPath}/js/jquery-3.2.1.js"></script>
ajax.jsp :
<%--
Created by IntelliJ IDEA.
User: 书山有路勤为径
Date: 2019/9/17
Time: 12:40
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>ajax回调时取值问题</title>
<script src="${pageContext.request.contextPath}/js/jquery-3.2.1.js"></script><!--引入jQuery库-->
<script>
function ajaxValue() {
$.ajax({
url:"${pageContext.request.contextPath}/ajax/value",
type:"post",
data:"null",
success:function(result){
//result是json的字符串或json数组,把json字符串转换为json对象
var jsonObj = JSON.parse(result);
//使用JavaScript或jQuery等实现局部更新
$.each(jsonObj,function(i,c){
if(i=="success"){
alert(jsonObj[i]);
}
});
},
error:function(){
alert("失败");
}
});
}
</script>
</head>
<body>
<a href="javascript:void(0);" onclick="ajaxValue()">点我执行</a></br>
姓名:<span id="name">${student.name}</span></br>
学号:<span id="no">${student.stuNo}</span></br>
年龄:<span id="age">${student.age}</span></br>
</body>
</html>
AjaxController :
package com.controller;
import com.model.Student;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import javax.servlet.http.HttpServletResponse;
@SessionAttributes(value = {"student"})
@Controller
@RequestMapping("/ajax")
public class AjaxController {
@RequestMapping("/value")
public void ajaxQuestion(Model model, HttpServletResponse response) throws Exception{
System.out.println("进来了");
Student stu = new Student();
stu.setName("小舞");
stu.setAge(18);
stu.setStuNo("35");
//把stu对象放入request作用域
model.addAttribute("student",stu);
//Ajax回调
JSONObject json = new JSONObject();
json.put("success","取值问题");
response.getWriter().println(json);
}
}
2.1 执行前:
2.2 点击执行
2.3 执行后,student对象在AjaxController已经保存在了request作用域中(model.addAttribute("student",stu);),甚至也保存在了session作用域中(@SessionAttributes(value = {"student"})),但回调后仍是取不到值的。因为保存在session中了,可以在回调成功的函数里利用window.location.href="http://holcalhost:8080/hotel/jsp/ajax.jsp" ,来跳转到该界面,可以获取到值,可这就违背了使用ajax的初衷。
3. 细想了一下,还是把 student对象 放在json里返回,再处理就可以取得值了,改动后的代码如下:
AjaxController :
//Ajax回调
JSONObject json = new JSONObject();
json.put("success",stu);
response.getWriter().println(json);
Ajax.jsp :
alert("ajax");
$.each(jsonObj,function(i,c){
if(i=="success"){
$("#name").html(c.name);
$("#no").html(c.stuNo);
$("#age").html(c.age);
}
});
3.1执行前
3.2点击执行
3.3执行后