Java 为什么没有';在Spring MVC中获取referer参数
我的相关请求处理代码如下Java 为什么没有';在Spring MVC中获取referer参数,java,spring,jsp,spring-mvc,model-view-controller,Java,Spring,Jsp,Spring Mvc,Model View Controller,我的相关请求处理代码如下 @RequestMapping(value = "/customer" , method = RequestMethod.GET) public String customer(ModelMap modelMap , @RequestParam Integer age) { modelMap.addAttribute("requestKey", "Hola!"); modelMap.addAttribute("age", age); return "c
@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>
<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>
顾客
${age}
request.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Customer</title>
</head>
<body>
<p>${age}</p>
</body>
</html>
<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>
请求主体
JSONTest=函数(){
$.ajax({
网址:“客户”,
键入:“获取”,
数据:{
姓名:“Okan”,
姓氏:“Pehlivan”,
年龄:22},
数据类型:“json”
});
};
${requestKey}
我的jQuery JSON网页
JSON
我想使用ajax获取请求,我有一些参数(name=Okan,例如)。我想在cust.jsp上显示任何参数。当我调试welcome()metod的代码时,age指定了my age。我将该值与其键“age”进行了映射。request.jsp文件未更改 Ajax调用不会更改视图。它将在success中返回同一页上的参数: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.jsp上返回,请求jsp不会更改,因为在AJAX调用之后,您不会在页面的任何地方更改它。 您确实发出了请求,但没有使用响应。 在ajax调用中添加一个success函数,然后读取其中的数据。 例如:
$.ajax({
url、数据、数据类型和方法不变,并添加以下内容:
成功:函数(responseText){
将responseText设置为您的
}
});代码>
为了在cust.jsp中显示年龄,需要在调用url时传递请求参数。比如:
自动装箱应该考虑请求参数的int
到Integer
转换,如果您看到异常,请尝试使用int
以下信息可能会有所帮助:
- 在jsp/javascript中使用${varName}时,需要模型
我们已经有了这些数据
- 考虑使用标记来控制动态输出
变量,它们可能会有所帮助
- 在进行ajax调用以请求数据时,以
处理程序并从那里继续您的逻辑
HTH.您能详细说明一下您在request.jsp中期望的是什么吗?它是referer或age值,或者两者都是??通过按钮,我想发送带有参数的request/request,并在cust.jsp中获取此参数。我将编辑我的request.jsp文件welcome()方法中的年龄值是多少?