SpringMVC HTTP状态码 405 - 请求方法'POST'不被支持

4
我有一个表单,从这个表单中查询数据库并将其发布到另一个页面以获取结果。然后,我从查询结果中选择一条记录,并将其带回到我进行查询的页面,以便我可以更新记录。
我点击更新按钮,它会将我带回到控制器和第一个调用的相同方法来进行查询,但是请求的参数现在是“更新”,因此应该进入方法中的更新条件。似乎我无法重新提交页面到相同的URL映射。 控制器
   @RequestMapping(value="citizen_registration.htm", method = RequestMethod.POST)
    public ModelAndView handleRequest(@Valid @ModelAttribute Citizens citizen, 
            BindingResult result,  ModelMap m, Model model,
            @RequestParam(value="user_request") String user_request) throws Exception {


        try{
             logger.debug("In Http method for CitizenRegistrationController - Punishment Registration");
             logger.debug("User Request Is " + user_request);


                 if(result.hasErrors()){

                //handle errors

                    // return new ModelAndView("citizen_registration");
                 }else{

                     //check if its a save or an update

                     if(user_request.equals("Save"))
                        //do save

                        else if (user_request.equals("Query"))

                        //do query

                        else if (user_request.equals("Update"))
                        //do update

                }
    }

错误日志
34789 [http-bio-8084-exec-3] DEBUG org.springframework.web.servlet.DispatcherServlet  - Bound request context to thread: org.apache.catalina.connector.RequestFacade@2ba3ece5
34791 [http-bio-8084-exec-3] DEBUG org.springframework.web.servlet.DispatcherServlet  - DispatcherServlet with name 'crimetrack' processing POST request for [/crimeTrack/getCitizen/citizen_registration.htm]
34792 [http-bio-8084-exec-3] DEBUG org.springframework.web.servlet.DispatcherServlet  - Testing handler map [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping@3a089b3] in DispatcherServlet with name 'crimetrack'
34792 [http-bio-8084-exec-3] DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping  - Looking up handler method for path /getCitizen/citizen_registration.htm
34796 [http-bio-8084-exec-3] DEBUG org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver  - Resolving exception from handler [null]: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported
34796 [http-bio-8084-exec-3] DEBUG org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver  - Resolving exception from handler [null]: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported
34796 [http-bio-8084-exec-3] DEBUG org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver  - Resolving exception from handler [null]: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported
34796 [http-bio-8084-exec-3] WARN  org.springframework.web.servlet.PageNotFound  - Request method 'POST' not supported
34796 [http-bio-8084-exec-3] DEBUG org.springframework.web.servlet.DispatcherServlet  - Null ModelAndView returned to DispatcherServlet with name 'crimetrack': assuming HandlerAdapter completed request handling
34796 [http-bio-8084-exec-3] DEBUG org.springframework.web.servlet.DispatcherServlet  - Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade@2ba3ece5
34796 [http-bio-8084-exec-3] DEBUG org.springframework.web.servlet.DispatcherServlet  - Successfully completed request
34796 [http-bio-8084-exec-3] DEBUG org.springframework.web.context.support.XmlWebApplicationContext  - Publishing event in WebApplicationContext for namespace 'crimetrack-servlet': ServletRequestHandledEvent: url=[/crimeTrack/getCitizen/citizen_registration.htm]; client=[127.0.0.1]; method=[POST]; servlet=[crimetrack]; session=[null]; user=[null]; time=[8ms]; status=[OK]
34796 [http-bio-8084-exec-3] DEBUG org.springframework.web.context.support.XmlWebApplicationContext  - Publishing event in Root WebApplicationContext: ServletRequestHandledEvent: url=[/crimeTrack/getCitizen/citizen_registration.htm]; client=[127.0.0.1]; method=[POST]; servlet=[crimetrack]; session=[null]; user=[null]; time=[8ms]; status=[OK]

FireBug

检查 FireBug 后,我得到了这个错误信息:"NetworkError: 405 Method Not Allowed - http://localhost:8084/crimeTrack/getCitizen/citizen_registration.htm",应该是这个链接:http://localhost:8084/crimeTrack/citizen_registration.htm"

getCitizen 是当查询第一次执行时我被带到的页面,它被包含在 URL 中。

我将 JSP 表单操作定义更改为 <form:form id="citizenRegistration" name ="citizenRegistration" method="POST" commandName="citizens" action="<%=request.getContextPath()%>/citizen_registration.htm">,然而当我启动应用程序并请求此页面时,我会得到:

HTTP Status 500 - /WEB-INF/jsp/citizen_registration.jsp (line: 31, column: 116) attribute for %>" is not properly terminated 

1
您的处理程序方法映射到citizen_registration.htm。您的控制器类是否映射到/getCitizen? - digitaljoel
@digitaljoel 是的,/getCitizen 有一个单独的映射,但我想要访问的映射是 /citizen_registration,但我看到 getCitizen 进入了 URL,我知道这与 ContextPath 有关,但我不确定我是否配置正确。我更新了问题,在 jsp 中显示了配置。 - devdar
1
@dev_darin,我怀疑问题可能出在action url上。在jsp中尝试这样写:<%=request.getContextPath()%>/getCitizen/citizen_registration.htm"> - Mallikarjuna Reddy
1
@dev_darin,最好尝试在脚本中提交表单。在控制器中,您不需要提及htm扩展名,因为它是可选的,请交叉验证web.xml。 - Mallikarjuna Reddy
@GMR <servlet-mapping><servlet-name>crimetrack</servlet-name><url-pattern>*.htm</url-pattern></servlet-mapping>的翻译。 - devdar
显示剩余3条评论
1个回答

2
function submitPage(){
     document.getElementById("citizenRegistration").action="getCitizen/citizen_registration.htm";
     document.getElementById("citizenRegistration").target="_self";    
     document.getElementById("citizenRegistration").method = "POST";
     document.getElementById("citizenRegistration").submit();
}

您可以按照上述提到的方法进行调用。使用onclick绑定提交按钮的函数。

我需要一些帮助,因为我没有使用过太多的jQuery。当我点击更新按钮时,我希望页面提交到citizen_registration URL控制器。这是我在citizen_registration页面上拥有的内容。$('#update').bind('click', function() { submitPage(); }); 更新是一个提交按钮,这样会起作用吗? - devdar
1
是的,您可以做相同的事情,也可以直接在onclick属性中提及,例如onclick="submitPage()" - Mallikarjuna Reddy
1
这是一个JS内置函数。那将是请求。它与单击功能相同。请检查一下.. http://www.javascript-coder.com/javascript-form/javascript-form-submit.phtml - Mallikarjuna Reddy
我只有一个问题,就是如何获取已发布的按钮值,因为我正在阻止按钮的默认行为,并且我正在使用request.getParameter来获取按钮值。 - devdar
您能帮我看看这个问题吗?http://stackoverflow.com/questions/15746371/springmvc-custom-collection-editor-not-returning-data-to-jsp?noredirect=1#comment22402937_15746371 - devdar
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接