當我使用@RequestMapping(method = RequestMethod.POST)註釋向控制器類添加方法時,Im正面臨一個問題。問題是當我添加此方法並啓動應用程序時。應用程序無法加載資源(CSS,JS等)。在瀏覽器中,我得到: 加載資源失敗:服務器響應的狀態爲405(方法不允許) 並在運行日誌中我收到此消息: org.springframework.web.servlet.PageNotFound handleHttpRequestMethodNotSupported 警告:不支持請求方法'GET'加載資源失敗:服務器響應狀態爲405 | Spring MVC
當我從控制器類中刪除此方法時,所有工作正常。不知道爲什麼會發生這種情況。我確定它與Dispatcher Servlet映射中的配置或資源無關,因爲如果沒有這種方法,每件事情都可以很好地工作。
我需要使用這種方法,因爲一些業務需求,否則是不可能的。
任何機構都可以幫助我確定問題出在哪裏。
@Controller
public class InvoiceController
{
@RequestMapping(value = "/index", method = RequestMethod.GET)
public ModelAndView adminPage() {
String username;
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
username = auth.getName(); //get logged in username
username.toUpperCase();
// Logic to build menue based on the Role of the user.
HashMap hm = new HashMap();
OrderItems od = new OrderItems();
ModelAndView model = new ModelAndView();
model.addObject("usr", username);
//Set the object for Menue Links on the Page
model.addObject("mnue", hm);
model.setViewName("index");
model.addObject("odi",od);
return model;
}
@RequestMapping(method = RequestMethod.POST)
public String save(HttpServletRequest request,
HttpServletResponse response,
Model model)
{
System.out.println("Im here in Genaric Post Method");
return null;
}
}
請注意,我使用Spring Security配置。與安全有什麼關係?或者控制器類配置有問題。
預先感謝您的期待。
問候,
d卡姆蘭
它已經在那裏。/index工作得很好。問題只與另一種方法。當我刪除其他方法的事情開始工作良好。其他方法有什麼問題? –
看來你沒有正確閱讀我的答案。如果聲明一個Controller類和一個沒有RequestMapping值屬性的方法,spring會將所有路徑映射到這個類和方法。您已將「/ index」添加到GET方法。添加「/ index」或任何其他值POST方法 –
明白了你的觀點。問題是我想要一個不應綁定到任何特定路徑的泛型方法。原因我想創建一個多步向導風格的應用程序,並且在頁面的每次post調用之後,我將在此方法內部添加邏輯來處理請求。有什麼辦法可以做到嗎? –