提交获得HTTP状态405后的@RequestMapping(method = RequestMethod.POST) - 请求方法'POST'

时间:2016-11-21 05:39:47

标签: java spring spring-mvc

嗨我在这个控制器中有一个有线的行为。我有两种方法获取准备以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 throw 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

感谢!!!

3 个答案:

答案 0 :(得分:1)

从控制器中删除@RequestMapping(value =&#34; / admin / notification / sendNotification&#34;)

答案 1 :(得分:1)

注释@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);
    }
}

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

答案 2 :(得分:1)

我修复了添加一个方法,即成功(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;
}