美文网首页
Spring MVC 使用Restful风格的URL

Spring MVC 使用Restful风格的URL

作者: Scallion | 来源:发表于2021-08-18 11:13 被阅读0次

Restful 是一种web软件架构风格,它不是标准也不是协议,它倡导的是一个资源定位及资源操作的风格。Restful风格的url分别对应了GET、POST、PUT、DELETE 四种请求。一般使用时主要还是使用GET和POST请求,PUT和DELETE基本不会使用。

    GET:一般用于查询,获取资源;
    POST:一般用于增加,新建资源;
    PUT:一般用于更新,修改资源;
    DELETE:一般用于删除资源。

Restful风格的url中可以包含请求参数变量,SpringMVC的Handle可以使用@PathVariable注解从url中获取参数变量。
事例代码:
前端页面无法直接发送PUT和DELETE请求,如果想要使用PUT和DELETE请求,需要使用POST请求,并在请求中携带_method参数,在 _method参数中指定当前请求的方式是PUT还是DELETE。同时还需要在web.xml中配置请求方式过滤器,在请求方式过滤器中解析出_method参数,并将请求转换成对应的请求方式。

  • 前端 jsp 页面代码
<div>
    <h2>SpringMVC对Restful风格url的支持</h2>
    <fieldset>
        <p>测试用例:SpringMVC对Restful风格url的支持</p>
        <a href="/demo/handle/15">rest_get测试</a>
        <form method="post" action="/demo/handle">
            <input type="text" name="username"/>
            <input type="submit" value="提交rest_post请求"/>
        </form>
        <form method="post" action="/demo/handle/15/lisi">
            <input type="hidden" name="_method" value="put"/>
            <input type="submit" value="提交rest_put请求"/>
        </form>
        <form method="post" action="/demo/handle/15">
            <input type="hidden" name="_method" value="delete"/>
            <input type="submit" value="提交rest_delete请求"/>
        </form>
    </fieldset>
</div>
  • 后台 Handle 代码
@Controller
@RequestMapping("/demo")
public class DemoController {

    @RequestMapping(value = "/handle/{id}", method = {RequestMethod.GET})
    public ModelAndView handleGet(@PathVariable("id") int id) {
        //服务器时间
        Date date = new Date();
        //返回服务器时间到前端页面
        //封装了数据和页面信息的modelAndView
        ModelAndView modelAndView = new ModelAndView();
        //addObject 其实是向请求域中request.setAttribute("date",date)
        modelAndView.addObject("date", date);
        //视图信息(封装跳转的页面信息)
        modelAndView.setViewName("success");
        System.out.println("date: " + date);
        System.out.println("id: " + id);
        System.out.println("get 请求执行了......");
        return modelAndView;
    }

    @RequestMapping(value = "/handle", method = {RequestMethod.POST})
    public ModelAndView handlePost(String username) {
        //服务器时间
        Date date = new Date();
        //返回服务器时间到前端页面
        //封装了数据和页面信息的modelAndView
        ModelAndView modelAndView = new ModelAndView();
        //addObject 其实是向请求域中request.setAttribute("date",date)
        modelAndView.addObject("date", date);
        //视图信息(封装跳转的页面信息)
        modelAndView.setViewName("success");
        System.out.println("date: " + date);
        System.out.println("username: " + username);
        System.out.println("post 请求执行了......");
        return modelAndView;
    }

    @RequestMapping(value = "/handle/{id}/{username}", method = {RequestMethod.PUT})
    public ModelAndView handlePut(@PathVariable("id") int id , @PathVariable("username") String username) {
        //服务器时间
        Date date = new Date();
        //返回服务器时间到前端页面
        //封装了数据和页面信息的modelAndView
        ModelAndView modelAndView = new ModelAndView();
        //addObject 其实是向请求域中request.setAttribute("date",date)
        modelAndView.addObject("date", date);
        //视图信息(封装跳转的页面信息)
        modelAndView.setViewName("success");
        System.out.println("date: " + date);
        System.out.println("id: " + id);
        System.out.println("username: " + username);
        System.out.println("put 请求执行了......");
        return modelAndView;
    }

    @RequestMapping(value = "/handle/{id}", method = {RequestMethod.DELETE})
    public ModelAndView handleDelete(@PathVariable("id") int id) {
        //服务器时间
        Date date = new Date();
        //返回服务器时间到前端页面
        //封装了数据和页面信息的modelAndView
        ModelAndView modelAndView = new ModelAndView();
        //addObject 其实是向请求域中request.setAttribute("date",date)
        modelAndView.addObject("date", date);
        //视图信息(封装跳转的页面信息)
        modelAndView.setViewName("success");
        System.out.println("date: " + date);
        System.out.println("id: " + id);
        System.out.println("delete 请求执行了......");
        return modelAndView;
    }
}
  • web.xml配置请求方式过滤器
<!--springmvc提供的针对post请求的编码过滤器-->
<filter>
  <filter-name>encoding</filter-name>
  <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  <init-param>
    <param-name>encoding</param-name>
    <param-value>UTF-8</param-value>
  </init-param>
</filter>

<!--配置springmvc请求方式转换过滤器,会检查请求参数中是否有_method参数,如果有就 按照指定的请求方式进行转换-->
<filter>
  <filter-name>hiddenHttpMethodFilter</filter-name>
  <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>

<filter-mapping>
  <filter-name>encoding</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
  <filter-name>hiddenHttpMethodFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

相关文章

网友评论

      本文标题:Spring MVC 使用Restful风格的URL

      本文链接:https://www.haomeiwen.com/subject/rhkgbltx.html