SpringBoot笔记(4)

SpringBoot笔记(4)

一、请求处理

1.1 常用参数注解使用

注解 使用
@PathVariable 获取URI模板指定请求,并赋值到变量中,不指定可以将所有请求放到map中,但是健值都为String
@RequestHeader 获取指定请求头,不指定可以获取所有到map中
@RequestParam 用于获取简单数据类型的参数如StringList<Integer>
@CookieValue 获取cookie的值,String或Cookie类型
@RequestBody 获取请求体数据
@ResposeBody 将对象转换为json格式并写入response的body区中
@RequestAttribute 相当于request.getAttribute(), 获取请求域中的值

矩阵变量:参数之间用;分割

​ SpringBoot默认不开启矩阵变量, 需要实现或重写webMvcConfigurer,将urlPathHelper的setRemoveSemicolonContent 设置为false

​ 可用于重写url,cookie被禁用时传入jesessionId

​ 参数前面要用{}的路径变量【/cars/{path};low=20;band=zhangsan,lisi】

二、视图解析器与模板引擎

1.模板引擎-Thymeleaf

1、thymeleaf简介

Thymeleaf is a modern server-side Java template engine for both web and standalone environments, capable of processing HTML, XML, JavaScript, CSS and even plain text.

现代化、服务端Java模板引擎

2、基本语法

1、表达式

表达式名字 语法 用途
变量取值 ${…} 获取请求域、session域、对象等值
选择变量 *{…} 获取上下文对象值
消息 #{…} 获取国际化等值
链接 @{…} 生成链接
片段表达式 ~{…} jsp:include 作用,引入公共页面片段

2、字面量

文本值: ‘one text’ , ‘Another one!’ ,…数字: 0 , 34 , 3.0 , 12.3 ,…布尔值: true , false

空值: null

变量: one,two,…. 变量不能有空格

3、文本操作

字符串拼接: +

变量替换: |The name is ${name}|

4、数学运算

运算符: + , – , * , / , %

5、布尔运算

运算符: and , or

一元运算: ! , not

**
**

6、比较运算

比较: > , < , >= , <= ( gt , lt , ge , le )等式: == , != ( eq , ne )

7、条件运算

If-then: (if) ? (then)

If-then-else: (if) ? (then) : (else)

Default: (value) ?: (defaultvalue)

8、特殊操作

无操作: _

3、设置属性值-th:attr

设置单个值

<form action="subscribe.html" th:attr="action=@{/subscribe}">
  <fieldset>
    <input type="text" name="email" />
    <input type="submit" value="Subscribe!" th:attr="value=#{subscribe.submit}"/>
  </fieldset>
</form>

设置多个值

<img src="../../images/gtvglogo.png"  th:attr="src=@{/images/gtvglogo.png},title=#{logo},alt=#{logo}" />

以上两个的代替写法 th:xxxx

<input type="submit" value="Subscribe!" th:value="#{subscribe.submit}"/>
<form action="subscribe.html" th:action="@{/subscribe}">

所有h5兼容的标签写法

https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#setting-value-to-specific-attributes

4、迭代

<tr th:each="prod : ${prods}">
        <td th:text="${prod.name}">Onions</td>
        <td th:text="${prod.price}">2.41</td>
        <td th:text="${prod.inStock}? #{true} : #{false}">yes</td>
</tr>
<tr th:each="prod,iterStat : ${prods}" th:class="${iterStat.odd}? 'odd'">
  <td th:text="${prod.name}">Onions</td>
  <td th:text="${prod.price}">2.41</td>
  <td th:text="${prod.inStock}? #{true} : #{false}">yes</td>
</tr>

5、条件运算

<a href="comments.html"
th:href="@{/product/comments(prodId=${prod.id})}"
th:if="${not #lists.isEmpty(prod.comments)}">view</a>
<div th:switch="${user.role}">
  <p th:case="'admin'">User is an administrator</p>
  <p th:case="#{roles.manager}">User is a manager</p>
  <p th:case="*">User is some other thing</p>
</div>

6、属性优先级

<span>SpringBoot笔记(4)</span>

2.Thymeleaf的使用

1、引入Starter

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

SpringBoot已经自动配置好了,

