Skay's 博客重构过程(From Servelt to Spring MVC to Spring Boot)

渗透技巧 3年前 (2020) admin
486 0 0

自己基础太差了,从头补起,但是最起码知道Java Web从最原始的Servlet到Spring MVC,再到现在最流行的开箱即用的Spring Boot,写的东西没有多高级,就是个博客,前端还是从Github扒下来的(此处感谢小北)。博客大概长这样:


Skay's 博客重构过程(From Servelt to Spring MVC to Spring Boot)


Skay's 博客重构过程(From Servelt to Spring MVC to Spring Boot)




一、Servlet 版本




1.三层架构

写一个项目,总要有点架构,这里采用了三层架构,三层架构的三层分别是dao(数据访问层)、logic(逻辑层)、view(表示层)以及通过实体类model作为数据传递的媒介。分别对应项目中的dao、servlet、dao、entity。

2.新建项目

从Maven建立一个JavaWeb项目

Skay's 博客重构过程(From Servelt to Spring MVC to Spring Boot)

其它默认next即可,idea会自动构建项目,初始构建好后如下

Skay's 博客重构过程(From Servelt to Spring MVC to Spring Boot)

main目录下增加java package,存放项目文件,博客项目结构如下,然后配置tomcat服务即可

Skay's 博客重构过程(From Servelt to Spring MVC to Spring Boot)

3.Blog项目目录结构

Skay's 博客重构过程(From Servelt to Spring MVC to Spring Boot)


4.以注册功能举例说明代码实现

(1) 新建User类,作为User的bean

package Entity;/** * @auther Skay * @date 2020/10/20 15:33 * @description */public class User {private String email;private String password;private String username;private String id;public String getId() {return id;}public void setId(String id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}}


(2) Servlet目录下新建RegisterServlet 逻辑处理类

