java - SpringMVC:HTTP Status 405 - JSPs only permit GET POST or HEAD
PHP中文网
PHP中文网 2017-04-17 17:45:45
0
3
794

跟着网上的视频敲示例代码,然后就报错了...
Web页面代码:

<!-- REST PUT -->
<form action="test/restPut/2" method="post">
  <input type="hidden" name="_method" value="PUT"/>
  <input type="submit" value="REST PUT" />
</form>

Controller代码:

@Controller
@RequestMapping("/test")
public class SpringRequestMapping
{
    // 成员属性;
    private final String SUCCESS = "success";
    
    /**
     * 功能描述:测试RESTful PUT;
     * @param id
     * @return
     */
    @RequestMapping(value = "/restPut/{id}", method = RequestMethod.PUT)
    public String restPut(@PathVariable("id") Integer id)
    {
        System.out.println("RESTful PUT:" + id);
        return SUCCESS;
    }
    
}

web.xml

<filter>
    <filter-name>HiddenHttpMethodFilter</filter-name>
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>    
  
<filter-mapping>
  <filter-name>HiddenHttpMethodFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

新手求助,麻烦大神指点一二,问题出在哪了???

PHP中文网
PHP中文网

认证高级PHP讲师

reply all(3)
PHPzhong

The /restPut/{id} interface you wrote is a restful style interface, and judging from the name of this interface, it is a data interface. What you return in this method is a jsp page, so spring thinks it is a jsp based on the returned content. Page interface, and clearly tells you that the jsp interface can only use the get post method in http.

If you really want to return data, such as json data, add @ResponseBody under the @RequestMapping annotation, and then return SUCCESS becomes return "{msg:"hello noob!"}"
Otherwise, don’t use the put method on your interface. Don’t name the put method either

洪涛

Specify the servlet name of mvc

<filter-mapping>

<filter-name>HiddenHttpMethodFilter</filter-name>
<servlet-name>mcpMvc</servlet-name>

</filter-mapping>

巴扎黑

The submission method of your form is post, but when configuring the mapping, set it to RequestMethod.PUT. Check to see if this is the problem.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!