SpringBoot的CRUD

SpringBoot的CRUD

javaspringboot笔记

晚来天欲雪,能饮一杯无?

选中web模块,创建springboot工程

导入静态资源(导哪里?)

一、导入静态资源

1. 所有 /webjars/** ,都去 classpath:/META-INF/resources/webjars/ 找资源;

1-1. webjars:以jar包的方式引入静态资源;http://www.webjars.org/

SpringBoot的CRUD
enter description here
SpringBoot的CRUD引入jquery的webjar
通过以下访问路径就可以访问到jquery的js了
http://localhost:8080/webjars/jquery/3.5.1/jquery.js

1-2. 导入自己的静态资源

SpringBoot的CRUD
enter description here
将自己的静态资源的放到static文件夹下

1-3. 配置自己的图标

将ico文件也放入static文件夹下

二、引入模板引擎(Thymeleaf)

在pom文件中引入dependency

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

1、导入thymeleaf的名称空间

<html lang="en" xmlns:th="http://www.thymeleaf.org">

三、引入实体类,dao层,和页面

SpringBoot的CRUD
enter description here

四、引入bootstrap

<!--引入bootstrap-->
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>bootstrap</artifactId>
            <version>5.0.0-beta1</version>
        </dependency>

将页面中的路径都改为Thymeleaf模板引擎的方式
SpringBoot的CRUD

1. 第一个功能,登陆页面的国际化

  • 在resources路径下创建i18n文件夹
  • 在i18n下创建index.properties配置文件(默认)
  • 继续创建index_zh_CN.properties(中文)
  • 继续创建index_en_US.properties(英文)
  • SpringBoot的CRUD
  • 点击Resource Bundle视图,点+号添加属性
  • SpringBoot的CRUD
  • 在application.properties中配置国际化配置文件的位置
  • SpringBoot的CRUD
  • 在页面中使用#{}取值
  • SpringBoot的CRUD
  • 完成!
  • SpringBoot的CRUD

1-1. 实现点击按钮转换语言

配置区域信息解析器(spring boot默认装配了,换掉它默认的,默认是根据请求头获取的)

  • 创建自己的MyLocaleResolver
  • SpringBoot的CRUD
  • 在前台页面加入以下,让他进行跳转时带上区域信息
  • SpringBoot的CRUD
  • 在MyLocaleResolver里进行配置
public class MyLocaleResolver implements LocaleResolver {
    /**
     * Resolve the current locale via the given request.
     * Can return a default locale as fallback in any case.
     *
     * @param request the request to resolve the locale for
     * @return the current locale (never {@code null})
     */
    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        String l = request.getParameter("l");
        //如果参数没带,就用系统默认的
        Locale locale = Locale.getDefault();
        //如果参数带了就用自己的
        //判断是否为空
        if (!StringUtils.isEmpty(l)){
            //切割
            String[] split = l.split("_");
          locale =  new Locale(split[0],split[1]);
        }
        return null;
    }
  • 创建自己的配置类,将MyLocaleResolver返回
  • SpringBoot的CRUD

五、登录功能实现

  • 在登录的form表单指定请求路径
  • SpringBoot的CRUD
  • 生成LoginController,然后做相应的登录方法
@Controller
public class LoginController {
 
    @PostMapping(value = "/user/login")
    public String login(@RequestParam("username") String username,
                        @RequestParam("password") String password,
                        Map<String,Object> map){
        if (StringUtils.isEmpty(username) && "123456".equals(password)){
            //登陆成功,到dashboard页面
            return "dashboard";
        }else {
            //向页面返回错误信息
            map.put("msg","用户名密码错误");
            return "/";
        }
    }
}
  • 在前台要加入name属性
  • SpringBoot的CRUD
  • 在配置文件中禁用缓存
  • SpringBoot的CRUD
  • 在页面加入P标签,来展示登录错误信息,并进行判断,只有在msg不为空的时候才可以展示
  • SpringBoot的CRUD

1. 解决表单重复提交

在MyMvcConfig添加视图映射的方法

@Bean
    public WebMvcConfigurer webMvcConfigurer(){
        return new WebMvcConfigurer() {
            @Override
            public void addViewControllers(ViewControllerRegistry registry) {
			   //添加视图映射
                registry.addViewController("/main.html").setViewName("dashboard");
            }
        };
    }

然后修改login方法,使用重定向到main.html
SpringBoot的CRUD

2. 配置拦截器

  • 在login方法中添加以下代码
  • SpringBoot的CRUD
  • 然后在component包中配置拦截器
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("/").forward(request,response);
            return false;
        }else {
            //已登录
            return true;
        }
    }
	}
  • 最后到MyMvcConfig中注册拦截器
  • SpringBoot的CRUD
上一篇:RabbitMQ学习(三)广播模型(fanout)


下一篇:SpringBoot的CRUD