【问题标题】:why didn't i get the referer parameter in Spring MVC为什么我在 Spring MVC 中没有得到 referer 参数
【发布时间】: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


【解决方案1】:

Ajax 调用不会改变视图。它将成功返回同一页面上的参数: 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

【讨论】:

    【解决方案2】:

    请求 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

    自动装箱应该处理请求参数的intInteger 的转换,如果您看到异常,请尝试使用int

    一些可能有帮助的信息:

    • 在 jsp/javascript 中使用 ${varName} 时,需要模型 已经有了这些数据。
    • 考虑使用标签来控制动态的输出 变量,它们可能会有所帮助。
    • 在进行 ajax 调用以请求数据时,以 处理程序并从那里继续您的逻辑。

    HTH。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-22
      • 2020-11-19
      • 2013-07-15
      • 1970-01-01
      • 1970-01-01
      • 2010-12-08
      • 1970-01-01
      相关资源
      最近更新 更多