Lombok,国际化,拦截器,Thymeleaf组件化,报错页面,注销页面
-
- Lombok的使用
- 国际化配置
- 拦截器
- Thymeleaf组件化
-
- 选择下拉框
- 报错页面
- 注销页面
- 开发过程:
- WEB开发过程中的注意事项:
Lombok的使用
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
有可能会出现Lombok导包后仍失效的问题:
IDEA的的解决办法:
打开seting->plugins->搜索lombok,安装其插件再重启就好好了
eclipse的解决办法:
需要自己把jar放到eclipse安装目录,然后用cmd启动安装
配置:静态资源需要使用thymeleaf接管:@{}
自己编写代码的时候,发现不用修改前端静态路径也可以,就算改了
server.servlet.context-path=/test
也依旧可以,可能版本问题?
国际化配置
th:后面可以加前端的许多属性,例如th:placeholder,th:text,th:value,根据所需要修改的属性进行修改
①先配置好i18n(国际化英文的简写)文件夹下的各类英语的properties
②然后配置properties或yaml,并在网页上进行值的修改和赋予,国际化消息是用#{}进行取值
spring.messages.basename=i18n.XXX
③查看源码localeResolver方法的编写,进行学习再改写
@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(
prefix = "spring.mvc",
name = {"locale"}
)
public LocaleResolver localeResolver() {
if (this.mvcProperties.getLocaleResolver() == org.springframework.boot.autoconfigure.web.servlet.WebMvcProperties.LocaleResolver.FIXED) {
return new FixedLocaleResolver(this.mvcProperties.getLocale());
} else {
AcceptHeaderLocaleResolver localeResolver = new AcceptHeaderLocaleResolver();
localeResolver.setDefaultLocale(this.mvcProperties.getLocale());
return localeResolver;
}
}
注意点:在WebMvcAutoConfiguration中的方法名为什么,自己重写的方法名就应该为其方法名,@ConditionalOnMissingBean会确认容器是否存在该容器。如果不一样,解析器仍会用原来那个。
④自定义本地解析器
public class MyLocaleResolver implements LocaleResolver {
@Override
public Locale resolveLocale(HttpServletRequest request) {
String language= request.getParameter("l");
Locale locale=Locale.getDefault();
if(!StringUtils.isEmpty(language)){
String[] split=language.split("_");
locale = new Locale(split[0], split[1]);
}
return locale;
}
@Override
public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {
}
}
⑤根据WebMvcConfigurer接口的方法进行重写
public class MyMvcConfig implements WebMvcConfigurer {
@Bean
public LocaleResolver localeResolver(){
return new MyLocaleResolver();
}
}
拦截器
①先写自定义拦截器类继承HandlerInterceptor接口
public class LoginHandlerInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
Object Login = request.getSession().getAttribute("Login");
if (Login == null) {
request.setAttribute("msg", "没有权限,请先登录");
request.getRequestDispatcher("/index.html").forward(request, response);
return false;
}else{
return true;
}
}
}
②再重写WebMvcConfigurer接口的addInterceptors方法,将写拦截器类进行配置使用
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**").excludePathPatterns("/index.html","/","/login");
}
Thymeleaf组件化
①建立模板页面model.html,在上面进行一些重复性的页面内容进行组件化
②利用对模板页面内容: th:fragment=“XXX”
③再在其他页面进行引用:th:insert="{model::XXX}",th:replace="{model::XXX}"
④如果要传递参数,可以直接使用()传参,接受判断即可
选择下拉框
因为department的主键为id,所以用id传给后台,进行判断知道其id的department对象,并进行赋值,则不能进行直接传入对象。
<select class="form-control" name="department.id">
<option th:each="department:${departments}" th:selected="${employee.getDepartment().getDepartmentName()==department.getDepartmentName()}" th:text="${department.getDepartmentName()}" th:value="${department.getId()}"></option>
</select>
报错页面
springboot的404页面的显示很简单,只需要在templates下面放一个error文件夹下面放404.html,不能用其他命名。404页面命名必须有404.html,其他报错页面也一样。
注销页面
利用session.invalidate()该方法主要是清空已定义的session。
开发过程:
1、前端搞定:页面长什么样子:数据
2、设计数据库
3、前端让他能够自动运行,独立化工程,用vue.js可以利用一下就假的数值进行赋予
4、数据接口对接:json,对象 all in one 把所需属性汇到一个对象身上
5、前后端联调测试
WEB开发过程中的注意事项:
①配置了server.servlet.context-path=/test这个之后
一些表单的提交action="/XXX"需要更换为th:action="@{/XXX}",要不然跳转的都是http://localhost:8080/XXX,不会跳到http://localhost:8080/test/XXX 进行访问。
②正式开发都是要把真实的文件给隐藏,给一个虚拟的html
@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("tour");
}
}
③使用Themeleaf可以有一些很强大的工具类
④Themeleaf传值不是用?而是用括号()
⑤Themeleaf的th:each运用,可以使用后端方法
<tr th:each="emp:${emps}">
<td th:text="${emp.getId()}"></td>
</tr>