目录
临近毕业,我的好大儿问了我一个GitHub上找的项目的相关问题,项目也是很简单,但是用到的东西很齐,在这里记录一下我从这个项目看到的东西以及相关概念。这是一个博客论坛,使用springboot+mybatis+thymeleaf+layui实现博客论坛。
项目地址:https://github.com/alaions/whiteCat
也是很久以前就准备对网上的项目进行分析,我认为当拿到一个web项目,应该按着如下步骤学习:
1、阅读项目的readme
这个是非常有必要的,介绍了如何配置环境以及启动项目,基本照着readme启动就不会有错,结果这个项目的readme没什么东西。
2、阅读项目的配置文件
最起码要对application.properties中的各个属性的值有个大概的了解,而且光看他们的取名应该就能看出来是啥意思:
server:
port: 8080
servlet:
context-path: /chen
server.servlet.context-path= # Context path of the application. 应用的上下文路径,也可以称为项目路径,是构成url地址的一部分。
当server.servlet.context-path不配置时,默认为 / ,如:localhost:8080/xxxxxx
当server.servlet.context-path有配置时,如上代码,此时的访问方式为localhost:8080/chen/xxxxxx
其他的基本都是常用的配置。在阅读完项目的配置文件后,还需要阅读代码中的配置文件,一般来说,每个工程下都会有一个config文件夹,里面会有一个XXXXConfig.java的文件,一般都是实现了WebMvcConfigurer接口的一个类
// 扩展配置类
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
@Value("${index.static.properties.avatar}")
private String avatarPath;
@Value("${index.static.properties.topicPicture}")
private String topicPicturePath;
public void addViewControllers(ViewControllerRegistry registry) {
/*registry.addViewController("/main.html").setViewName("admin/adindex");*/
registry.addViewController("/login.html").setViewName("login");
registry.addViewController("/").setViewName("login");
registry.addViewController("/register.html").setViewName("register");
registry.addViewController("/index.html").setViewName("redirect:toUserIndex");
registry.addViewController("/code.html").setViewName("code");
}
/*https://blog.csdn.net/weixin_44690195/article/details/108855892*/
public void addResourceHandlers(ResourceHandlerRegistry registry) {
// windows
/*registry.addResourceHandler("/images/avatar/**").
addResourceLocations(MyStaticProperties.avatar);
registry.addResourceHandler("/images/topicPicture/**").
addResourceLocations(MyStaticProperties.topicPicture);
registry.addResourceHandler("/images/other/**").
addResourceLocations(MyStaticProperties.otherPicture);*/
// /images/** 映射到哪里去,前端里面的路由 file:/home/fileUpload/ 表示需要访问linux系统文件所在的文件夹路径名称
// linux的地址
String avatar = "file:" + avatarPath;
String topicPicture = "file:" + topicPicturePath;
registry.addResourceHandler("/images/avatar/**").
addResourceLocations(avatar);
registry.addResourceHandler("/images/topicPicture/**").
addResourceLocations(topicPicture);
registry.addResourceHandler("/images/other/**").
addResourceLocations("file:/www/wwwroot/images/other/");
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginHandlerInterceptor())
.addPathPatterns("/**").excludePathPatterns("/css/**", "/font-awesome/**",
"/fonts/**", "/images/**", "/img/**", "/js/**", "/layui/**", "/lib/**",
"/", "/login", "/register", "/login.html", "/main.html", "/register.html",
"/userIndex.html", "/toUserIndex","/detail/**", "/submitComment", "/reply/**", "write.html",
"/personal.html", "/index.html", "/toArticle", "/article.html", "/category/**",
"/article/selectSubmit", "/**/lastPage", "/**/nextPage", "/**/toWhichPage/**",
"/topic/supportOrCriticism/**", "/comment/supportOrCriticism/**", "/register/**",
"/toUserPage", "/user.html", "/searchName", "/otherPersonal.html", "/toOtherPersonal/**",
"/searchTag", "/submitComment/**", "/toOtherPersonalPrivate/**", "/code.html");
}
}
在代码中可以看到实现了三个方法,了解他们对于我们理解项目有很大作用:
1、 addViewControllers方法
其实就是省去了controller层代码的编写,因为要访问一个页面,得通过controller层的路径映射进行匹配,使用这个方法,可以快速的实现页面跳转。例如
registry.addViewController("/").setViewName("login");
它的作用是将一个请求直接映射为一个页面,功能跟以下代码一样:
@Controller
public class test {
@GetMapping("/")
public String test(){
return "login";
}
}
2、 addResourceHandlers
该方法是对本地资源映射路径进行配置:
@Value("${index.static.properties.avatar}")
private String avatarPath;
String avatar = "file:" + avatarPath;
registry.addResourceHandler("/images/avatar/**").
addResourceLocations(avatar);
这段代码的含义是对"/images/avater/**"路径下的资源,全部映射到avater这个值上,我们可以看到avater是通过@Value注解从配置文件中获取值的,看一下配置文件中是如何配置的:
3、addInterceptors方法
顾名思义,添加一个拦截器,可以对我们选择的请求进行拦截,并对其进行一些操作。需要我们手动编写一个拦截器类,项目里使用的是作者自己定义的LoginHandlerInterceptor类:
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){
return true;
} else {
request.setAttribute("msg", "没有权限,请先登录");
request.getRequestDispatcher("/login.html").forward(request, response);
return false;
}
}
}
3、开始了解这个项目的大致流程
由于公司电脑还没装IDEA(妈的,两个月前说要给我权限让我装,现在还没给,一坨屎),只能通过VsCode来大致看看代码,然后结合作者本人的项目稍微讲讲:
就从这个项目启动开始说起,默认路径为http://47.115.225.24:8080/chen/
为什么地址是这样呢?为什么会有这个页面?来一步步分析:
这个IP地址是作者的部署的服务器地址,8080则是项目启动的端口,可以在application.properites中进行配置,那么/chen/是怎么来的呢,其实上面已经说了是通过server.servlet.context-path进行配置,那么除去/chen,路径就为/,一般来说/对应的页面是index.html,而我发现这里没有index.html:
那这是什么页面呢 ?
答案也已经讲过了,在配置类中addViewControllers方法中进行了设置,路径为"/"请求将返回login。login是什么呢?光返回一个字符串,谁知道是什么。其实配置文件也给出了答案,通过加上prefix,suffix,那么"login"字符串=="/resources/templates/login.html",那么首页也就是这个界面。
项目使用的是thymeleaf来进行前端代码的编写,这个跟html没什么太大区别,多了一点语法,在这里不赘述了,因为看得懂基本的前端代码就看得懂thymeleaf,使用的比较多的就是通过$获取值,通过@进行跳转。我们观察login.html,它是一个登录注册的页面,我们来看如何登录的,可以在login.html中看到表单属性中有th:action="@{/login}",点击登录就会发现一个"/login"的请求,在LoginController类中可以找到处理对应请求的代码:
@GetMapping("/login")
public String checkLogin(String username, String password, HttpSession session){
User user = userService.getUserByUsername(username);
System.out.println(userService.getAdminPassword());
if (Objects.nonNull(user)){
if("admin".equals(username) && userService.getAdminPassword().equals(password)){
session.setAttribute("loginUser", user);
return "redirect:/main.html";
} else if(password.equals(user.getPassword())){
session.setAttribute("loginUser", user);
return "redirect:/toUserIndex";
}
}
return "redirect:/loginFiled";
}
/*registry.addViewController("/main.html").setViewName("admin/adindex");*/
@GetMapping("/main.html")
public String toAdmin(HttpSession session){
User loginUser = (User) session.getAttribute("loginUser");
String username = loginUser.getUsername();
if ("admin".equals(username)){
return "admin/adindex";
}
else {
return "redirect:/";
}
}
代码还是比较简单容易理解的,我们在这里主要看返回值,发现如果是admin账号登录,登录成功就返回"redirect:/main.html";普通账号登录就返回"redirect:/toUserIndex";登录失败就返回"redirect:/loginFiled"。可以看到都使用到了redirect(重定向),与之相对的有forward(转发),可以去了解一下其中的不同。或者想一下为什么这里要用redirect。
在这里我们假设是admin账号登录,发现会重定向到"/main.html",对应的代码在上图,作者还对其进行了检验,判断用户名是不是"admin",然后返回"admin/adindex",我们可以找到对应的静态文件,大致流程就是这样。
4,总结
今天在公司闲着无聊,随手写了一篇记录一下对这个项目的一些知识的复习,讲的并不是很详细,数据库啊,Mybatis-Plus啊,这些都没讲,以后有机会再记录一下,温故而知新,可以为师矣。