其他分享
首页 > 其他分享> > SpringBoot的CRUD

SpringBoot的CRUD

作者:互联网

SpringBoot的CRUD

javaspringboot笔记

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

选中web模块,创建springboot工程

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

一、导入静态资源

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

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

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

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

enter description here
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层,和页面

enter description here
enter description here

四、引入bootstrap

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

将页面中的路径都改为Thymeleaf模板引擎的方式
enter description here

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

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

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

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;
    }

五、登录功能实现

@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 "/";
        }
    }
}

1. 解决表单重复提交

在MyMvcConfig添加视图映射的方法

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

然后修改login方法,使用重定向到main.html
enter description here

2. 配置拦截器

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;
        }
    }
	}

标签:webjars,return,SpringBoot,locale,CRUD,request,public,页面
来源: https://www.cnblogs.com/lxs1204/p/14157190.html