为什么我没有在 Spring MVC 中获取 referer 参数
why didn't i get the referer parameter in Spring MVC
我的相关请求处理代码如下
@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 获取请求,并且我有一些参数,(例如 name=Okan)。我想在 cust.jsp 上显示任何参数。当我调试 welcome() 方法的代码时,年龄分配了我的年龄。我用它的键 "age" 映射了这个值。 request.jsp 文件未更改。
Ajax调用不会改变视图。它将成功 return 同一页面上的参数: function(response) {} 。如果你想改变视图,你需要在一个单独的方法上进行非 ajax GET 调用并传递内联参数,如:
控制器:
@RequestMapping(value = "/customer/{name}/{lastname}/{age}" , method = RequestMethod.GET)
public String customer(@PathVariable(value = "name") String name, @PathVariable(value = "lastname") String lastname, @PathVariable(value = "age") String age) {
//code here
return "cust";
}
JSP
window.open("/customer/Okan/Pehlivan/22 ","_self");
它将在 cust.jspreturn
请求 jsp 没有更改,因为在 AJAX 调用之后您没有在页面上的任何地方更改它。
您确实提出了请求,但没有使用响应。
向您的 ajax 调用添加一个成功函数,然后读取那里的数据。
例如:
$.ajax({
url, data , dataType and method as is with an addition of the following :
success : function(responseText){
Set responseText to your <p>
}
});
为了显示cust.jsp中的年龄,需要在调用url时传递请求参数。类似于:http://host-path/context-path/customer?age=69
自动装箱应该处理请求参数的 int
到 Integer
的转换,如果您看到异常请尝试使用 int
。
一些可能有用的信息:
- 在 jsp / javascript 中使用 ${varName} 时,您需要模型
已有该数据。
- 考虑使用标签来控制动态的输出
变量,它们可能会有所帮助。
- 进行 ajax 调用以请求数据时,在
处理程序并从那里继续你的逻辑。
HTH.
我的相关请求处理代码如下
@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 获取请求,并且我有一些参数,(例如 name=Okan)。我想在 cust.jsp 上显示任何参数。当我调试 welcome() 方法的代码时,年龄分配了我的年龄。我用它的键 "age" 映射了这个值。 request.jsp 文件未更改。
Ajax调用不会改变视图。它将成功 return 同一页面上的参数: function(response) {} 。如果你想改变视图,你需要在一个单独的方法上进行非 ajax GET 调用并传递内联参数,如:
控制器:
@RequestMapping(value = "/customer/{name}/{lastname}/{age}" , method = RequestMethod.GET)
public String customer(@PathVariable(value = "name") String name, @PathVariable(value = "lastname") String lastname, @PathVariable(value = "age") String age) {
//code here
return "cust";
}
JSP
window.open("/customer/Okan/Pehlivan/22 ","_self");
它将在 cust.jspreturn
请求 jsp 没有更改,因为在 AJAX 调用之后您没有在页面上的任何地方更改它。 您确实提出了请求,但没有使用响应。 向您的 ajax 调用添加一个成功函数,然后读取那里的数据。 例如:
$.ajax({
url, data , dataType and method as is with an addition of the following :
success : function(responseText){
Set responseText to your <p>
}
});
为了显示cust.jsp中的年龄,需要在调用url时传递请求参数。类似于:http://host-path/context-path/customer?age=69
自动装箱应该处理请求参数的 int
到 Integer
的转换,如果您看到异常请尝试使用 int
。
一些可能有用的信息:
- 在 jsp / javascript 中使用 ${varName} 时,您需要模型 已有该数据。
- 考虑使用标签来控制动态的输出 变量,它们可能会有所帮助。
- 进行 ajax 调用以请求数据时,在 处理程序并从那里继续你的逻辑。
HTH.