怎么将一个ssm项目改成使用Spring Boot项目

2019/05/23 22:47
阅读数 3.2K

1. 第一步:修改pom.xml

SpringBoot的依赖配置加入到原来的pom.xml文件里面

  1. 加入SpringBoot的配置
  2. 移除原来与SpringBoot冲突的依赖

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>1.5.20.RELEASE</version>

<relativePath /> <!-- lookup parent from repository -->

</parent>

<groupId>cn.gzsxt</groupId>

<artifactId>rbac-springboot</artifactId>

<version>1.0</version>

 

 

<properties>

<java.version>1.8</java.version>

</properties>

 

<dependencies>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-jdbc</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

<!-- 移除嵌入式 tomcat 服务器 -->

<exclusions>

<exclusion>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-tomcat</artifactId>

</exclusion>

</exclusions>

</dependency>

<dependency>

<groupId>org.mybatis.spring.boot</groupId>

<artifactId>mybatis-spring-boot-starter</artifactId>

<version>1.3.4</version>

</dependency>

 

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-devtools</artifactId>

<scope>runtime</scope>

</dependency>

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

<scope>runtime</scope>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-test</artifactId>

<scope>test</scope>

</dependency>

 

<!-- jsp -->

<dependency>

<groupId>javax.servlet.jsp</groupId>

<artifactId>javax.servlet.jsp-api</artifactId>

<version>2.2.1</version>

<scope>provided</scope>

</dependency>

<!-- servlet -->

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>javax.servlet-api</artifactId>

<scope>provided</scope>

</dependency>

 

<!-- jstl -->

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>jstl</artifactId>

</dependency>

 

<!-- dbcp2 -->

<dependency>

<groupId>org.apache.commons</groupId>

<artifactId>commons-dbcp2</artifactId>

</dependency>

 

<!-- log4j -->

<dependency>

<groupId>log4j</groupId>

<artifactId>log4j</artifactId>

<version>1.2.17</version>

</dependency>

 

 

<!-- shiro-springmvc -->

<dependency>

<groupId>org.apache.shiro</groupId>

<artifactId>shiro-spring</artifactId>

<version>1.3.2</version>

</dependency>

 

</dependencies>

 

<build>

<plugins>

<plugin>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>

</plugin>

</plugins>

</build>

 

</project>

 

2. 第二步:创建启动类

--Application

package cn.lxm;

 

import org.springframework.boot.autoconfigure.AutoConfigureAfter;

import org.springframework.boot.autoconfigure.SpringBootApplication;

 

import cn.lxm.config.MvcConfig;

import cn.lxm.config.ShiroConfig;

 

@SpringBootApplication

@AutoConfigureAfter(value= {MvcConfig.class,ShiroConfig.class})

public class Application {

 

 

}

 

--ServletInitializer

package cn.gzsxt;

 

import org.springframework.boot.builder.SpringApplicationBuilder;

import org.springframework.boot.web.support.SpringBootServletInitializer;

 

 

public class ServletInitializer extends SpringBootServletInitializer {

 

 

 

@Override

protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {

 

      return builder.sources(Application.class);

}

 

}

 

 

3. 第三步:删除配置web.xml

  1. 删除web.xml 配置文件
  2. 配置的过滤放在自定义的配置类实现
  3. 移除原来多余的配置类,如Mybatis整合SpringSpringMVC配置

 

package cn.lxm.config;

 

import org.springframework.boot.SpringBootConfiguration;

import org.springframework.boot.web.servlet.FilterRegistrationBean;

import org.springframework.context.annotation.Bean;

import org.springframework.web.filter.DelegatingFilterProxy;

import org.springframework.web.servlet.config.annotation.InterceptorRegistration;

import org.springframework.web.servlet.config.annotation.InterceptorRegistry;

import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

 

import cn.lxm.interceptor.TokenIneterceptor;

 

@SpringBootConfiguration

public class MvcConfig extends WebMvcConfigurerAdapter {

 

@Bean

public FilterRegistrationBean getFilterRegistration() {

//创建一个过滤器注册处理对象

FilterRegistrationBean filterRegistrationBean=new FilterRegistrationBean();

//创建一个代理过滤器设置给该注册对象

filterRegistrationBean.setFilter(new DelegatingFilterProxy());

//指定过滤器的名称【注意事项,必须和下面配置代理的Bean的名通过】

filterRegistrationBean.setName("shiroFilter");

//设置参数

filterRegistrationBean.addInitParameter("targetFilterLifecycle", "true");

//设置拦截路径

filterRegistrationBean.addUrlPatterns("/*");

return filterRegistrationBean;

}

 

 

 

@Bean(name="tokenIneterceptor")

public TokenIneterceptor getTokenIneterceptor() {

return new TokenIneterceptor();

}

 

@Override

public void addInterceptors(InterceptorRegistry registry) {

InterceptorRegistration tokenRegistration = registry.addInterceptor(getTokenIneterceptor());

tokenRegistration.addPathPatterns("/**");

 

}

 

}

 

4. 第四步:修改Index代码

SpringBootJSP页面JavaScript跳转支持有问题。所以修改主页代码

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>

<!-- 实现:如果没有登录跳转到登录页面,如果已经登录了,就跳转到首页 -->

<shiro:authenticated>

  <jsp:forward page="/toIndex" />

</shiro:authenticated>

<shiro:notAuthenticated>

   <jsp:forward page="/toLogin" />

</shiro:notAuthenticated>

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部