1.创建一个webapp的maven工程
目录结构如下所示:
前端使用的是adminLTE,需要将相关的文件分别放到目录中的css,img,plugins文件夹中
2.在pom.xml文件中引入相关坐标

1 xml version="1.0" encoding="UTF-8"?> 2 3 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0modelVersion> 6 7 <groupId>club.nipengfeigroupId> 8 <artifactId>ssm5artifactId> 9 <version>1.0-SNAPSHOTversion> 10 <packaging>warpackaging> 11 12 <name>ssm5 Maven Webappname> 13 14 <url>http://www.example.comurl> 15 16 <properties> 17 <spring.version>5.0.2.RELEASEspring.version> 18 <slf4j.version>1.6.6slf4j.version> 19 <log4j.version>1.2.12log4j.version> 20 <mysql.version>5.1.6mysql.version> 21 <mybatis.version>3.4.5mybatis.version> 22 <spring.security.version>5.0.1.RELEASEspring.security.version> 23 properties> 24 <dependencies> 25 26 <dependency> 27 <groupId>org.aspectjgroupId> 28 <artifactId>aspectjweaverartifactId> 29 <version>1.6.8version> 30 dependency> 31 <dependency> 32 <groupId>org.springframeworkgroupId> 33 <artifactId>spring-aopartifactId> 34 <version>${spring.version}version> 35 dependency> 36 <dependency> 37 <groupId>org.springframeworkgroupId> 38 <artifactId>spring-contextartifactId> 39 <version>${spring.version}version> 40 dependency> 41 <dependency> 42 <groupId>org.springframeworkgroupId> 43 <artifactId>spring-webartifactId> 44 <version>${spring.version}version> 45 dependency> 46 <dependency> 47 <groupId>org.springframeworkgroupId> 48 <artifactId>spring-webmvcartifactId> 49 <version>${spring.version}version> 50 dependency> 51 <dependency> 52 <groupId>org.springframeworkgroupId> 53 <artifactId>spring-testartifactId> 54 <version>${spring.version}version> 55 dependency> 56 <dependency> 57 <groupId>org.springframeworkgroupId> 58 <artifactId>spring-txartifactId> 59 <version>${spring.version}version> 60 dependency> 61 <dependency> 62 <groupId>org.springframeworkgroupId> 63 <artifactId>spring-jdbcartifactId> 64 <version>${spring.version}version> 65 dependency> 66 <dependency> 67 <groupId>junitgroupId> 68 <artifactId>junitartifactId> 69 <version>4.12version> 70 <scope>compilescope> 71 dependency> 72 <dependency> 73 <groupId>mysqlgroupId> 74 <artifactId>mysql-connector-javaartifactId> 75 <version>${mysql.version}version> 76 dependency> 77 <dependency> 78 <groupId>javax.servletgroupId> 79 <artifactId>servlet-apiartifactId> 80 <version>2.5version> 81 <scope>providedscope> 82 dependency> 83 <dependency> 84 <groupId>javax.servlet.jspgroupId> 85 <artifactId>jsp-apiartifactId> 86 <version>2.0version> 87 <scope>providedscope> 88 dependency> 89 <dependency> 90 <groupId>jstlgroupId> 91 <artifactId>jstlartifactId> 92 <version>1.2version> 93 dependency> 94 95 <dependency> 96 <groupId>log4jgroupId> 97 <artifactId>log4jartifactId> 98 <version>${log4j.version}version> 99 dependency> 100 <dependency> 101 <groupId>org.slf4jgroupId> 102 <artifactId>slf4j-apiartifactId> 103 <version>${slf4j.version}version> 104 dependency> 105 <dependency> 106 <groupId>org.slf4jgroupId> 107 <artifactId>slf4j-log4j12artifactId> 108 <version>${slf4j.version}version> 109 dependency> 110 111 <dependency> 112 <groupId>org.mybatisgroupId> 113 <artifactId>mybatisartifactId> 114 <version>${mybatis.version}version> 115 dependency> 116 <dependency> 117 <groupId>org.mybatisgroupId> 118 <artifactId>mybatis-springartifactId> 119 <version>1.3.0version> 120 dependency> 121 <dependency> 122 <groupId>c3p0groupId> 123 <artifactId>c3p0artifactId> 124 <version>0.9.1.2version> 125 <type>jartype> 126 <scope>compilescope> 127 dependency> 128 <dependency> 129 <groupId>com.github.pagehelpergroupId> 130 <artifactId>pagehelperartifactId> 131 <version>5.1.2version> 132 dependency> 133 <dependency> 134 <groupId>org.springframework.securitygroupId> 135 <artifactId>spring-security-webartifactId> 136 <version>${spring.security.version}version> 137 dependency> 138 <dependency> 139 <groupId>org.springframework.securitygroupId> 140 <artifactId>spring-security-configartifactId> 141 <version>${spring.security.version}version> 142 dependency> 143 <dependency> 144 <groupId>org.springframework.securitygroupId> 145 <artifactId>spring-security-coreartifactId> 146 <version>${spring.security.version}version> 147 dependency> 148 <dependency> 149 <groupId>org.springframework.securitygroupId> 150 <artifactId>spring-security-taglibsartifactId> 151 <version>${spring.security.version}version> 152 dependency> 153 154 <dependency> 155 <groupId>mysqlgroupId> 156 <artifactId>mysql-connector-javaartifactId> 157 <version>${mysql.version}version> 158 dependency> 159 160 <dependency> 161 <groupId>javax.annotationgroupId> 162 <artifactId>jsr250-apiartifactId> 163 <version>1.0version> 164 dependency> 165 dependencies> 166 167 <build> 168 <finalName>ssm5finalName> 169 <pluginManagement> 170 <plugins> 171 <plugin> 172 <artifactId>maven-clean-pluginartifactId> 173 <version>3.1.0version> 174 plugin> 175 176 <plugin> 177 <artifactId>maven-resources-pluginartifactId> 178 <version>3.0.2version> 179 plugin> 180 <plugin> 181 <artifactId>maven-compiler-pluginartifactId> 182 <version>3.8.0version> 183 plugin> 184 <plugin> 185 <artifactId>maven-surefire-pluginartifactId> 186 <version>2.22.1version> 187 plugin> 188 <plugin> 189 <artifactId>maven-war-pluginartifactId> 190 <version>3.2.2version> 191 plugin> 192 <plugin> 193 <artifactId>maven-install-pluginartifactId> 194 <version>2.5.2version> 195 plugin> 196 <plugin> 197 <artifactId>maven-deploy-pluginartifactId> 198 <version>2.8.2version> 199 plugin> 200 plugins> 201 pluginManagement> 202 build> 203 project>
3.创建spring的核心配置文件applicationContext.xml
其中整合了MyBatis框架

