HTTP状态405 – Spring MVC中不支持请求方法“POST”

我使用freemarker模板作为视图部分创建了一个spring mvc应用程序。 在这尝试使用forms添加模型。我也使用spring security这里是代码

employee.ftl

Add Employee
Firstname:
Employee Code:

employeeController.java

 @RequestMapping(value = "/addEmployee", method = RequestMethod.POST) public String addEmployee(@ModelAttribute("employee") Employee employee) { employeeService.add(employee); return "employee"; } 

web.xml中

     appServlet org.springframework.web.servlet.DispatcherServlet 1   appServlet /   org.springframework.web.context.ContextLoaderListener   contextConfigLocation  /WEB-INF/spring/appServlet/servlet-context.xml, /WEB-INF/spring/springsecurity-servlet.xml     springSecurityFilterChain org.springframework.web.filter.DelegatingFilterProxy   springSecurityFilterChain /*   

弹簧security.xml文件

                    

单击提交按钮时,它返回错误`

HTTP状态405 – 不支持请求方法“POST”

`我在ftl和controller上都给了POST方法。 那么为什么会这样呢?

我不确定这是否有帮助,但我遇到了同样的问题。

您正在使用带有CSRF保护的springSecurityFilterChain。 这意味着您必须在通过POST请求发送表单时发送令牌。 尝试将下一个输入添加到表单:

  

据我所知,上述解决方案不适用于最新的SpringSecurity。 您可以通过以下操作URL发送它,而不是通过隐藏传递:

 

我找到了解决方案。 这是因为春季安全跨站请求伪造(CSRF)保护。 它会阻止url。 所以我在表单中添加了一个额外的字段。

  

现在它正常运作。

尝试替换:

 action="addEmployee" 

有:

 action="${pageContext.request.contextPath}/addEmployee" 

除非您使用的是Spring 3.2

看到XML后编辑:

尝试将servlet-context.xml移动到WEB-INF目录并将其重命名为“appServlet-context.xml”。 然后删除该行:

 /WEB-INF/spring/appServlet/servlet-context.xml, 

来自web.xml中的contextConfigLocation。

惯例是上下文xml文件名为'[servlet-name] -context.xml’,其中[servlet-name]是DispatcherServlet的名称。

还尝试在表单操作中添加“/”,因此:

 action="/addEmployee" 

这对我有用:

 .and().csrf().disable(); 

另一种解决方案(但每种forms)