加载资源失败:服务器响应状态为405 | Spring MVC
问题描述:
当我使用@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卡姆兰
答
添加端点URL值请求映射
@RequestMapping(value="/index", method = RequestMethod.POST)
这是因为邮政法宣映射到所有路径既是方法和控制器类没有那价值
它已经在那里。/index工作得很好。问题只与另一种方法。当我删除其他方法的事情开始工作良好。其他方法有什么问题? –
看来你没有正确阅读我的答案。如果声明一个Controller类和一个没有RequestMapping值属性的方法,spring会将所有路径映射到这个类和方法。您已将“/ index”添加到GET方法。添加“/ index”或任何其他值POST方法 –
明白了你的观点。问题是我想要一个不应绑定到任何特定路径的泛型方法。原因我想创建一个多步向导风格的应用程序,并且在页面的每次post调用之后,我将在此方法内部添加逻辑来处理请求。有什么办法可以做到吗? –