一、首页(欢迎页)配置
扩展 SpringMVC 配置,在 config 文件夹下编写自定义配置类 MyMVCConfig,该类实现 WebMvcConfigurer 接口,重写 addViewControllers 方法
@Configuration
@EnableWebMvc
public class MyMVCConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
registry.addViewController("/index").setViewName("index");
}
}
注意:使用 thymeleaf 的话需要关闭 thymeleaf 缓存:
spring:
profiles: prod #配置环境的名称
thymeleaf:
cache: false
进行上述配置后,再次访问 http://localhost:8084/index 或 http://localhost:8084/,便会自动路由到 index.html
二、国际化
页面国际化:很多网站可以一键切换网站内容的语言,例如中文和英文的一键切换。
如何在 IDEA 中进行页面国际化配置?
1、File —— Setting ——File encoding,确定其中选项编码都设置为 UTF-8,否则会出现乱码。
2、在 resource 目录下创建文件夹 i18n,在文件夹中建立 login.properties 和login_zh_CH.properties。
3、打开建立的 properties 文件,左下角选择 Resource Bundle,选择“+”即可进行添加配置。
4、全局配置文件中指定国际化配置文件的位置:
spring:
messages:
basename: i18n.login
5、在 config 文件夹下建立 MyLocaleResolver 类,实现 LocaleResolver 接口,重写其中的 resolveLocale 和 setLocale 方法
public class MyLocaleResolver implements LocaleResolver {
@Override
public Locale resolveLocale(HttpServletRequest httpServletRequest) {
String language = httpServletRequest.getParameter("l"); //获取前端传入参数 l(语言地区类型)
Locale locale = Locale.getDefault(); //如果没有指定,则使用默认
if(!StringUtils.isEmpty(language)){
String[] split = language.split("_");//zh_CN
new Locale(split[0], split[1]); //国家_地区
}
return locale;
}
@Override
public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {
}
}
6、在自定义配置类 MyMVCConfig 中进行配置,将国际化配置文件 MyLocaleResolver 类放入 Bean 中。
@Configuration
@EnableWebMvc
public class MyMVCConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
registry.addViewController("/index").setViewName("index");
}
@Bean
public LocaleResolver localeResolver(){
return new MyLocaleResolver();
}
}
7、前端设置相应触发(按钮、链接等)
三、登录拦截器
1、自定义 LoginHandlerInteceptor 类,并且实现 HandlerInterceptor 接口,重写其中的 preHandle 方法:判断用户session是否存在,存在则不拦截(返回 false),不存在则拦截(返回true)且请求转发到指定页面(例如首页或者登录页)。
public class LoginHandlerInteceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//登陆成功之后,会存在用户的session
Object loginUser = request.getSession().getAttribute("loginUser");
if (loginUser == null){
request.setAttribute("msg","没有权限,请先登录!");
request.getRequestDispatcher("/index.html").forward(request, response);
return false;
}else
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
}
}
2、在自定义配置类 MyMVCConfig 中进行配置,重写 addInterceptors 方法,添加上一步配置的拦截器,且设置拦截内容和不拦截内容。
@EnableWebMvc
public class MyMVCConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
registry.addViewController("/index").setViewName("index");
}
@Bean
public LocaleResolver localeResolver(){
return new MyLocaleResolver();
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginHandlerInteceptor())
.addPathPatterns("/**") //拦截哪些
.excludePathPatterns("/index.html", "/", "/user/login", "/css/**", "/js/**", "/img/**"); //不拦截哪些
}
}
四、404(错误页)配置
SpringBoot 中配置错误页面很简单,在 resources/templates 目录下建一个 error 文件夹,并且把 404.html 文件放入其中即可,SpringBoot 出现错误时会自动索引到该文件。