【问题标题】:SpringMVC : Throws 415 Unsupported Media Type while attempting POST request call for redirectionSpringMVC:尝试 POST 请求重定向时抛出 415 Unsupported Media Type
【发布时间】:2017-04-30 20:23:55
【问题描述】:

浏览器响应如下:

服务器拒绝此请求,因为请求实体的格式不受所请求方法的请求资源支持。

而不是重定向到主页(home.jsp)

welcome.jsp 文件:

<form method ="POST" action = "<c:url value='/login'/>" >

       <input id="name" name="name" type="text">
       <input id="password" name="password" type="password">
       <input type="submit" id="loginNew" value="LoginNew"> 
  </form>

控制器类:

@RequestMapping( value="/login" , method = RequestMethod.POST )
    public ModelAndView authenticate ( @RequestBody User userObj ) throws Exception {
       ModelAndView modelAndView = new ModelAndView ();
        try {
            user = userService.authenticateUser ( userObj );
        } catch ( Exception e ) {
            e.printStackTrace();
         }
        modelAndView.setViewName("/home");
        return modelAndView;
    }

【问题讨论】:

    标签: jsp spring-mvc java-8 tomcat8 spring-4


    【解决方案1】:

    通过在方法参数中添加 @ModelAttribute 而不是 @RequestBody 解决了问题,然后按预期重定向到 home.jsp 页面。

    @RequestMapping( value="/login" , method = RequestMethod.POST )
            public ModelAndView authenticate ( @ModelAttribute User userObj ) throws Exception {
               ModelAndView modelAndView = new ModelAndView ();
                try {
                    user = userService.authenticateUser ( userObj );
                } catch ( Exception e ) {
                    e.printStackTrace();
                 }
                modelAndView.setViewName("/home");
                return modelAndView;
            }
    

    【讨论】:

      猜你喜欢
      • 2021-07-16
      • 1970-01-01
      • 2012-04-19
      • 1970-01-01
      • 2019-08-21
      • 2020-01-02
      • 2018-12-06
      • 2012-07-14
      • 1970-01-01
      相关资源
      最近更新 更多