1、所有thymeleaf的配置值都在 ThymeleafProperties

2、配置好了 SpringTemplateEngine

3、配好了 ThymeleafViewResolver

4、我们只需要直接开发页面

放在templates下,后缀为html

    public static final String DEFAULT_PREFIX = "classpath:/templates/";

    public static final String DEFAULT_SUFFIX = ".html";  //xxx.html

5、导入命名空间

2、编写controller层

@Controller
public class ViewTestController {
    @RequestMapping("/success")
    public String ViewTest(Model model){
        model.addAttribute("msg","郜宇博,你好"); 
        model.addAttribute("link","http://www.baidu.com");
        //已经自动配置添加后缀
        return "success";
    }
}

3、编写页面

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1 th:text="${msg}">nihao</h1>
    <a href="www.hao123.com" th:href="${link}">百度</a>
</body>
</html>

三、 拦截器

0、拦截器原理

1、根据当前请求,找到HandlerExecutionChain【可以处理请求的handler以及handler的所有 拦截器】

2、先来顺序执行 所有拦截器的 preHandle方法

  • 1、如果当前拦截器prehandler返回为true。则执行下一个拦截器的preHandle
  • 2、如果当前拦截器返回为false。直接 倒序执行所有已经执行了的拦截器的 afterCompletion;

3、如果任何一个拦截器返回false。直接跳出不执行目标方法

4、所有拦截器都返回True。执行目标方法

5、倒序执行所有拦截器的postHandle方法。

6、前面的步骤有任何异常都会直接倒序触发 afterCompletion

7、页面成功渲染完成以后,也会倒序触发 afterCompletion

<span>SpringBoot笔记(4)</span>

1、 HandlerInterceptor接口

/**
 * 登录检查
 * 1、配置好拦截器要拦截哪些请求
 * 2、把这些配置放在容器中
 */
@Slf4j
public class LoginInterceptor implements HandlerInterceptor {

    /**
     * 目标方法执行之前
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        String requestURI = request.getRequestURI();
        log.info("preHandle拦截的请求路径是{}",requestURI);

        //登录检查逻辑
        HttpSession session = request.getSession();

        Object loginUser = session.getAttribute("loginUser");

        if(loginUser != null){
            //放行
            return true;
        }

        //拦截住。未登录。跳转到登录页
        request.setAttribute("msg","请先登录");
//        re.sendRedirect("/");
        request.getRequestDispatcher("/").forward(request,response);
        return false;
    }

    /**
     * 目标方法执行完成以后
     * @param request
     * @param response
     * @param handler
     * @param modelAndView
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        log.info("postHandle执行{}",modelAndView);
    }

    /**
     * 页面渲染以后
     * @param request
     * @param response
     * @param handler
     * @param ex
     * @throws Exception
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        log.info("afterCompletion执行异常{}",ex);
    }
}

2. 配置拦截器

/**
 * 1、编写一个拦截器实现HandlerInterceptor接口
 * 2、拦截器注册到容器中(实现WebMvcConfigurer的addInterceptors)
 * 3、指定拦截规则【如果是拦截所有,静态资源也会被拦截】
 */
@Configuration
public class AdminWebConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginInterceptor())
                .addPathPatterns("/**")  //所有请求都被拦截包括静态资源
                .excludePathPatterns("/","/login","/css/**","/fonts/**","/images/**","/js/**"); //放行的请求
    }
}

四、文件上传

1、页面表单

<form method="post" action="/upload" enctype="multipart/form-data">
    <input type="file" name="file"><br>
    <input type="submit" value="提交">
</form>

2、文件上传代码

    /**
     * MultipartFile 自动封装上传过来的文件
     * @param email
     * @param username
     * @param headerImg
     * @param photos
     * @return
     */
    @PostMapping("/upload")
    public String upload(@RequestParam("email") String email,
                         @RequestParam("username") String username,
                         @RequestPart("headerImg") MultipartFile headerImg,
                         @RequestPart("photos") MultipartFile[] photos) throws IOException {

        log.info("上传的信息:email={},username={},headerImg={},photos={}",
                email,username,headerImg.getSize(),photos.length);

        if(!headerImg.isEmpty()){
            //保存到文件服务器,OSS服务器
            String originalFilename = headerImg.getOriginalFilename();
            headerImg.transferTo(new File("H:\\cache\\"+originalFilename));
        }

        if(photos.length > 0){
            for (MultipartFile photo : photos) {
                if(!photo.isEmpty()){
                    String originalFilename = photo.getOriginalFilename();
                    photo.transferTo(new File("H:\\cache\\"+originalFilename));
                }
            }
        }


        return "main";
    }