1 xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:aop="http://www.springframework.org/schema/aop" 6 xmlns:tx="http://www.springframework.org/schema/tx" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans.xsd 9 http://www.springframework.org/schema/context 10 http://www.springframework.org/schema/context/spring-context.xsd 11 http://www.springframework.org/schema/aop 12 http://www.springframework.org/schema/aop/spring-aop.xsd 13 http://www.springframework.org/schema/tx 14 http://www.springframework.org/schema/tx/spring-tx.xsd"> 15 16 <context:component-scan base-package="club.nipengfei"> 17 18 <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> 19 context:component-scan> 20 21 22 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 23 <property name="driverClass" value="com.mysql.jdbc.Driver"/> 24 <property name="jdbcUrl" value="jdbc:mysql:///ssm1"/> 25 <property name="user" value="root"/> 26 <property name="password" value="npf123"/> 27 bean> 28 29 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 30 <property name="dataSource" ref="dataSource"/> 31 bean> 32 33 <bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 34 <property name="basePackage" value="club.nipengfei.dao"/> 35 bean> 36 37 38 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 39 <property name="dataSource" ref="dataSource" /> 40 bean> 41 42 <tx:advice id="txAdvice" transaction-manager="transactionManager"> 43 <tx:attributes> 44 <tx:method name="find*" read-only="true"/> 45 <tx:method name="*" isolation="DEFAULT"/> 46 tx:attributes> 47 tx:advice> 48 49 <aop:config> 50 <aop:advisor advice-ref="txAdvice" pointcut="execution(* club.nipengfei.service.impl.*ServiceImpl.*(..))"/> 51 aop:config> 52 beans>
4.创建springmvc的核心配置文件springmvc.xml

1 xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:mvc="http://www.springframework.org/schema/mvc" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 6 xsi:schemaLocation=" 7 http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans.xsd 9 http://www.springframework.org/schema/mvc 10 http://www.springframework.org/schema/mvc/spring-mvc.xsd 11 http://www.springframework.org/schema/context 12 http://www.springframework.org/schema/context/spring-context.xsd"> 13 14 <context:component-scan base-package="club.nipengfei"> 15 <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> 16 context:component-scan> 17 18 <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 19 <property name="prefix" value="/pages/"/> 20 <property name="suffix" value=".jsp"/> 21 bean> 22 23 <mvc:resources mapping="/css/**" location="/css/"/> 24 <mvc:resources mapping="/img/**" location="/img/"/> 25 <mvc:resources location="/plugins/" mapping="/plugins/**" /> 26 27 <mvc:annotation-driven/> 28 beans>
5.在web.xml中配置如下信息

1 xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns="http://xmlns.jcp.org/xml/ns/javaee" 4 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 5 version="3.1"> 6 7 <display-name>Archetype Created web Applicationdisplay-name> 8 9 <listener> 10 <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class> 11 listener> 12 13 <context-param> 14 <param-name>contextConfigLocationparam-name> 15 <param-value>classpath:applicationContext.xmlparam-value> 16 context-param> 17 18 <servlet> 19 <servlet-name>dispatcherServletservlet-name> 20 <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class> 21 22 <init-param> 23 <param-name>contextConfigLocationparam-name> 24 <param-value>classpath:springmvc.xmlparam-value> 25 init-param> 26 27 <load-on-startup>1load-on-startup> 28 servlet> 29 <servlet-mapping> 30 <servlet-name>dispatcherServletservlet-name> 31 <url-pattern>/url-pattern> 32 servlet-mapping> 33 34 35 <filter> 36 <filter-name>characterEncodingFilterfilter-name> 37 <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class> 38 <init-param> 39 <param-name>encodingparam-name> 40 <param-value>UTF-8param-value> 41 init-param> 42 filter> 43 <filter-mapping> 44 <filter-name>characterEncodingFilterfilter-name> 45 <url-pattern>/*url-pattern> 46 filter-mapping> 47 web-app>
6.遇到的问题
当运行该工程时报500错误
http500错误是服务器内部错误,看提示显示index.jsp页面的第七行报错。于是在pages文件下新建一个hello.jsp,将index.jsp跳转改为/pages/hello.jsp发现能正常访问。估计是main.jsp中的错误,后来才发现main.jsp页面中引入了aside.jsp其中有使用spring-security的标签,而在pom.xml中没有引入spring-security相应的坐标。因此在pom.xml中引入相应的坐标后就能正常访问。