2016-11-21 29 views
1

您好,我在這個控制器有一個有線行爲。我有兩種方法獲取準備的資料在html表單和POST顯示獲得德提出:@RequestMapping(method = RequestMethod.POST)提交後得到HTTP狀態405 - 請求方法'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方法後一如既往接到請求,但回覆彈簧後拋出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

謝謝!!!

+0

確保你提供確切的參數,您的帖子方法 – koutuk

+0

@koutuk將有什麼不同?爲什麼錯誤的參數會導致405? –

回答

1

刪除@RequestMapping(值= 「/管理/通知/ sendNotification時」)從控制器

+0

對不起,我嘗試在類中使用全局的RequestMapping,並在每種方法中分開 –

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

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; 
} 
相關問題