SpringBoot-4 web开发

spring boot 的web开发

1)创建spring boot 项目,选择需要自动配置的模块

2)创建 完成后编写业务代码,即可运行

参照自动配置原理

XXXautoconfiguration 自动配置

XXXEnableconfigurationproperties 指定配置类的类型

configurationproperties 将配置信息与spring boot 配置文件 参数绑定,可以通过设置参数即可更改配置信息

1、spring boot 对于静态资源的配置

spring boot以jar方式打包,对于静态资源的放置有要求。所有/webjars/**都去

classpath:/META-INF/resources/webjars下找资源

l例子:

<link  th:href="@{META-INF/resources/webjars/bootstrap/4.0.0/css/bootstrap.css}" rel="stylesheet">

1、引入webjars静态资源==(https://www.webjars.org/)==(如jQuery、bootstrap等)

2、引入依赖 以jquery为例

<dependency>
 <groupId>org.webjars</groupId>
 <artifactId>jquery</artifactId>
 <version>3.4.1</version>
</dependency>

项目的静态资源存放:

1、classpath:/resources

2、classpath:/static

3、classpath:/public

欢迎页的设置:

​ 静态资源文件夹下的所有index.html页面

​ localhost:8080/

2、模板引擎

spring boot 默认不支持jsp文件。官方推荐thymeleaf模板引擎。理解为是一个页面控制器

1)、引入thymeleaf依赖:

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

2)、thymeleaf语法&用法

spring boot 对thymeleaf默认设置:

public class ThymeleafProperties {
    private static final Charset DEFAULT_ENCODING;
    public static final String DEFAULT_PREFIX = "classpath:/templates/";
    public static final String DEFAULT_SUFFIX = ".html";
    private boolean checkTemplate = true;
    private boolean checkTemplateLocation = true;
    private String prefix = "classpath:/templates/";
    private String suffix = ".html";
    private String mode = "HTML";

thymeleaf对比JSTL语句:

在这里插入图片描述

表达式:

${}:

​ 1、获取对象属性,调用方法

​ 2、使用内置的基本对象:四大域对象(servletContext、request、response、session、)

*{}:与${}用法基本一致,唯一区别是代替< th:object>

@{}:定义url地址,@{/pages/hello(参数1=值,参数2=值)}。值可以使用${}取值

~{}:片段

使用案例:

html页面:

<div th:text="${lijie}"/>
<div th:text="${lijie1}"/>
<div th:utext="${lijie1}"/>
<hr/>
<h4 th:each="name:${list} "  th:text="${name}"></h4>

springmvc的controller层:

@Controller
public class testcontro {
    @RequestMapping("/success")
    public String test(Map<String,Object> map){
        map.put("lijie","李杰");
        map.put("lijie1","<h1>理解</h1>");
        map.put("list", Arrays.asList("好的","lisa","debug"));
        return "success";
    }
}

在编码时,若想使前端页面实时生效,需禁用thymeleaf缓存。并ctrl+F9重新编译。

3、spring MVC扩展

如果保留Spring Boot MVC特性,你只需添加其他的MVC配置(拦截器,格式化处理器,视图控制器等)。你可以添加自己的WebMvcConfigurerAdapter类型的@Configuration类,而不需要注解@EnableWebMvc。如果希望使用自定义的RequestMappingHandlerMapping,RequestMappingHandlerAdapter,或ExceptionHandlerExceptionResolver,你可以声明一个WebMvcRegistrationsAdapter实例提供这些组件。

如果想全面控制Spring MVC,你可以添加自己的@Configuration,并使用@EnableWebMvc注解。

springboot已经配置了绝大部分springMVC的内容,若有自定义的配置则需要写一个配置类,@configuration。

如自定义的拦截器、类型转换器。

@Configuration
public class Myconfig extends WebMvcConfigurationSupport {
    ....//配置内容
}

4、代码

1、默认访问页:

​ 方法一:可以写一个空参数的@RequestMapping("/"),设置跳转到指定的首页。

​ 方法二:写一个配置类。

2、国际化

springMVC中使用国际化步骤:

​ 1)、编写国际化配置文件

​ 2)、使用ResourceBunleMessage

​ 3)、在页面使用fmt:message取出国际化内容

springboot 中使用国际化

springboot自动配置类MessageSourceAutoConfiguration ,配置了国际化。可通过在spring boot配置文件中设

spring.messages属性配置国际化文件。

@Bean
@ConfigurationProperties(
    prefix = "spring.messages"
)
login.button=登录/sign in
login.password=密码/password
login.remember=记住我/remember me
login.tip=请登录/please sign in
login.username=账户/username

在这里插入图片描述

使用thymeleaf获得国际化内容。#{ 国际化文件内容}

<body class="text-center">
   <form class="form-signin" action="dashboard.html" th:action="@{/user/login}" method="post">
      <img class="mb-4" th:src="@{/img/bootstrap-solid.svg}" src="asserts/img/bootstrap-solid.svg" alt="" width="72" height="72"/>
      <h1 class="h3 mb-3 font-weight-normal" th:text="#{login.tip}">请登录</h1>

      <label class="sr-only" th:text="#{login.username}">Username</label>
      <input type="text"  name="username" class="form-control" th:placeholder="#{login.username}" required="" autofocus="">
           <label class=" sr-only" th:text="#{login.password}">Password</label>
      <input type="password" name="password" class="form-control" th:placeholder="#{login.password}"  required="">
      <div class="checkbox mb-3">
         <label>
                 <input type="checkbox" value="" /><a th:text="#{login.remember}"></a>
              </label>
      </div>
      <button class="btn btn-lg btn-primary btn-block" type="submit" th:text="#{login.button}">登录</button>
      <p class="mt-5 mb-3 text-muted">© 2017-2018</p>
      <a class="btn btn-sm" >中文</a>
      <a class="btn btn-sm" >English</a>
   </form>
</body>
原理:

​ springMVC中通过LocaleResolver接口解析区域信息,实现国际化。spring boot在无区域解析配置时自动配置默认的区域解析。通过浏览器请求头获取语言信息

​ 自定义区域解析配置类步骤:

​ 1、可以在前端请求上附带区域信息。

​ 2、获取提交的区域信息

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值