【问题标题】:@RequestMapping( method = RequestMethod.POST) after submit got HTTP Status 405 - Request method 'POST'@RequestMapping(method = RequestMethod.POST) 提交后获得 HTTP 状态 405 - 请求方法“POST”
【发布时间】:2016-11-21 14:57:03
【问题描述】:

您好,我在这个控制器中有一个有线行为。我有两种方法获取准备信息以显示在 html 表单和 POST 以获取提交:

控制器:

@Controller
public class NotificationController {

    final String  JSP_NOTIFICATION_01="pages/sendPush/createNotification";
    final String  JSP_NOTIFICATION_02="pages/sendPush/createNotificationStep2";


    @RequestMapping(value ="/admin/notification/newNotification",method = RequestMethod.GET)
    public String newNotification( Map<String, Object> model, HttpServletRequest request) {

        //prepare  info to fill html form

        request.getSession().setAttribute("notificacion", notification);
        return JSP_NOTIFICATION_01;
    }


    @RequestMapping( value ="/admin/notification/sendNotification", method = RequestMethod.POST)
    public String saveNotification(@ModelAttribute("notForm") SendNotificationModel notForm,
                                      Map<String, Object> model,HttpServletRequest request) {


        //Get all information from HTML form

        System.out.println("llego.."+resultado);

        model.put("resultado", resultado);

        return JSP_NOTIFICATION_02;
    }   
}

JSP

        <form:form action="${pageContext.request.contextPath}/admin/notification/sendNotification" method="post" commandName="notForm">
                <form:hidden path="clientName"  />
                <form:hidden path="clientCode"  />
            </tr>
            <tr>
                <td>topics:</td>
                <td><form:select path="topics" items="${topicList}" /></td>
            </tr>
            <tr>
                <td>users:</td>
                <td><form:select  multiple="true" path="users" items="${userList}"  /></td>
            </tr>
            <tr>
                <td>Tipo de despliege :</td>
                <td><form:select path="tipoNotificacion" items="${tipoNotificacionList}" /></td>
            </tr>

        </table>
            <tr>
                <td colspan="2" align="center"><input type="submit" value="Enviar" /></td>
            </tr>
        </table>

    </form:form>

提交POST方法后一如既往的接收请求,但是返回spring后抛出405错误:

HTTP Status 405 - Request method 'POST' not supported
type Status report
message Request method 'POST' not supported
description The specified HTTP method is not allowed for the requested resource.

我正在使用 Spring 4.1.3 和 tomcat8

谢谢!!!

【问题讨论】:

  • 确保为您的 post 方法提供准确的参数
  • @koutuk 那会有什么不同?为什么错误的参数会导致 405?

标签: java spring spring-mvc


【解决方案1】:

从控制器中移除 @RequestMapping(value ="/admin/notification/sendNotification")

【讨论】:

  • 对不起,我尝试在类中使用全局 RequestMapping 并在每个方法中分开
  • @kuhajeyan 先生,你是救命稻草,我一直在努力寻找解决方案几个小时,这个简单的技巧做到了,非常感谢!
【解决方案2】:

注解@RequestMapping 可以在类和方法级别使用。在类级别使用时,它适用于所有方法。例如看下面的代码。

@Controller
@RequestMapping("/appointments")
public class AppointmentsController {
    private final AppointmentBook appointmentBook;
    @RequestMapping(method = RequestMethod.GET)
    public Map<String, Appointment> get() {
        return appointmentBook.getAppointmentsForToday();
    }
    @RequestMapping(path = "/{day}", method = RequestMethod.POST)
    public Map<String, Appointment> getForDay(@PathVariable @DateTimeFormat(iso=ISO.DATE) Date day, Model model) {
        return appointmentBook.getAppointmentsForDay(day);
    }
}

在上面的控制器代码中 /appointments 是相对的,它适用于下面的所有方法。方法开始处的任何 @RequestMapping 都会添加到相对路径中。但是请注意,@RequestMapping 在类级别不是强制性的。请阅读以下官方文档以进一步了解。 http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html

【讨论】:

    【解决方案3】:

    我修复了添加一种方法,成功 (GET)。在 POST 方法中,我进行了重定向并且可以正常工作

    参考http://howtodoinjava.com/spring/spring-mvc/spring-mvc-display-validate-and-submit-form-example/

    @RequestMapping( value ="/admin/notification/sendNotification", method = RequestMethod.POST)
    public String saveNotification(@ModelAttribute("notForm") SendNotificationModel notForm,
                                      Map<String, Object> model,HttpServletRequest request) {
    
        NotificationDetails newNot = new NotificationDetails();//(NotificationDetails)request.getSession().getAttribute("notificacion");
    
        String resultado= "Probando" ;
        System.out.println("llego.."+resultado);
    
        model.put("resultado", resultado);
    
        return "redirect:/admin/notification/sendNotification/success";
    }
    
    
    @RequestMapping(value = "/admin/notification/sendNotification/success", method = RequestMethod.GET)
    public String success(Model model)
    {
        return JSP_NOTIFICATION_02;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-03
      • 2016-11-11
      • 2015-06-26
      • 1970-01-01
      • 2012-06-24
      • 2015-05-01
      相关资源
      最近更新 更多