【发布时间】:2015-05-04 18:16:35
【问题描述】:
我的相关请求处理代码如下
@RequestMapping(value = "/customer" , method = RequestMethod.GET)
public String customer(ModelMap modelMap , @RequestParam Integer age) {
modelMap.addAttribute("requestKey", "Hola!");
modelMap.addAttribute("age", age);
return "cust";
}
@RequestMapping(value = "/request" , method = RequestMethod.GET)
public String welcome(ModelMap modelMap , @RequestParam(value = "age" , required = false)Integer age) {
modelMap.addAttribute("requestKey", "Hola!");
modelMap.addAttribute("age", age);
return "request";
}
@RequestMapping(value = "/request" , method = RequestMethod.POST)
public String responseBody(ModelMap modelMap , final @RequestBody Student student){
modelMap.addAttribute("name", student.getName());
modelMap.addAttribute("lastname", student.getLastname());
modelMap.addAttribute("age", student.getAge());
return "request";
}
cust.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Customer</title>
</head>
<body>
<p>${age}</p>
</body>
</html>
request.jsp
<html>
<head>
<title>RequestBody</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
JSONTest = function() {
$.ajax({
url: "customer",
type: "get",
data: {
name: "Okan",
lastname: "Pehlivan",
age:22 },
dataType: "json"
});
};
</script>
</head>
<body>
<p>${requestKey}</p>
<h1>My jQuery JSON Web Page</h1>
<div id="resultDivContainer"></div>
<button type="button" onclick="JSONTest()">JSON</button>
</body>
</html>
我想使用 ajax 获取请求,并且我有一些参数(名称 = Okan,例如)。我想在 cust.jsp 上显示任何参数。当我调试welcome() 方法的代码时,年龄分配了我的年龄。我用它的键“年龄”映射了这个值。 request.jsp 文件没有改变。
【问题讨论】:
-
您能否详细说明您对 request.jsp 的期望是引用者还是年龄值或两者兼而有之??
-
带按钮,我想发送带参数的请求/request,并在cust.jsp中获取这个patameter。我将编辑我的 request.jsp 文件
-
你在 welcome() 方法中的年龄值是什么?
标签: java spring jsp spring-mvc model-view-controller