405 Method Not Allowed http method being used: Req

2019-09-05 17:13发布

问题:

I am working with Spring MVC pattern and I am trying to make a JSP form which is like this as of now -

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

<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(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 -

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 -

<?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>

回答1:

If you are hitting the url

 http://127.0.0.1:8080/dataweb/test 

from the browser, you are making a GET request.

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. Change your form tag to look something like this. You will also need to change your form to a Spring style. 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";
}


回答2:

Your form does not include the action you want to use. You need to send the form to your test action:

<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).



回答3:

Your Parameter is Datacenters dataCenters .

You need to use @ModelAttribute for your bean to be used in the form. 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.