MVC配置原理
//如果我们要扩展Springmvc,官方建议我们这样去做
@Configuration//配置类
//@EnableWebMvc //导入了一个类 DelegatingWebMvcConfiguration.class:从容器中获取所有的webmvcconfig;
public class MyConfig_02 implements WebMvcConfigurer {
//视图跳转
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/kuang").setViewName("index");
}
}
扩展springmvc
1.首页配置
1. 所有静态页面都需要thymeleaf接管
2. url:@{}
2.页面国际化
- 需要配置i18n文件
-
如果需要在项目中进行按钮自动转换,我们需要自定义一个组件LocaleResolver
public class MyLocaleResolver implements LocaleResolver { //解析请求 @Override public Locale resolveLocale(HttpServletRequest httpServletRequest) { //获取请求中的语言参数 String language = httpServletRequest.getParameter("l"); Locale locale = Locale.getDefault();//如果没有就使用默认的 //如果请求的链接携带了国际化参数 if(!StringUtils.isEmpty(language)){ //zh_CN String[] s = language.split("_"); //国家 地区 locale= new Locale(s[0],s[1]); } return locale; } @Override public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) { } }
-
记得将自己写的组件配置到spring容器中@Bean
@Configuration public class MyConfig implements WebMvcConfigurer { @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/").setViewName("login"); registry.addViewController("/login.html").setViewName("login"); } //自定义的国际化组件就生效了 @Bean public LocaleResolver localeResolver(){ return new MyLocaleResolver(); } }
-
#{}
3.登录+拦截器
登录Controller
@Controller
public class LoginController {
@RequestMapping("/user/login")
//@ResponseBody
public String login(
@RequestParam("username") String username,
@RequestParam("password")String password,
Model model, HttpSession httpSession){
//具体的业务
if(!StringUtils.isEmpty(username)&& "123456".equals(password)){
httpSession.setAttribute("loginUser",username);
// return "index";
return "redirect:/main.html";
}else{
model.addAttribute("msg","登录失败!!!");
return "redirect:/login.html";
}
}
}
拦截器
public class LoginHandlerInterceptor 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("/login.html").forward(request, response);
return false;
} else {
return true;
}
}
}
4.员工列表展示
-
提取公共页面
-
th:fragment=“top” (这个top可以是任意的参数XXX)
-
th:replace="~{commons/commons::top}"
-
如果要使用参数可以使用()传参,接收判断即可
传参:(index.html)
<div th:replace="~{commons/commons::xxx(active='list.html')}"></div>
接收判断:(commons.html)
<li> <a th:class="${active=='list.html'?'nav-link active':'nav-link'}" th:href="@{/emps}"> <span>员工管理</span> </a> </li>
-
-
列表循环展示
<tr th:each="emp:${emps}"> <td th:text="${emp.getId()}"></td> <td >[[${emp.getLastName()}]]</td> <td th:text="${emp.getEmail()}"></td> <td th:text="${emp.getGender()==0?'女':'男'}"></td> <td th:text="${emp.department.getDepartmentName()}"></td> <td th:text="${#dates.format(emp.getBirth(),'yyyy-MM-dd HH:mm:ss')}"></td> <td> <button class="btn btn-sm btn-primary">编辑</button> <button class="btn btn-sm btn-danger">删除</button> </td> </tr>
5.添加员工
6.修改员工
7.删除员工