简体   繁体   English

405不允许使用方法http方法:使用Spring MVC时不支持请求方法'GET'。

[英]405 Method Not Allowed http method being used: Request method 'GET' not supported` while using Spring MVC?

I am working with Spring MVC pattern and I am trying to make a JSP form which is like this as of now - 我正在使用Spring MVC模式,目前正在尝试制作这样的JSP表单-

In the form, I have four rows, first row is just for labelling and other three rows I need to put my data in the text box. 在表单中,我有四行,第一行仅用于标记,其他三行需要将数据放入文本框。 For example- for DC1, I will insert numServers value in the textbox, I will insert ipaddress value in the textbox and hostname value in the textbox and same with dc2 and dc3 例如,对于DC1,我将在文本框中插入numServers值,在文本框中插入ipaddress值,在文本框中插入hostname ,并与dc2dc3相同

<form method="post" enctype="multipart/form-data">
    <table>
        <tr>
            <td>Datacenter Name</td>
            <td>Number of Servers</td>
            <td>IP Address(comma separated)</td>
            <td>Host Name(comma separated)</td>
        </tr>
        <tr>
            <td><label for="dc1">DC1</label></td>
            <input type="hidden" name="datacenters[0].name" value="DC1"/>
            <td><input type="text" name="datacenter[0].numServers" size="20"></td>
            <td><input type="text" name="datacenter[0].ipAddress" size="60"></td>             
            <td><input type="text" name="datacenter[0].hostName"  size="60"></td>
        </tr>
        <tr>
            <td><label for="dc2">DC2</label></td>
            <input type="hidden" name="datacenters[1].name" value="DC2"/>
            <td><input type="text" name="datacenter[1].numServers" size="20"></td>
            <td><input type="text" name="datacenter[1].ipAddress" size="60"></td>             
            <td><input type="text" name="datacenter[1].hostName"  size="60"></td>
        </tr>
        <tr>
            <td><label for="dc3">DC3</label></td>
            <input type="hidden" name="datacenters[2].name" value="DC3"/>
            <td><input type="text" name="datacenter[2].numServers" size="20"></td>
            <td><input type="text" name="datacenter[2].ipAddress" size="60"></td>             
            <td><input type="text" name="datacenter[2].hostName"  size="60"></td>
        </tr>
        <tr>
            <td colspan="2">&nbsp;</td>
        </tr>
    </table>
    <input type="submit">
</form>

Now I have defined a model to hold this entire information: 现在,我定义了一个模型来保存全部信息:

class Datacenter {

    private String name;
    private String ipAddress;
    private String hostName;
    private String numServers;
    // Add getters and setters..
}


public class Datacenters {
    private List<Datacenter> datacenters;
    //Getters and setters..
}

Now I am supposed to read these values after hitting the submit button as I will be typing necessary values in the textbox. 现在,我应该在点击“提交”按钮之后读取这些值,因为我将在文本框中键入必要的值。 I am using RequestMapping in my below code which is binding the above model model to my Controller class this way: 我在下面的代码中使用RequestMapping ,该代码将上述模型模型以这种方式绑定到我的Controller类:

@RequestMapping(value = "test", method = RequestMethod.POST)
public HashMap<String, String> testRequest(Datacenters dataCenters) {

}

But somehow whenever I am hitting the URL http://127.0.0.1:8080/dataweb/test , I am always getting this exception - 但是无论如何,只要我访问URL http://127.0.0.1:8080/dataweb/test ,我总是会收到此异常-

Error Message : Request method 'GET' not supported . 405 Method Not Allowed http method being used: GET. Request method 'GET' not supported

I am not sure what does it mean and how to fix it? 我不确定这是什么意思,以及如何解决? Any thoughts what wrong I would be doing here? 有什么想法我在这里会做错什么吗?

Below is my web.xml file - 以下是我的web.xml文件-

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>p13nzookweb</display-name>
  <listener>
    <listener-class>com.host.webres.resource.env.hostResourceRuntimeListener</listener-class>
  </listener>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
    <welcome-file>index</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/spring/context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/dataweb/*</url-pattern>
  </servlet-mapping>
</web-app>

If you are hitting the url 如果您点击网址

 http://127.0.0.1:8080/dataweb/test 

from the browser, you are making a GET request. 从浏览器中,您正在发出GET请求。

For this you need to specify a request mapping in your controller like this: 为此,您需要像这样在控制器中指定请求映射:

@RequestMapping(value = "test", method = RequestMethod.GET)
public String testRequestGetPage(ModelMap model) {
     // display the page you want to see
     model.addAttribute("DataCenter", new DataCenter());
     return "dataCenter.jsp";
}

Now to handle the form submit, you need to specify the action. 现在要处理表单提交,您需要指定操作。 In addition Spring will need the commandName, to map the model to the inputs. 另外,Spring将需要commandName来将模型映射到输入。 Change your form tag to look something like this. 更改表单标签,使其看起来像这样。 You will also need to change your form to a Spring style. 您还需要将表单更改为Spring样式。 Ex: 例如:

<form:form commandName="dataCenters" method="post" action="test">

In your post mapping, get the model: 在您的帖子映射中,获取模型:

@RequestMapping(value = "test", method = RequestMethod.POST)
public String testRequest(@ModelAttribute("DataCenter")DataCenter dataCenter) {

 // do something with dataCenter
 return "redirect:someNewUrl";
}

Your Parameter is Datacenters dataCenters . 您的参数是Datacenters dataCenters。

You need to use @ModelAttribute for your bean to be used in the form. 您需要将@ModelAttribute用于您的bean,以便在表单中使用它。 You may refer to this http://forum.spring.io/forum/spring-projects/web/87774-list-form-binding-via-modelattribute Or search online for an example using @ModelAttribute. 您可以参考http://forum.spring.io/forum/spring-projects/web/87774-list-form-b​​inding-via-modelattribute或使用@ModelAttribute在网上搜索示例。

Your form does not include the action you want to use. 您的形式不包括action要使用。 You need to send the form to your test action: 您需要将表单发送到您的test操作:

<form method="post" enctype="multipart/form-data" action="test">

It wasn't clear from the question but it sounds like you called http://127.0.0.1:8080/dataweb/test from the browser (which is a GET operation). 这个问题尚不清楚,但听起来您是从浏览器中调用了http://127.0.0.1:8080/dataweb/test (这是GET操作)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 405不允许的方法:请求方法&#39;POST&#39;不支持| Ajax / Spring MVC - 405 Method Not Allowed : Request method 'POST' not supported | Ajax/Spring MVC HTTP状态405-Spring MVC不支持请求方法&#39;POST&#39; - HTTP Status 405 - Request method 'POST' not supported Spring MVC HTTP 状态 405 - 不支持请求方法“POST”(Spring MVC) - HTTP Status 405 - Request method 'POST' not supported (Spring MVC) Spring MVC - HTTP 状态 405 - 不支持请求方法“POST” - Spring MVC - HTTP Status 405 - Request method 'POST' not supported Spring MVC HTTP状态405-不支持请求方法“ POST” - Spring MVC HTTP Status 405 - Request method 'POST' not supported Spring MVC 请求方法“POST”不支持-&gt; HTTP 405 - Spring MVC Request method 'POST' not supported -> HTTP 405 Spring MVC:HTTP 405-发出POST请求时不支持请求方法“ GET” - Spring MVC: HTTP 405 - Request method 'GET' not supported when making POST request HTTP状态405-在Spring MVC中执行返回“重定向:*。*”时,不支持请求方法“ GET” - HTTP Status 405 - Request method 'GET' not supported when doing return “redirect:*.*” in spring mvc HTTP状态405 - 不支持请求方法“GET” - HTTP Status 405 - Request method 'GET' not supported 出现意外错误(类型=不允许的方法,状态=405)。 spring 引导不支持请求方法“GET” - There was an unexpected error (type=Method Not Allowed, status=405). Request method 'GET' not supported with spring boot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM