SpringBoot Web开发

SpringBoot Web开发

1.静态资源映射规则

1.1什么是webjars 呢?

Webjars本质就是以jar包的方式引入我们的静态资源 , 我们以前要导入一个静态资源文件,直接导入即可。

使用SpringBoot需要使用Webjars,我们可以去搜索一下:

网站:https://www.webjars.org

要使用jQuery,我们只要要引入jQuery对应版本的pom依赖即可!


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

访问:只要是静态资源,SpringBoot就会去对应的路径寻找资源,我们这里访问:http://localhost:8080/webjars/jquery/3.4.1/jquery.js

1.2静态资源映射规则

自定义静态资源路径

我们也可以自己通过配置文件来指定一下,哪些文件夹是需要我们放静态资源文件的,在application.properties中配置;

spring.resources.static-locations=classpath:/coding/,classpath:/app/

一旦自己定义了静态文件夹的路径,原来的自动配置就都会失效了!

总结:

1.在springboot中,我们可以使用以下方式处理静态资源

  • webjars localhost:8080/webjars/
  • public,static,/**,rescources localhost:8080/

2.优先级:rescources>static(默认)>public

2.Thymeleaf模板引擎

2.1模板引擎

前端交给我们的页面,是html页面。如果是我们以前开发,我们需要把他们转成jsp页面,jsp好处就是当我们查出一些数据转发到JSP页面以后,我们可以用jsp轻松实现数据的显示,及交互等。

jsp支持非常强大的功能,包括能写Java代码,但是呢,我们现在的这种情况,SpringBoot这个项目首先是以jar的方式,不是war,像第二,我们用的还是嵌入式的Tomcat,所以呢,他现在默认是不支持jsp的

Thymeleaf模板引擎,这模板引擎呢,是一个高级语言的模板引擎,他的这个语法更简单。而且呢,功能更强大。

2.2引入Thymeleaf

怎么引入呢,对于springboot来说,什么事情不都是一个start的事情嘛,我们去在项目中引入一下。给大家三个网址:

Thymeleaf 官网:https://www.thymeleaf.org/

Thymeleaf 在Github 的主页:https://github.com/thymeleaf/thymeleaf

Spring官方文档:找到我们对应的版本

https://docs.spring.io/spring-boot/docs/2.2.5.RELEASE/reference/htmlsingle/#using-boot-starter

        <dependency>
			<groupId>org.thymeleaf</groupId>
			<artifactId>thymeleaf-spring5</artifactId>
		</dependency>
		<dependency>
			<groupId>org.thymeleaf.extras</groupId>
			<artifactId>thymeleaf-extras-java8time</artifactId>
		</dependency>

视图解析:

我们只需要把我们的html页面放在类路径下的templates下,thymeleaf就可以帮我们自动渲染了。

使用thymeleaf什么都不需要配置,只需要将他放在指定的文件夹下即可!

2.3Thymeleaf 语法学习

1.我们要使用thymeleaf,需要在html文件中导入命名空间的约束,方便提示。

我们可以去官方文档的#3中看一下命名空间拿来过来:

xmlns:th="http://www.thymeleaf.org"

测试:

@Controller
public class IndexController {
    @RequestMapping("/test")
    public String index(Model model){
        model.addAttribute("msg","这是一个提示信息!");
        return "test";
    }
}

<!DOCTYPE html>
<html  xmlns:th="http://www.thymeleaf.org" >
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div>
    <h1 th:text="${msg}">你好</h1>
</div>
</body>
</html>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-O9Du2ku7-1658804103684)(C:\Users\xhd\AppData\Roaming\Typora\typora-user-images\image-20220722203911056.png)]

2.3.4Thymeleaf的使用语法

1、我们可以使用任意的 th:attr 来替换Html中原生属性的值!

图片

Variable Expressions: ${…}

Selection Variable Expressions: *{…} 选择

Message Expressions: #{…}

Link URL Expressions: @{…} 地址

Fragment Expressions: ~{…}

3.MVC自动配置原理

官方文档地址:

https://docs.spring.io/spring-boot/docs/2.2.5.RELEASE/reference/htmlsingle/#boot-features-spring-mvc-auto-configuration

@Configuration//自定义去扩展springmvc,就要插入注解
@EnableWebMvc//就是导入了一个类,DelegatingWebMvcConfiguration从容器中获取所有的webmvccconfig
public class MymvcConfig implements WebMvcConfigurer {

    @Override//视图跳转
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/xhd").setViewName("/test");
    }
}

@EnableWebMvc:全面接管即:SpringBoot对SpringMVC的自动配置不需要了,所有都是我们自己去配置!只需在我们的配置类中要加一个@EnableWebMvc。

总结一句话:@EnableWebMvc将WebMvcConfigurationSupport组件导入进来了;

而导入的WebMvcConfigurationSupport只是SpringMVC最基本的功能!

一般在扩展的时候不要写@EnableWebMvc。

4.国际化

1.需要配置i18n文件,使用#{}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Tu2e5zDp-1658804103686)(C:\Users\xhd\AppData\Roaming\Typora\typora-user-images\image-20220723153340205.png)]

2.我们需要在项目中进行按钮自动切换,我们需要自定义一个组件 LocaleResolver

	<a class="btn btn-sm" th:href="@{/index.html(l=zh_CN)}">中文</a>
	<a class="btn btn-sm" th:href="@{/index.html(l=en_US)}">English</a>
//实现国际化
public class MyLocaleResolver implements LocaleResolver {
    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        //获得请求
        String l = request.getParameter("l");
        Locale locale = request.getLocale();//默认请求
        if (!StringUtils.isEmpty(l)){//不是空,进行切分
            String[] split = l.split("_");//国家,地区
            locale=new Locale(split[0],split[1]);
        }
        return locale;
    }
    @Override
    public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {
    }
}

3.记得将自己写的组件配置到spring容器中 @Bean

@Configuration//自定义去扩展springmvc,就要插入注解
public class MymvcConfig implements WebMvcConfigurer {
    @Bean//自定义国际化失效
    public LocaleResolver localeResolver(){
        return new MyLocaleResolver();
    }
}

5.拦截器

1.编写拦截器类,进行判断

public class LoginHandlerInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        Object loginuser = request.getSession().getAttribute("Loginuser");
        if (loginuser==null){
            request.setAttribute("msg","没有权限,请先登录");
            request.getRequestDispatcher("/index.html").forward(request,response);
            return false;
        }else {
            return true;
        }

    }
}

2.配置mvc进行生效

    //配置拦截器生效
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**")
                .excludePathPatterns("/index.html","/","/user/login","/css/**","/js/**","/img/**");

    

6.列表展示

1.提取公共页面

​ 1.取别名 th:fragment="topbar"

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-GnI2zckF-1658804103687)(C:\Users\xhd\AppData\Roaming\Typora\typora-user-images\image-20220723184853094.png)]

​ 2.参数展示<div th:replace="~{commons/commons::topbar}"></div>

​ 3.如果需要传参可以直接使用()传参,进行判断即可

<div th:replace="~{commons/commons::siderbar(active='list.html')}"></div>

<a th:class="${active=='main.html' ? 'nav-link active':'nav-link'}" th:href="@{/dashboard.html.html}">

2.列表循环展示

               <tr th:each="emp:${emps}">
				<td th:text="${emp.getId()}"></td>
				<td th:text="${emp.getLastName()}"></td>
				<td th:text="${emp.getEmail()}"></td>
				<td th:text="${emp.getGender()==0?'':''}"></td>
				<td th:text="${emp.department.getDepartmentName()}"></td>
				<td th:text="${#dates.format(emp.getBrith(),'yyy-MM-dd')}"></td>
				<td>
					<button class="btn btn-sm btn-primary">编辑</button>
					<button class="btn btn-sm btn-danger">删除</button>
				</td>
				</tr>

==0?‘女’:‘男’}">



编辑
删除


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值