package Servlet;import Dao.JdbcUtil;import Dao.UserDao;import Entity.JDBCConnection;import Entity.Return_result;import Entity.User;import Util.initutil;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;import java.io.PrintWriter;import java.util.Enumeration;import java.util.HashMap;/** * @auther Skay * @date 2020/10/20 15:41 * @description */public class RegisterServlet extends HttpServlet {private Return_result return_result = new Return_result();private User user = new User();private String token = "";private HashMap<String,Object> req_map = new HashMap<>();;private String Action = "";protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//        PrintWriter out = response.getWriter();//        out.println("hello world");doPost(request,response);}protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {PrintWriter out = response.getWriter();token = initutil.gettoken(request.getCookies(),token); //获取tokenEnumeration<String> a = request.getParameterNames(); //获取的请求数 保存到map中req_map = initutil.parsreq(request.getParameterNames(),request,response,req_map);Action = request.getParameter("action");RegisterServlet registerServlet = new RegisterServlet(req_map);//        out.println(registerServlet.run(request,response).get_return_result());if(registerServlet.run(request,response).get_map_result().get("result").equals("true")){response.sendRedirect("./login.html");}else {response.sendRedirect("./register.html");}}public RegisterServlet() {}public RegisterServlet(HashMap<String, Object> req_map) {user.setUsername((String) req_map.get("username"));user.setPassword((String) req_map.get("password"));user.setEmail((String) req_map.get("email"));}public Return_result run(HttpServletRequest request, HttpServletResponse response) {UserDao userDao = new UserDao();if(userDao.register_dao(user)){return_result.add_result_para("result","true");}else {return_result.add_result_para("result","false");}return return_result;}}


(3) Dao目录下新建 UserDap 数据处理类

Skay's 博客重构过程(From Servelt to Spring MVC to Spring Boot)

(4) 最后webapp/WEB-INF/ 目录下新建register.html 以html作为VIEW 层

js处理前端数据提交以及接收后端数据,然后渲染页面

Skay's 博客重构过程(From Servelt to Spring MVC to Spring Boot)


5.Filter

  • 全局的安全参数过滤

  • 全局的登录session校验

6.Util 存放工具类

  • AES加密工具类

  • Random工具类


二、Spring MVC 版本




1.关于MVC

模型(Model)、视图(View)和控制器(Controller)编写项目时,这边增加了一个Service

2.新建项目

idea真是亲爸爸

Skay's 博客重构过程(From Servelt to Spring MVC to Spring Boot)

IDEA 会自动帮我们下载好必要的 jar 包,并且为我们创建好一些默认的目录和文件,创建好以后项目结构如下:

Skay's 博客重构过程(From Servelt to Spring MVC to Spring Boot)

tomcat服务器的配置

Skay's 博客重构过程(From Servelt to Spring MVC to Spring Boot)

如果需要Tomcat服务器找到spring相关的jar包,还需要将【lib】文件夹整个剪贴到【WEB-INF】下,并重新建立依赖,一些项目中用到的外部依赖jar也需要放置到此目录下:

Skay's 博客重构过程(From Servelt to Spring MVC to Spring Boot)

ps:关于找不到jstl标签库,还需要单独的配置

链接: https://pan.baidu.com/s/1g5T56F7o-ovOGxjznM1t8g 提取码: tdm3

WEB/INF的lib下,除了导入jstl.jar包,还要导入standard.jar包。另外,解压standard.jar包,把.tld文件放在WEB/INF下。

3.web.xml 的配置

Skay's 博客重构过程(From Servelt to Spring MVC to Spring Boot)

4.dispatcher-servlet.xml的配置

注意注释

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xmlns:mvc="http://www.springframework.org/schema/mvc"       xmlns:p="http://www.springframework.org/schema/p"       xmlns:context="http://www.springframework.org/schema/context"       xsi:schemaLocation="        http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans.xsd        http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context.xsd        http://www.springframework.org/schema/mvc        http://www.springframework.org/schema/mvc/spring-mvc.xsd"><!--    我的注释 扫描controller下的组件 --><context:component-scan base-package="controller"/><!--    我的注释 这里是用来寻找service--><context:component-scan base-package="service" /><!--    我的注释 这里是用来寻找dao--><context:component-scan base-package="repository" /><!--    我的注释 这里是用来寻找过滤器--><context:component-scan base-package="validator" /><!--    我的注释 这里是视图解析器--><bean id="viewResolver"          class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/page/" /><property name="suffix" value=".jsp" /></bean><!--    配置消息属性文件 --><bean id="messageSource"          class="org.springframework.context.support.ReloadableResourceBundleMessageSource"><property name="basename" value="/WEB-INF/resource/errorMessages.properties" /></bean><mvc:interceptors><!-- 配置一个全局拦截器,拦截所有请求 --><!--<bean class="interceptor.TestInterceptor" />--><mvc:interceptor><!-- 配置拦截器作用的路径 --><mvc:mapping path="/**"/><!--定义在<mvc:interceptor>元素中,表示匹配指定路径的请求才进行拦截--><bean class="interceptor.PermissionInterceptor"/></mvc:interceptor><mvc:interceptor><!-- 配置拦截器作用的路径 --><mvc:mapping path="/**"/><!--定义在<mvc:interceptor>元素中,表示匹配指定路径的请求才进行拦截--><bean class="interceptor.SafeInterceptor"/></mvc:interceptor></mvc:interceptors></beans>


5.项目目录结构

Skay's 博客重构过程(From Servelt to Spring MVC to Spring Boot)

6.以注册功能举例说明一个功能的项目实现

(1) entity 目录下的User bean 同上

(2) 数据处理层

Dao 目录数据处理目录换了一个名字,repository,且需要加上@Repository注解

Skay's 博客重构过程(From Servelt to Spring MVC to Spring Boot)

(3) Controller 以及 Service

首先service目录下新建RegisterService接口

package service;import entity.User;import org.springframework.stereotype.Service;/** * @auther Skay * @date 2020/11/13 16:39 * @description */public interface RegisterService {boolean register(User user);}


service目录下新建impl目录,并新建RegisterServiceImpl实现这个接口,且需要加上@Service注解,service层是需要跟数据层通信,必然会用到UserDao,这里会用到另一个注解@Autowire 用来引入UserDao

package service.impl;import entity.User;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import repository.UserDao;import service.RegisterService;/** * @auther Skay * @date 2020/11/13 16:42 * @description */@Servicepublic class RegisterServiceImpl implements RegisterService {@Autowiredprivate UserDao userDao;@Overridepublic boolean register(User user) {return userDao.register_dao(user);}}


接下来在controller目录下新建RegisterController类,首先肯定需要

这里有几点需要记录说明

  • 控制器需要@Controller注解

  • @Autowired注入 RegisterService,

  • 这里我还是有了一个验证器,所以也需要注入Validator

  • @RequestMapping 匹配前台请求的url (还可以这样绑定参数@RequestMapping(value = “/comment/{articleId}”))

  • 关于传参 Model model, User user 利用这种方式传参,最后返回的register 其实时register.jsp的简写,因为xml中做了相应配置,所以可以不用写。

  • forward: 用来重定向

  • model 还有addAttribute方法,用来向View 返回数据

    @Controllerpublic class RegisterController {@Autowiredprivate RegisterService registerService;@Resourceprivate Validator validator;@RequestMapping(value = "/do_register")public String register(Model model, User user, BindingResult result){//        添加验证 是否为空validator.validate(user,result);if(registerService.register(user)){return "forward:login";}else {return "forward:register";}}@RequestMapping(value = "/register")public String register(){return "register";}}

    (4)View 层

    以jsp作为view层,在page目录下新建register.jsp 利用模板标签来获取数据

    Skay's 博客重构过程(From Servelt to Spring MVC to Spring Boot)


    (5) 关于拦截器以及验证器

    interceptor validator  具体参考 http://c.biancheng.net/spring_mvc/


    三、Spring Boot 版本




    个人理解就是Spring MVC 去掉了配置xml文件的过程,然后用不着tomcat自己就可以跑起来,所以具体代码先鸽子,这里有篇文章写的很详尽https://zhuanlan.zhihu.com/p/166144865


    四、总结




    内容很基础,活到老学到老。

    完整代码请移步  https://github.com/0linlin0/skay_blog



    原文始发于微信公众号(赛博少女):Skay's 博客重构过程(From Servelt to Spring MVC to Spring Boot)

    版权声明:admin 发表于 2020年11月24日 下午2:54。
    转载请注明:Skay's 博客重构过程(From Servelt to Spring MVC to Spring Boot) | CTF导航

    相关文章

    暂无评论

    您必须登录才能参与评论!
    立即登录
    暂无评论...