SSM框架整合
1,开发环境的搭建
JDK+Eclipse+Tomcat+Maven的安装与配置
参考:http://blog.csdn.net/zhshulin/article/details/30779873
2,Maven Web项目创建
创建一个Maven项目来配置环境
参考:http://blog.csdn.net/zhshulin/article/details/37921705
3,SSM的整合
2个配置文件:
① spring-mybatis.xml,包含spring和mybatis的配置文件,
②spring-mvc的配置文件,
2个资源文件:
①jdbc.propertis
②log4j.properties
目录结构:
3.1框架中引入的Jar包(pom.xml配置,也可以直接导入包)
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 3 <modelVersion>4.0.0</modelVersion> 4 <groupId>org.projectManagement</groupId> 5 <artifactId>projectManagement</artifactId> 6 <packaging>war</packaging> 7 <version>0.0.1-SNAPSHOT</version> 8 <name>projectManagement Maven Webapp</name> 9 <url>http://maven.apache.org</url> 10 11 <properties> 12 <!-- spring版本号 --> 13 <spring.version>4.0.2.RELEASE</spring.version> 14 <!-- mybatis版本号 --> 15 <mybatis.version>3.2.6</mybatis.version> 16 <!-- log4j日志文件管理包版本 --> 17 <slf4j.version>1.7.7</slf4j.version> 18 <log4j.version>1.2.17</log4j.version> 19 </properties> 20 <dependencies> 21 <!-- 导入java ee jar 包 --> 22 <dependency> 23 <groupId>org.apache.openejb</groupId> 24 <artifactId>javaee-api</artifactId> 25 <version>6.0-1</version> 26 </dependency> 27 <dependency> 28 <groupId>junit</groupId> 29 <artifactId>junit</artifactId> 30 <version>4.11</version> 31 <!-- 表示开发的时候引入,发布的时候不会加载此包 --> 32 <scope>test</scope> 33 </dependency> 34 <!-- spring核心包 --> 35 <dependency> 36 <groupId>org.springframework</groupId> 37 <artifactId>spring-core</artifactId> 38 <version>${spring.version}</version> 39 </dependency> 40 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-oxm</artifactId> 49 <version>${spring.version}</version> 50 </dependency> 51 <dependency> 52 <groupId>org.springframework</groupId> 53 <artifactId>spring-tx</artifactId> 54 <version>${spring.version}</version> 55 </dependency> 56 57 <dependency> 58 <groupId>org.springframework</groupId> 59 <artifactId>spring-jdbc</artifactId> 60 <version>${spring.version}</version> 61 </dependency> 62 63 <dependency> 64 <groupId>org.springframework</groupId> 65 <artifactId>spring-webmvc</artifactId> 66 <version>${spring.version}</version> 67 </dependency> 68 <dependency> 69 <groupId>org.springframework</groupId> 70 <artifactId>spring-aop</artifactId> 71 <version>${spring.version}</version> 72 </dependency> 73 74 <dependency> 75 <groupId>org.springframework</groupId> 76 <artifactId>spring-context-support</artifactId> 77 <version>${spring.version}</version> 78 </dependency> 79 80 <dependency> 81 <groupId>org.springframework</groupId> 82 <artifactId>spring-test</artifactId> 83 <version>${spring.version}</version> 84 </dependency> 85 <!-- mybatis核心包 --> 86 <dependency> 87 <groupId>org.mybatis</groupId> 88 <artifactId>mybatis</artifactId> 89 <version>${mybatis.version}</version> 90 </dependency> 91 <!-- mybatis/spring包 --> 92 <dependency> 93 <groupId>org.mybatis</groupId> 94 <artifactId>mybatis-spring</artifactId> 95 <version>1.2.2</version> 96 </dependency> 97 <!-- 导入Mysql数据库链接jar包 --> 98 <dependency> 99 <groupId>mysql</groupId> 100 <artifactId>mysql-connector-java</artifactId> 101 <version>5.1.30</version> 102 </dependency> 103 <!-- 导入dbcp的jar包,用来在applicationContext.xml中配置数据库 --> 104 <dependency> 105 <groupId>commons-dbcp</groupId> 106 <artifactId>commons-dbcp</artifactId> 107 <version>1.2.2</version> 108 </dependency> 109 <!-- JSTL标签类 --> 110 <dependency> 111 <groupId>jstl</groupId> 112 <artifactId>jstl</artifactId> 113 <version>1.2</version> 114 </dependency> 115 <!-- 日志文件管理包 --> 116 <!-- log start --> 117 <dependency> 118 <groupId>log4j</groupId> 119 <artifactId>log4j</artifactId> 120 <version>${log4j.version}</version> 121 </dependency> 122 123 124 <!-- 格式化对象,方便输出日志 --> 125 <dependency> 126 <groupId>com.alibaba</groupId> 127 <artifactId>fastjson</artifactId> 128 <version>1.1.41</version> 129 </dependency> 130 131 132 <dependency> 133 <groupId>org.slf4j</groupId> 134 <artifactId>slf4j-api</artifactId> 135 <version>${slf4j.version}</version> 136 </dependency> 137 138 <dependency> 139 <groupId>org.slf4j</groupId> 140 <artifactId>slf4j-log4j12</artifactId> 141 <version>${slf4j.version}</version> 142 </dependency> 143 <!-- log end --> 144 <!-- 映入JSON --> 145 <dependency> 146 <groupId>org.codehaus.jackson</groupId> 147 <artifactId>jackson-mapper-asl</artifactId> 148 <version>1.9.13</version> 149 </dependency> 150 <!-- 上传组件包 --> 151 <dependency> 152 <groupId>commons-fileupload</groupId> 153 <artifactId>commons-fileupload</artifactId> 154 <version>1.3.1</version> 155 </dependency> 156 <dependency> 157 <groupId>commons-io</groupId> 158 <artifactId>commons-io</artifactId> 159 <version>2.4</version> 160 </dependency> 161 <dependency> 162 <groupId>commons-codec</groupId> 163 <artifactId>commons-codec</artifactId> 164 <version>1.9</version> 165 </dependency> 166 <dependency> 167 <groupId>org.mybatis.generator</groupId> 168 <artifactId>mybatis-generator-maven-plugin</artifactId> 169 <version>1.3.2</version> 170 </dependency> 171 </dependencies> 172 <build> 173 <plugins> 174 <plugin> 175 <groupId>org.apache.maven.plugins</groupId> 176 <artifactId>maven-compiler-plugin</artifactId> 177 <configuration> 178 <source>1.8</source> 179 <target>1.8</target> 180 </configuration> 181 </plugin> 182 </plugins> 183 <finalName>projectManagement</finalName> 184 </build> 185 </project>
3.2、Spring与MyBatis的整合
所有需要的JAR包都引入以后,首先进行Spring与MyBatis的整合,然后再进行JUnit测试,
3.2.1、建立JDBC属性文件(jdbc.properties,文件编码修改为utf-8)
1 #<?xml version="1.0" encoding="UTF-8"?> 2 driver=com.mysql.jdbc.Driver 3 url=jdbc:mysql://localhost:3306/数据库名?useUnicode=true&characterEncoding=utf8 4 username=用户名 5 password=密码 6 #定义初始连接数 7 initialSize=0 8 #定义最大连接数 9 maxActive=20 10 #定义最大空闲 11 maxIdle=20 12 #定义最小空闲 13 minIdle=1 14 #定义最长等待时间 15 maxWait=60000
3.2.2、建立spring-mybatis.xml配置文件
这个文件就是用来完成spring和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" xmlns:p="http://www.springframework.org/schema/p" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:mvc="http://www.springframework.org/schema/mvc" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context-3.1.xsd 10 http://www.springframework.org/schema/mvc 11 http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> 12 <!-- 自动扫描 --> 13 <context:component-scan base-package="com.projMgr(包名要改)" /> 14 <!-- 引入配置文件<property resource="classpath:jdbc.properties" /> --> 15 <bean id="propertyConfigurer" 16 class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 17 <property name="location" value="classpath:jdbc.properties" /> 18 19 </bean> 20 21 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 22 destroy-method="close"> 23 <property name="driverClassName" value="${driver}" /> 24 <property name="url" value="${url}" /> 25 <property name="username" value="${username}" /> 26 <property name="password" value="${password}" /> 27 <!-- 初始化连接大小 --> 28 <property name="initialSize" value="${initialSize}"></property> 29 <!-- 连接池最大数量 --> 30 <property name="maxActive" value="${maxActive}"></property> 31 <!-- 连接池最大空闲 --> 32 <property name="maxIdle" value="${maxIdle}"></property> 33 <!-- 连接池最小空闲 --> 34 <property name="minIdle" value="${minIdle}"></property> 35 <!-- 获取连接最大等待时间 --> 36 <property name="maxWait" value="${maxWait}"></property> 37 </bean> 38 39 <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 --> 40 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 41 <property name="dataSource" ref="dataSource" /> 42 <!-- 自动扫描mapping.xml文件 --> 43 <property name="mapperLocations" value="classpath:com/projMgr/mapping/*.xml"></property> 44 </bean> 45 46 <!-- DAO接口所在包名,Spring会自动查找其下的类 --> 47 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 48 <property name="basePackage" value="com.projMgr.Dao" /> 49 <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> 50 </bean> 51 52 <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx --> 53 <bean id="transactionManager" 54 class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 55 <property name="dataSource" ref="dataSource" /> 56 </bean> 57 </beans>
3.2.3、Log4j的配置(log4j.properties)
为了方便调试,一般都会使用日志来输出信息,Log4j是Apache的一个开放源代码项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台、文件、GUI组件,甚至是套接口服务器、NT的事件记录器、UNIX Syslog守护进程等;我们也可以控制每一条日志的输出格式;通过定义每一条日志信息的级别,我们能够更加细致地控制日志的生成过程。
1 #定义LOG输出级别 2 log4j.rootLogger=INFO,Console,File 3 #定义日志输出目的地为控制台 4 log4j.appender.Console=org.apache.log4j.ConsoleAppender 5 log4j.appender.Console.Target=System.out 6 #可以灵活地指定日志输出格式,下面一行是指定具体的格式 7 log4j.appender.Console.layout = org.apache.log4j.PatternLayout 8 log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n 9 10 #文件大小到达指定尺寸的时候产生一个新的文件 11 log4j.appender.File = org.apache.log4j.RollingFileAppender 12 #指定输出目录 13 log4j.appender.File.File = logs/ssm.log 14 #定义文件最大大小 15 log4j.appender.File.MaxFileSize = 10MB 16 # 输出所以日志,如果换成DEBUG表示输出DEBUG以上级别日志 17 log4j.appender.File.Threshold = ALL 18 log4j.appender.File.layout = org.apache.log4j.PatternLayout 19 log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH:mm:ss}][%c]%m%n
3.2.4、JUnit测试
经过以上步骤(log4j不配也没影响),我们已经完成了Spring和mybatis的整合,这样我们就可以编写一段测试代码来试试是否成功了。
3.2.4.1、创建测试用表
1 DROP TABLE IF EXISTS `user`; 2 3 CREATE TABLE `user` ( 4 5 `id` int(11) NOT NULL AUTO_INCREMENT, 6 7 `user_name` varchar(40) NOT NULL, 8 9 `password` varchar(255) NOT NULL, 10 11 `age` int(4) NOT NULL, 12 13 PRIMARY KEY (`id`) 14 15 ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; 16 17 /*Data for the table `user` */ 18 19 insert into `user`(`id`,`user_name`,`password`,`age`) values (1,'张三','2013',4);
3.2.4.2、利用MyBatis Generator自动创建代码
参考:
http://blog.csdn.net/zhshulin/article/details/23912615
http://www.cnblogs.com/smileberry/p/4145872.html
这个可根据表自动创建实体类、MyBatis映射文件以及DAO接口,复制到项目中。
建立文件夹,所需文件:
generatorConfig.xml文件:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE generatorConfiguration 3 PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" 4 "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> 5 <generatorConfiguration> 6 <!-- 数据库驱动--> 7 <classPathEntry location="mysql-connector-java-5.1.30.jar"/> 8 <context id="DB2Tables" targetRuntime="MyBatis3"> 9 <commentGenerator> 10 <property name="suppressDate" value="true"/> 11 <!-- 是否去除自动生成的注释 true:是 : false:否 --> 12 <property name="suppressAllComments" value="true"/> 13 </commentGenerator> 14 <!--数据库链接URL,用户名、密码 --> 15 <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/数据库名称" userId="用户名" password="密码"> 16 </jdbcConnection> 17 <javaTypeResolver> 18 <property name="forceBigDecimals" value="false"/> 19 </javaTypeResolver> 20 <!-- 生成模型的包名和位置--> 21 <javaModelGenerator targetPackage="test.model" targetProject="src"> 22 <property name="enableSubPackages" value="true"/> 23 <property name="trimStrings" value="true"/> 24 </javaModelGenerator> 25 <!-- 生成映射文件的包名和位置--> 26 <sqlMapGenerator targetPackage="test.mapping" targetProject="src"> 27 <property name="enableSubPackages" value="true"/> 28 </sqlMapGenerator> 29 <!-- 生成DAO的包名和位置--> 30 <javaClientGenerator type="XMLMAPPER" targetPackage="test.IDao" targetProject="src"> 31 <property name="enableSubPackages" value="true"/> 32 </javaClientGenerator> 33 <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名--> 34 <table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> 35 </context> 36 </generatorConfiguration>
在mybatis-generator-core-1.3.2文件所在处,按住shift,右键,选择“在此处打开命令窗口”,黏贴一下代码,回车运行。
1 java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite
运行结果:
生成结果:
复制到项目中,并修改目录、文件名称
3.2.4.3、建立Service接口和实现类
目录结构:
以下是具体代码:
IUserService.java
1 package com.projMgr.service; 2 3 import java.util.List; 4 5 import com.projMgr.pojo.User; 6 7 public interface IUserService { 8 public User getUserById(int userId); 9 }
UserServiceImpl.java
1 package com.projMgr.service; 2 3 import java.util.List; 4 5 import javax.annotation.Resource; 6 7 import org.springframework.stereotype.Service; 8 9 import com.projMgr.Dao.IUserDao; 10 import com.projMgr.pojo.User; 11 import com.projMgr.service.IUserService; 12 13 @Service("userService") 14 public class UserServiceImpl implements IUserService { 15 @Resource 16 private IUserDao userDao; 17 @Override 18 public User getUserById(int userId) { 19 return this.userDao.selectByPrimaryKey(userId); 20 } 21 22 }
3.2.4.4、建立测试类
测试类在src/test/java中建立,下面测试类中注释掉的部分是不使用Spring时,一般情况下的一种测试方法;如果使用了Spring那么就可以使用注解的方式来引入配置文件和类,然后再将service接口对象注入,就可以进行测试了。
如果测试成功,表示Spring和Mybatis已经整合成功了。输出信息使用的是Log4j打印到控制台。
TestMyBatis.java
1 package org.projMgr.testmybatis; 2 3 import javax.annotation.Resource; 4 5 import org.apache.log4j.Logger; 6 //import org.junit.Before; 7 import org.junit.Test; 8 import org.junit.runner.RunWith; 9 //import org.springframework.context.ApplicationContext; 10 //import org.springframework.context.support.ClassPathXmlApplicationContext; 11 import org.springframework.test.context.ContextConfiguration; 12 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 13 14 import com.alibaba.fastjson.JSON; 15 import com.projMgr.pojo.User; 16 import com.projMgr.service.IUserService; 17 18 @RunWith(SpringJUnit4ClassRunner.class) //��ʾ�̳���SpringJUnit4ClassRunner�� 19 @ContextConfiguration(locations = {"classpath:spring-mybatis.xml"}) 20 21 public class TestMyBatis { 22 private static Logger logger = Logger.getLogger(TestMyBatis.class); 23 // private ApplicationContext ac = null; 24 @Resource 25 private IUserService userService = null; 26 27 // @Before 28 // public void before() { 29 // ac = new ClassPathXmlApplicationContext("applicationContext.xml"); 30 // userService = (IUserService) ac.getBean("userService"); 31 // } 32 33 @Test 34 public void test1() { 35 User user = userService.getUserById(1); 36 // System.out.println(user.getUserName()); 37 // logger.info("ֵ:"+user.getUserName()); 38 logger.info(JSON.toJSONString(user)); 39 } 40 }
测试结果:
如果测试成功,则完成Spring和mybatis这两大框架的整合,下面在继续进行SpringMVC的整合。
3.3、整合SpringMVC
上面已经完成了2大框架的整合,SpringMVC的配置文件单独放,然后在web.xml中配置整合。
3.3.1、配置spring-mvc.xml
配置主要是自动扫描控制器,视图模式,注解的启动这三个。
spring-mvc.xml
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" xmlns:p="http://www.springframework.org/schema/p" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:mvc="http://www.springframework.org/schema/mvc" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context-3.1.xsd 10 http://www.springframework.org/schema/mvc 11 http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> 12 <!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 --> 13 <context:component-scan base-package="com.projMgr.controller(包名需改)" /> 14 <!--避免IE执行AJAX时,返回JSON出现下载文件 --> 15 <bean id="mappingJacksonHttpMessageConverter" 16 class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> 17 <property name="supportedMediaTypes"> 18 <list> 19 <value>text/html;charset=UTF-8</value> 20 </list> 21 </property> 22 </bean> 23 <!-- 启动SpringMVC的注解功能,完成请求和注解POJO的映射 --> 24 <bean 25 class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> 26 <property name="messageConverters"> 27 <list> 28 <ref bean="mappingJacksonHttpMessageConverter" /> <!-- JSON转换器 --> 29 </list> 30 </property> 31 </bean> 32 <!-- 定义跳转的文件的前后缀 ,视图模式配置--> 33 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 34 <!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 --> 35 <property name="prefix" value="/WEB-INF/jsp/" /> 36 <property name="suffix" value=".jsp" /> 37 </bean> 38 39 <!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 --> 40 <bean id="multipartResolver" 41 class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 42 <!-- 默认编码 --> 43 <property name="defaultEncoding" value="utf-8" /> 44 <!-- 文件大小最大值 --> 45 <property name="maxUploadSize" value="10485760000" /> 46 <!-- 内存中的最大值 --> 47 <property name="maxInMemorySize" value="40960" /> 48 </bean> 49 50 </beans>
3.3.2、配置web.xml文件
这里面对spring-mybatis.xml的引入以及配置的spring-mvc的Servlet就是为了完成SSM整合
web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns="http://java.sun.com/xml/ns/javaee" 5 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 6 id="WebApp_ID" version="3.0"> 7 <display-name>Archetype Created Web Application</display-name> 8 <!-- Spring和mybatis的配置文件 --> 9 <context-param> 10 <param-name>contextConfigLocation</param-name> 11 <param-value>classpath:spring-mybatis.xml</param-value> 12 </context-param> 13 14 <!-- 编码过滤器 --> 15 <filter> 16 <filter-name>encodingFilter</filter-name> 17 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 18 <async-supported>true</async-supported> 19 <init-param> 20 <param-name>encoding</param-name> 21 <param-value>UTF-8</param-value> 22 </init-param> 23 </filter> 24 <filter-mapping> 25 <filter-name>encodingFilter</filter-name> 26 <url-pattern>/*</url-pattern> 27 </filter-mapping> 28 <!-- Spring监听器 --> 29 <listener> 30 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 31 </listener> 32 <!-- 防止Spring内存溢出监听器 --> 33 <listener> 34 <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> 35 </listener> 36 37 <!-- Spring MVC servlet --> 38 <servlet> 39 <servlet-name>SpringMVC</servlet-name> 40 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 41 <init-param> 42 <param-name>contextConfigLocation</param-name> 43 <param-value>classpath:spring-mvc.xml</param-value> 44 </init-param> 45 <load-on-startup>1</load-on-startup> 46 <async-supported>true</async-supported> 47 </servlet> 48 <servlet-mapping> 49 <servlet-name>SpringMVC</servlet-name> 50 <!-- 此处可以可以配置成*.do,对应struts的后缀习惯 --> 51 <url-pattern>/</url-pattern> 52 </servlet-mapping> 53 <welcome-file-list> 54 <welcome-file>/index.jsp</welcome-file> 55 </welcome-file-list> 56 <!-- 配置SESSION超时,单位是分钟 --> 57 <session-config> 58 <session-timeout>30</session-timeout> 59 </session-config> 60 </web-app>
3.3.3、测试
至此已经完成了SSM三大框架的整合了,接下来测试一下,如果成功了即可。
3.3.3.1、新建jsp页面
showUser.jsp
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 3 <html> 4 <head> 5 <title>测试</title> 6 </head> 7 8 <body> 9 ${user.userName} 10 ${user.password} 11 </body> 12 </html>
3.3.3.2、建立UserController类
UserController.java
1 package com.projMgr.controller; 2 3 import java.util.List; 4 5 import javax.annotation.Resource; 6 import javax.servlet.http.HttpServletRequest; 7 8 //import org.springframework.beans.factory.annotation.Autowired; 9 import org.springframework.stereotype.Controller; 10 import org.springframework.ui.Model; 11 //import org.springframework.ui.Model; 12 import org.springframework.web.bind.annotation.RequestMapping; 13 import com.projMgr.pojo.User; 14 import com.projMgr.service.IUserService; 15 16 @Controller 17 @RequestMapping("/user") 18 public class UserController { 19 @Resource 20 private IUserService userService; 21 22 @RequestMapping("/showUser") 23 public String toIndex(HttpServletRequest request,Model model){ 24 int userId = Integer.parseInt(request.getParameter("id")); 25 User user = this.userService.getUserById(userId); 26 model.addAttribute("user", user); 27 return "/showUser"; 28 } 29 30 31 }
3.3.3.3、部署项目
tomcat8.0启动,成功日志:(可用于出错对照)
1 二月 23, 2017 2:36:26 下午 org.apache.catalina.core.AprLifecycleListener init 2 信息: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:Program FilesJavajre1.8.0_25in;C:WINDOWSSunJavain;C:WINDOWSsystem32;C:WINDOWS;C:/Program Files/Java/jre1.8.0_25/bin/server;C:/Program Files/Java/jre1.8.0_25/bin;C:/Program Files/Java/jre1.8.0_25/lib/amd64;D:developing hinkgem-jeesite-6c5ea82docmavenmaven3in;C:ProgramDataOracleJavajavapath;D:UsersAdministratorinSenchaCmd5.1.0.26;C:Program Files (x86)AMD APPinx86_64;C:Program Files (x86)AMD APPinx86;C:WINDOWSsystem32;C:WINDOWS;C:WINDOWSSystem32Wbem;C:WINDOWSSystem32WindowsPowerShellv1.0;C:Program FilesJavajdk1.8.0_25in;C:Program FilesJavajdk1.8.0_25jrein;D:developingmysqlin;C:Program Files (x86)IVI FoundationIVIin;C:Program FilesIVI FoundationIVIin;C:PROGRA~2IVIFOU~1VISAWinNTBin;C:Program Files (x86)IVI FoundationVISAWinNTBin;C:Program FilesIVI FoundationVISAWin64Bin;C:Program Files (x86)IVI FoundationVISAWinNTBin;C:Program Files (x86)ATI TechnologiesATI.ACECore-Static;C:Program FilesAgilentIO Libraries Suitein;C:Program FilesIVI FoundationVISAWin64agvisa;C:Program Files (x86)AgilentIO Libraries Suitein;C:Program Files (x86)IVI FoundationVISAWinNTagvisa;D:developingapache-maven-3.2.2in;D:Program Files (x86)Gitcmd;D:Program Files (x86)Gitin;C:Program FilesAntapache-ant-1.9.7in;D:Program FilesTortoiseSVNin;D:developingeclipse;;. 3 二月 23, 2017 2:36:26 下午 org.apache.tomcat.util.digester.SetPropertiesRule begin 4 警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:projectManagement' did not find a matching property. 5 二月 23, 2017 2:36:26 下午 org.apache.coyote.AbstractProtocol init 6 信息: Initializing ProtocolHandler ["http-nio-89"] 7 二月 23, 2017 2:36:26 下午 org.apache.tomcat.util.net.NioSelectorPool getSharedSelector 8 信息: Using a shared selector for servlet write/read 9 二月 23, 2017 2:36:26 下午 org.apache.coyote.AbstractProtocol init 10 信息: Initializing ProtocolHandler ["ajp-nio-8009"] 11 二月 23, 2017 2:36:26 下午 org.apache.tomcat.util.net.NioSelectorPool getSharedSelector 12 信息: Using a shared selector for servlet write/read 13 二月 23, 2017 2:36:26 下午 org.apache.catalina.startup.Catalina load 14 信息: Initialization processed in 1036 ms 15 二月 23, 2017 2:36:26 下午 org.apache.catalina.core.StandardService startInternal 16 信息: Starting service Catalina 17 二月 23, 2017 2:36:26 下午 org.apache.catalina.core.StandardEngine startInternal 18 信息: Starting Servlet Engine: Apache Tomcat/8.0.9 19 二月 23, 2017 2:36:27 下午 org.apache.catalina.util.SessionIdGenerator createSecureRandom 20 信息: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [117] milliseconds. 21 二月 23, 2017 2:36:34 下午 org.apache.catalina.core.ApplicationContext log 22 信息: No Spring WebApplicationInitializer types detected on classpath 23 SLF4J: Class path contains multiple SLF4J bindings. 24 SLF4J: Found binding in [jar:file:/D:/developing/eclipse/workspace/ProjectManagement/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/projectManagement/WEB-INF/lib/slf4j-log4j12-1.7.6.jar!/org/slf4j/impl/StaticLoggerBinder.class] 25 SLF4J: Found binding in [jar:file:/D:/developing/eclipse/workspace/ProjectManagement/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/projectManagement/WEB-INF/lib/slf4j-log4j12-1.7.7.jar!/org/slf4j/impl/StaticLoggerBinder.class] 26 SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation. 27 SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory] 28 二月 23, 2017 2:36:35 下午 org.apache.catalina.core.ApplicationContext log 29 信息: Initializing Spring root WebApplicationContext 30 [org.springframework.web.context.ContextLoader] - Root WebApplicationContext: initialization started 31 [org.springframework.web.context.support.XmlWebApplicationContext] - Refreshing Root WebApplicationContext: startup date [Thu Feb 23 14:36:35 CST 2017]; root of context hierarchy 32 [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - Loading XML bean definitions from class path resource [spring-mybatis.xml] 33 [org.springframework.beans.factory.config.PropertyPlaceholderConfigurer] - Loading properties file from class path resource [jdbc.properties] 34 [org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor] - JSR-330 'javax.inject.Inject' annotation found and supported for autowiring 35 [org.springframework.web.context.ContextLoader] - Root WebApplicationContext: initialization completed in 2081 ms 36 二月 23, 2017 2:36:37 下午 org.apache.catalina.core.ApplicationContext log 37 信息: Initializing Spring FrameworkServlet 'SpringMVC' 38 [org.springframework.web.servlet.DispatcherServlet] - FrameworkServlet 'SpringMVC': initialization started 39 [org.springframework.web.context.support.XmlWebApplicationContext] - Refreshing WebApplicationContext for namespace 'SpringMVC-servlet': startup date [Thu Feb 23 14:36:37 CST 2017]; parent: Root WebApplicationContext 40 [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - Loading XML bean definitions from class path resource [spring-mvc.xml] 41 [org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor] - JSR-330 'javax.inject.Inject' annotation found and supported for autowiring 42 [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] - Mapped URL path [/user/showUser] onto handler 'userController' 43 [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] - Mapped URL path [/user/showUser.*] onto handler 'userController' 44 [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] - Mapped URL path [/user/showUser/] onto handler 'userController' 45 [org.springframework.web.servlet.DispatcherServlet] - FrameworkServlet 'SpringMVC': initialization completed in 1254 ms 46 二月 23, 2017 2:36:38 下午 org.apache.coyote.AbstractProtocol start 47 信息: Starting ProtocolHandler ["http-nio-89"] 48 二月 23, 2017 2:36:38 下午 org.apache.coyote.AbstractProtocol start 49 信息: Starting ProtocolHandler ["ajp-nio-8009"] 50 二月 23, 2017 2:36:38 下午 org.apache.catalina.startup.Catalina start 51 信息: Server startup in 12041 ms
输入地址:localhost:端口号/项目名称/user/showUser?id=1
如图,这样,基本的框架就搭建完成了!
4,过程错误
4.1情况1:
Project configuration is not up-to-date with pom.xml. Run Maven->Update Project or use Quick Fix.
在Problems View的出错提示右键选Quick Fix,再按提示确定就OK.或者,右键项目->Maven->Update Project
4.2情况2:
JavaServer Faces 2.2 can not be installed : One or more constraints have not been satisfied.
JavaServer Faces 2.2 requires Dynamic Web Module 2.5 or newer.
先改web.xmlwebapp节点:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
</web-app>
然后关闭Eclipse,改项目下的.settingsorg.eclipse.wst.common.project.facet.core.xml.将版本改成为3.1,将成后是<installed facet="jst.web" version="3.1"/>,再启动Eclipse.最后出现上面的情况1,按上面更新下配置就OK.
4.3情况3:
情况3:
JDK版本设置:
pom.xml配置
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>