【问题标题】:On returning ModelAndView in Spring, the url is not changed在 Spring 中返回 ModelAndView 时,url 没有改变
【发布时间】:2020-03-22 13:07:06
【问题描述】:

我试图理解为什么在返回 new ModelAndView() 后,url 没有改变。这是我的代码:

<div class="container" id="container">
   <button id="test1" onclick="showSchedule(this.id)">Test button</button>
</div>
<script>
  function showSchedule(buttonName) {
    $.ajax({
      url: "/appointment/employee/" + buttonName,
      type: "GET",
      dataType: 'html',
      success: function (response) {
        $("#container").html(response);
      },
      error: function (response) {
        console.log(response);
      }
    });
  }

这是捕获该请求的控制器:

@RequestMapping("/appointment")
public class AppointmentController {

  @GetMapping(value = "/employee/{scheduleName}")
  public @ResponseBody
  ModelAndView showSchedule(@PathVariable("scheduleName") String scheduleName,
                            Model model,
                            HttpServletRequest request) throws IOException {
    model.addAttribute("scheduleName", scheduleName);
    ModelAndView mav = new ModelAndView(new MappingJackson2JsonView());
    String url = request.getRequestURL().toString();
    mav.addObject("url", url);
    mav.addObject("view", "renderAppointments");
    mav.setViewName("renderAppointments");
    return mav;
  }
}

在这种情况下,jsp 被渲染,但 url 没有改变。如果我将 dataType 更改为 json,我会收到所有需要的信息并可以更改 url,但是 jsp 不会呈现

【问题讨论】:

    标签: jquery ajax spring spring-boot jsp


    【解决方案1】:

    您正在执行 ajax 调用 ($.ajax)。

    这不会更改您的 URL,因为它是一个异步 Javascript 调用 - 因此,这是预期的行为。

    我不知道你到底想在这里做什么,但这会工作,没有你所有的 Javascript。

    <form action="/appointment/employee/test1">
        <input type="submit" value="Test button" />
    </form>
    

    【讨论】:

    • 那么你能告诉我我应该怎么做才能改变它吗?
    猜你喜欢
    • 2018-12-20
    • 1970-01-01
    • 2015-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-24
    • 2013-12-31
    相关资源
    最近更新 更多