转发视图,重定向视图,视图控制器,RESTful

相较于Thymeleaf的返回值,转发视图需要forward:这个前缀,来防止ThymeleafView渲染

重定向视图,它需要加上前缀redirect,它在进行页面跳转时会自动为所设置的绝对路径前面加上上下文路径

视图控制器:

首先,这里index爆红,但不影响使用,先空一个位置,日后查知再来补充

: 

其次,<mvc:annotation-driven/>的功能非常多,像是ajax也需要用到它,这里同样空一格,等待以后查知再来处理

 RESTful:

它是一种开发风格(REST风格)

  • 一切皆资源
  • REST风格提倡URL地址使用统一的风格设计,从前到后各个单词使用斜杠分开,不使用问号键值对的方式来携带请求参数,而是将要发送给服务器的数据作为URL地址的一部分,以保证整体风格的一致性(例如下方表格,/1后面的1其实是占位符,使用@PathVariable来获取数据)
操作传统方式REST风格
查询操作getUserById?Id=1user/1-->get请求方式
保存操作SaveUseruser-->post请求方式
删除操作DeleteUser?id=1user/1-->delete请求方式
更新操作updateUseruser-->put请求方式

在进行这些操作时

    @RequestMapping(value = "/user",method = RequestMethod.GET)
    public String getAllUser(){
        System.out.println("查询所有用户信息--》/user-->get");
        return "success";
    }

普通和之前所学一样,而带有参数的可以使用占位符来进行

    @RequestMapping(value = "/user/{id}",method = RequestMethod.GET)
    public String getUserById(@PathVariable("id") Integer id){
        System.out.println("根据id查询用户信息--》/user"+id+"--》get");
        return "success";
    }

POST和GET不带参数的方法一样

但是:浏览器目前只能发送GET和POST请求,若要发送put,delete请求,需要在web.xml中配置一个过滤器(如下方代码块所示)配置过滤器后还需满足两个条件,

1.当前请求必须为post

2.当前请求必须传输请求参数_method(例如过滤器代码块下方代码块)

<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>
<form th:action="@{/user}" method="post">
    <input type="hidden" name="_method" value="put">
    <input type="submit" value="修改用户信息">
</form>

在这里看源码:

protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
    HttpServletRequest requestToUse = request;
    if ("POST".equals(request.getMethod()这里的作用是检查是否是POST请求) && request.getAttribute("javax.servlet.error.exception") == null) {
        String paramValue = request.getParameter(this.methodParam获取请求参数,即_method的值);
        if (StringUtils.hasLength(paramValue)这里判断_method的值是否不为空或者不为空字符串) {
            String method = paramValue.toUpperCase(Locale.ENGLISH);
                这里的作用:将_method的值转换为大写
            if (ALLOWED_METHODS.contains(method)判断这里是否是大写的PUT,DELETE,PATCH) {
                requestToUse = new HttpMethodRequestWrapper(request, method);将请求对象替换后放行
            }
        }
    }

    filterChain.doFilter((ServletRequest)requestToUse, response);
}

java知识点碎片复习:

1. toUpperCase将小写字母转换为大写

toLowerCase将大写字母转换为小写

String  cc = “aBc123”.toUpperCase();结果就是:ABC123。

2.将资源共享到请求域中的五种方法

  • springAPI
  • model
  • map
  • ModelAndVue
  • ModelMap
    @RequestMapping(value = "/employee",method = RequestMethod.GET)
    public String getAllEmloyee(Model model){
        Collection<Employee> all = dao.getAll();
        model.addAttribute("allEmployee",all);
        return "employee_list";
    }

 这个就是控制层的代码步骤,调用dao中的方法,查询所有员工信息,然后将数据共享到请求域中,

1.springAPI

2.model

3.map

4.ModelAndVue

5.ModelMap

这里使用model

<table>
  <tr>
    <th colspan="5">employee_list</th>
  </tr>
  <tr>
    <th>id</th>
    <th>lastName</th>
    <th>email</th>
    <th>gender</th>
    <th>options</th>
  </tr>
  <tr th:each="employee:${allEmployee}">
    <td th:text="${employee.id}"></td>
    <td th:text="${employee.lastName}"></td>
    <td th:text="${employee.email}"></td>
    <td th:text="${employee.gender}"></td>
    <td>
      <a href="">delete</a>
      <a href="">update</a>
    </td>
  </tr>
</table>

加载静态资源:

当前工程的web.xml配置的前端控制器DispatcherServlet的url-pattern是/
tomcat的web.xml配置的defaultServlet的url-pattern也是/
此时,浏览器发送的请求会优先被DispatcherServlet进行处理,但是DispatcherServlet无法处理静态资源
若配置了<mvc:default-servlet-handler/>此时浏览器所有请求都会被DispatcherServlet进行处理
若配置了<mvc:default-servlet-handler/>和<mvc:annotation-driven/>此时浏览器所有请求都会先被DispatcherServlet进行处理,无法处理再交给DefaultServlet进行处理

  • 46
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值