数据后台管理(一)环境搭建
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.0</modelVersion> 6 7 <groupId>club.nipengfei</groupId> 8 <artifactId>ssm5</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 <packaging>war</packaging> 11 12 <name>ssm5 Maven Webapp</name> 13 <!-- FIXME change it to the project's website --> 14 <url>http://www.example.com</url> 15 16 <properties> 17 <spring.version>5.0.2.RELEASE</spring.version> 18 <slf4j.version>1.6.6</slf4j.version> 19 <log4j.version>1.2.12</log4j.version> 20 <mysql.version>5.1.6</mysql.version> 21 <mybatis.version>3.4.5</mybatis.version> 22 <spring.security.version>5.0.1.RELEASE</spring.security.version> 23 </properties> 24 <dependencies> 25 <!-- spring --> 26 <dependency> 27 <groupId>org.aspectj</groupId> 28 <artifactId>aspectjweaver</artifactId> 29 <version>1.6.8</version> 30 </dependency> 31 <dependency> 32 <groupId>org.springframework</groupId> 33 <artifactId>spring-aop</artifactId> 34 <version>${spring.version}</version> 35 </dependency> 36 <dependency> 37 <groupId>org.springframework</groupId> 38 <artifactId>spring-context</artifactId> 39 <version>${spring.version}</version> 40 </dependency> 41 <dependency> 42 <groupId>org.springframework</groupId> 43 <artifactId>spring-web</artifactId> 44 <version>${spring.version}</version> 45 </dependency> 46 <dependency> 47 <groupId>org.springframework</groupId> 48 <artifactId>spring-webmvc</artifactId> 49 <version>${spring.version}</version> 50 </dependency> 51 <dependency> 52 <groupId>org.springframework</groupId> 53 <artifactId>spring-test</artifactId> 54 <version>${spring.version}</version> 55 </dependency> 56 <dependency> 57 <groupId>org.springframework</groupId> 58 <artifactId>spring-tx</artifactId> 59 <version>${spring.version}</version> 60 </dependency> 61 <dependency> 62 <groupId>org.springframework</groupId> 63 <artifactId>spring-jdbc</artifactId> 64 <version>${spring.version}</version> 65 </dependency> 66 <dependency> 67 <groupId>junit</groupId> 68 <artifactId>junit</artifactId> 69 <version>4.12</version> 70 <scope>compile</scope> 71 </dependency> 72 <dependency> 73 <groupId>mysql</groupId> 74 <artifactId>mysql-connector-java</artifactId> 75 <version>${mysql.version}</version> 76 </dependency> 77 <dependency> 78 <groupId>javax.servlet</groupId> 79 <artifactId>servlet-api</artifactId> 80 <version>2.5</version> 81 <scope>provided</scope> 82 </dependency> 83 <dependency> 84 <groupId>javax.servlet.jsp</groupId> 85 <artifactId>jsp-api</artifactId> 86 <version>2.0</version> 87 <scope>provided</scope> 88 </dependency> 89 <dependency> 90 <groupId>jstl</groupId> 91 <artifactId>jstl</artifactId> 92 <version>1.2</version> 93 </dependency> 94 <!-- log start --> 95 <dependency> 96 <groupId>log4j</groupId> 97 <artifactId>log4j</artifactId> 98 <version>${log4j.version}</version> 99 </dependency> 100 <dependency> 101 <groupId>org.slf4j</groupId> 102 <artifactId>slf4j-api</artifactId> 103 <version>${slf4j.version}</version> 104 </dependency> 105 <dependency> 106 <groupId>org.slf4j</groupId> 107 <artifactId>slf4j-log4j12</artifactId> 108 <version>${slf4j.version}</version> 109 </dependency> 110 <!-- log end --> 111 <dependency> 112 <groupId>org.mybatis</groupId> 113 <artifactId>mybatis</artifactId> 114 <version>${mybatis.version}</version> 115 </dependency> 116 <dependency> 117 <groupId>org.mybatis</groupId> 118 <artifactId>mybatis-spring</artifactId> 119 <version>1.3.0</version> 120 </dependency> 121 <dependency> 122 <groupId>c3p0</groupId> 123 <artifactId>c3p0</artifactId> 124 <version>0.9.1.2</version> 125 <type>jar</type> 126 <scope>compile</scope> 127 </dependency> 128 <dependency> 129 <groupId>com.github.pagehelper</groupId> 130 <artifactId>pagehelper</artifactId> 131 <version>5.1.2</version> 132 </dependency> 133 <dependency> 134 <groupId>org.springframework.security</groupId> 135 <artifactId>spring-security-web</artifactId> 136 <version>${spring.security.version}</version> 137 </dependency> 138 <dependency> 139 <groupId>org.springframework.security</groupId> 140 <artifactId>spring-security-config</artifactId> 141 <version>${spring.security.version}</version> 142 </dependency> 143 <dependency> 144 <groupId>org.springframework.security</groupId> 145 <artifactId>spring-security-core</artifactId> 146 <version>${spring.security.version}</version> 147 </dependency> 148 <dependency> 149 <groupId>org.springframework.security</groupId> 150 <artifactId>spring-security-taglibs</artifactId> 151 <version>${spring.security.version}</version> 152 </dependency> 153 154 <dependency> 155 <groupId>mysql</groupId> 156 <artifactId>mysql-connector-java</artifactId> 157 <version>${mysql.version}</version> 158 </dependency> 159 160 <dependency> 161 <groupId>javax.annotation</groupId> 162 <artifactId>jsr250-api</artifactId> 163 <version>1.0</version> 164 </dependency> 165 </dependencies> 166 167 <build> 168 <finalName>ssm5</finalName> 169 <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> 170 <plugins> 171 <plugin> 172 <artifactId>maven-clean-plugin</artifactId> 173 <version>3.1.0</version> 174 </plugin> 175 <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --> 176 <plugin> 177 <artifactId>maven-resources-plugin</artifactId> 178 <version>3.0.2</version> 179 </plugin> 180 <plugin> 181 <artifactId>maven-compiler-plugin</artifactId> 182 <version>3.8.0</version> 183 </plugin> 184 <plugin> 185 <artifactId>maven-surefire-plugin</artifactId> 186 <version>2.22.1</version> 187 </plugin> 188 <plugin> 189 <artifactId>maven-war-plugin</artifactId> 190 <version>3.2.2</version> 191 </plugin> 192 <plugin> 193 <artifactId>maven-install-plugin</artifactId> 194 <version>2.5.2</version> 195 </plugin> 196 <plugin> 197 <artifactId>maven-deploy-plugin</artifactId> 198 <version>2.8.2</version> 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 <!--开启注解扫描,希望处理service和dao,controller不需要Spring框架处理--> 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 <!--Spring整合MyBatis框架--> 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 <!--配置SqlSessionFactory工厂--> 29 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 30 <property name="dataSource" ref="dataSource"/> 31 </bean> 32 <!--配置AccountDao接口所在包--> 33 <bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 34 <property name="basePackage" value="club.nipengfei.dao"/> 35 </bean> 36 <!--配置Spring框架声明式事务管理--> 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 <!--配置AOP增强--> 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 <!--开启注解扫描,只扫描Controller--> 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 <!--开启SpringMVC注解支持--> 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 Application</display-name> 8 <!--配置监听器,默认只加载WEB-INF目录下的applicationContext.xml配置文件--> 9 <listener> 10 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 11 </listener> 12 <!--设置配置文件路径--> 13 <context-param> 14 <param-name>contextConfigLocation</param-name> 15 <param-value>classpath:applicationContext.xml</param-value> 16 </context-param> 17 <!--配置前端控制器--> 18 <servlet> 19 <servlet-name>dispatcherServlet</servlet-name> 20 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 21 <!--加载springmvc.xml配置文件--> 22 <init-param> 23 <param-name>contextConfigLocation</param-name> 24 <param-value>classpath:springmvc.xml</param-value> 25 </init-param> 26 <!--启动服务器,创建servlet--> 27 <load-on-startup>1</load-on-startup> 28 </servlet> 29 <servlet-mapping> 30 <servlet-name>dispatcherServlet</servlet-name> 31 <url-pattern>/</url-pattern> 32 </servlet-mapping> 33 34 <!--解决中文乱码过滤器--> 35 <filter> 36 <filter-name>characterEncodingFilter</filter-name> 37 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 38 <init-param> 39 <param-name>encoding</param-name> 40 <param-value>UTF-8</param-value> 41 </init-param> 42 </filter> 43 <filter-mapping> 44 <filter-name>characterEncodingFilter</filter-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中引入相应的坐标后就能正常访问。