springboot-web-crud

springmvc扩展器

@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
//所有扩展功能有些在里面
}

视图控制器

@Override
public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/").setViewName("index");
    registry.addViewController("/index.html").setViewName("index");
    registry.addViewController("/main.html").setViewName("dashboard");
}

区域解析器

/**
 * 可以在连接上携带区域信息
 */
public class MyLocaleResolver implements LocaleResolver {
    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        String l = request.getParameter("l");
        Locale locale = Locale.getDefault();
        if(!StringUtils.isEmpty(l)){
            String[] s = l.split("_");
            locale = new Locale(s[0],s[1]);
        }
        return locale;
    }

    @Override
    public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {

    }
}
@Bean
public LocaleResolver localeResolver(){
    return new MyLocaleResolver();
}

登录拦截器

public class LoginHandlerInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    //登录控制器在session里加入loginUser
        Object loginUser = request.getSession().getAttribute("loginUser");
        if(loginUser == null){
            request.setAttribute("msg","请先登录");
            request.getRequestDispatcher("/index.html").forward(request,response);
            return false;
        }
        return true;
    }
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**").
            excludePathPatterns("/index.html","/","/user/login","/asserts/**");
}

thymeleaf相关

公共部分

  1. 创建common包下commons.html
  2. 将公共部分在页面下编写,并 th:fragment=“id”
<nav class="navbar navbar-dark sticky-top bg-dark flex-md-nowrap p-0" th:fragment="topBar">
  ...
</nav>
  1. 在要插入的部分
<div th:insert="~{common/commons::topBar}"></div>
<div th:replace="~{common/commons::sideBar(active='main.html')}"></div>
  1. 若有区别,可在括号里传参,如上
<a th:class="${active=='main.html'?'nav-link active':'nav-link'}" th:href="@{/main.html}">

比较

<td th:text="${emp.getSex()}==0?'女':'男'"></td>

一些数据类型

<td th:text="${#dates.format(emp.getBirth(),'yyyy-MM-dd HH:mm:ss')}"></td>
<p style="color: darkred" th:text="${msg}" th:if="${not #strings.isEmpty(msg)}"></p>

IDEA热部署

参考

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <optional>true</optional>
</dependency>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值