五、异常处理

1、默认规则

  • 默认情况下,Spring Boot提供/error处理所有错误的映射

  • 对于机器客户端,它将生成JSON响应,其中包含错误,HTTP状态和异常消息的详细信息。对于浏览器客户端,响应一个“ whitelabel”错误视图,以HTML格式呈现相同的数据

    <span>SpringBoot笔记(4)</span>

    error/下的4xx,5xx页面会被自动解析;

<span>SpringBoot笔记(4)</span>

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/115188.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • LInux CUDA环境配置

    LInux CUDA环境配置1.安装toolkit(1)cd/home/CUDA_train/software/cuda4.1(2)./cudatoolkit_4.1.28_linux_64_rhel6.x.run指定安装目录(3)配置cudatoolkit环境变量(a)vim~/.bashrc(b)添加如下行,用于添加cudabin的路径到环境变量PATHexportPATH=$PAT

    2022年5月22日
    189
  • awvs使用方法_一般人游50米要多久

    awvs使用方法_一般人游50米要多久Awvs的是一款非常好用的web漏洞扫描工具,他的扫描速度比较快,可以自己选择扫描速度,比较灵活。Awvs分为老的版本和新版本,下面我介绍的是新版本的功能和用法。功能介绍如下:仪表盘(监视器)功能,添加目标功能,漏洞排序功能,扫描功能,发现功能,用户功能,扫描配置功能,网络扫描功能,追踪器功能,防火墙设置,邮件设置,引擎,时间排除功能,代理功能,常规设置主要使用的功能是前面的6个,后面的根据个人的需要进行配置详细介绍如下:Dashboard功能:翻译意思仪表盘(监视器),可以对扫描对扫描

    2022年9月22日
    3
  • Java IO用法详解

    Java IO用法详解一、javaio概述1.1相关概念JavaIOJavaIO即Java输入输出系统。不管我们编写何种应用,都难免和各种输入输出相关的媒介打交道,其实和媒介进行IO的过程是十分复杂的,这要考虑的因素特别多,比如我们要考虑和哪种媒介进行IO(文件、控制台、网络),我们还要考虑具体和它们的通信方式(顺序、随机、二进制、按字符、按字、按行等等)。Java类库的设计者通过设计大量的类来攻克这些难题…

    2022年5月1日
    51
  • pycharm安装三方库_pycharm无网络安装第三方库

    pycharm安装三方库_pycharm无网络安装第三方库系统:mac10.15.3软件:2019.3pycharm导入第三方库的方法是1先点击File>OtherSetting>PreferencesforNewProjects2点击红框中的小+3在红线位置搜索要加载的第三方库,然后点击箭头所指的installPackage安装模块,点击后按钮按钮为灰色,等恢复后NumPy模块就已经安装完成了,并且显示在列表中,…

    2022年8月27日
    2
  • 不要再问芝士和奶酪有什么区别了!一次解释清楚「建议收藏」

    不要再问芝士和奶酪有什么区别了!一次解释清楚「建议收藏」在西方,奶酪绝对是全民食物,无论男女老少,很多都是“没奶酪会死星人”。两位世界知名大佬都曾对它发表过经典言论,丘吉尔在二战时说,一个为世界提供300种以上奶酪的国家是不应该灭亡的。而戴高乐总统的看法则是:“要统治一个拥有600种奶酪的国家,是很困难的。”    但在中国,它的接受面好像还真没那么广,如果深究起来是有很多方面的原因,包括历史、地域、文化等,说起来也是太复杂,还有奶酪的

    2022年4月20日
    60
  • error C4996: ‘stricmp’: The POSIX name for this item is deprecated

    error C4996: ‘stricmp’: The POSIX name for this item is deprecated

    2021年9月3日
    70

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号