1.@bean
作用:相当于xml的配置 注入bean(给容器添加组件)。
写一个方法,返回他的对象本身的值。
方法名==id 返回类型==class return的值 == 组件在容器的实例
2.@conditional:(条件装配 底层)
满足conditional指定的条件,才进行组件注入 @conditiononbean( name="tom") 只有容器中有tom这个bean下面的的类和方法才有效。
3.ImportResource("classpath:beans.xml") 放在配置类里面,可以将配置文件的bean注入到容器里。
4.@ConfigurationProperties(prefix = "mycar") 将配置文件的属性的值注入到类的属性里。
@EnableConfigurationProperties(car.class)
//1.开启car的属性配置,2.把car这个组件自动注册到容器内 不需要再car实体类上面写@component。
5.springbootApplication
(1)springbootconfigration :代表当前是配置类。
(2)compaentscan:指定扫描哪些。
(3)EnableAutoConfigration
lombok
9.22
thymeleaf模板
思路:处理静态资源
1.登录功能:
(1)在controller里面写跳转到登录页面的方法@requestmapping("/"),
找到登录页面(主页面)写他的controller方法,
在此方法里面判断登录的账号密码。
方法:if(!StringUtils.isEmpty(user.getUserName()) && "123456".equals(user.getPassword())
在方法写一个User对象,用密码和user对象的password对比。
写一个HttpSession参数,把登录的用户保存起来: session.setAttribute("user",user);
在前端页面中就可以使用这个session对象。调用“user”值可以传入在浏览器传入的user对象的name。
登录成功要重定向到main.html。return redirect:/main.html. 避免了表单重复提交。
。。。。。redirect与forward的区别
redirect:普通的return的返回的是一个页面的路径,而redirect则发送的是一个requestmapping的请求路径。直接把main.html的页面的请求路径加在redirect后面,
直接重定向到了他的页面,而普通的return到他的页面的话,在每一次刷新的时候都会重复提交表单。
forward:可以将request对象的信息带着供服务器使用。而redirect的对象不能共享数据
在方法中写Model对象可以使用model.addAttribute()方法,在前端页面可以传入值
通过session对象将输入的userName和password属性将user对象用setattribute方法保存起来。
2.实现将main主页的用户信息显示出来
(1)首先 在前端页面f12找到用户信息的名称,然后在main.index页面找到相应的位置(ctrl+f),将他固定的值换成 [[${session.user.userName}]]
thymeleaf的写法:
链接使用 :th:src=@{链接}
循环写法:
使用
model.addAttribute("users",users); 将users的值传入到前端
拦截器
1.实现一个HandlerInterceptor接口
public class LoginInterceptor implements HandlerInterceptor {
2。重写他的preHandle方法。
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
通过session获得user对象
HttpSession session = request.getSession();
Object user = session.getAttribute("user");
判断是否登录,如果登录则放行
if (user!=null){
// 放行
return true;
}
// 拦截住
提示信息
在前端页面为:
request.setAttribute("msg","请先登录");
request.getRequestDispatcher("/").forward(request,response);
return false;
}
3.配置拦截器
编写一个类implements WebMvcConfigurer 加@Configuration注解表示交给spring容器管理。
4.重写他的addInterceptors方法
加上拦截的路径 addPathPatterns("/**")拦截所有
excludePathPatterns("/","/login","/css/**","/fonts/**","/images/**","/js/**")表示放行的路径。
@Configuration
public class AdminWebConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
// /** 静态资源也被拦截
registry.addInterceptor(new Logininterceptor()).addPathPatterns("/**")
.excludePathPatterns("/","/login","/css/**","/fonts/**","/images/**","/js/**");
9.24
1,-----------文件上传
(1)