完整搭建SSM框架之黑暗中的摸索
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了完整搭建SSM框架之黑暗中的摸索相关的知识,希望对你有一定的参考价值。
JavaWeb基础该学的都了解过一点了,那么必须要在项目中来练习和升华了(就像高三对数理化的整合练习!)
一共分为三个步骤:
1.环境的选择和准备
2.工程的创建
3.代码和配置文件的创建
- 环境
- 工程创建
在pom.xml文件中加入依赖包(去除index.jsp的报错)
<!-- 导入java ee jar 包 -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>
搞成如下图的样子:
然后,再到属性里边来。调整发布的配置。
- 代码填入
jar包版本:Spring 4.0.2 RELEASE、Spring MVC 4.0.2 RELEASE、MyBatis 3.2.6
pom.xml文件中的所有jar包:(ctrl+s后会自动引入jar包)
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>com.tydic</groupId> 5 <artifactId>testSSM3</artifactId> 6 <packaging>war</packaging> 7 <version>0.0.1-SNAPSHOT</version> 8 <name>testSSM3 Maven Webapp</name> 9 <url>http://maven.apache.org</url> 10 <properties> 11 <!-- spring版本号 --> 12 <spring.version>4.0.2.RELEASE</spring.version> 13 <!-- mybatis版本号 --> 14 <mybatis.version>3.2.6</mybatis.version> 15 <!-- log4j日志文件管理包版本 --> 16 <slf4j.version>1.7.7</slf4j.version> 17 <log4j.version>1.2.17</log4j.version> 18 </properties> 19 20 <dependencies> 21 <dependency> 22 <groupId>junit</groupId> 23 <artifactId>junit</artifactId> 24 <version>3.8.1</version> 25 <scope>test</scope> 26 </dependency> 27 28 <!-- 导入java ee jar 包 --> 29 <dependency> 30 <groupId>javax</groupId> 31 <artifactId>javaee-api</artifactId> 32 <version>7.0</version> 33 </dependency> 34 35 <dependency> 36 <groupId>junit</groupId> 37 <artifactId>junit</artifactId> 38 <version>4.11</version> 39 <!-- 表示开发的时候引入,发布的时候不会加载此包 --> 40 <scope>test</scope> 41 </dependency> 42 <!-- spring核心包 --> 43 <dependency> 44 <groupId>org.springframework</groupId> 45 <artifactId>spring-core</artifactId> 46 <version>${spring.version}</version> 47 </dependency> 48 49 <dependency> 50 <groupId>org.springframework</groupId> 51 <artifactId>spring-web</artifactId> 52 <version>${spring.version}</version> 53 </dependency> 54 <dependency> 55 <groupId>org.springframework</groupId> 56 <artifactId>spring-oxm</artifactId> 57 <version>${spring.version}</version> 58 </dependency> 59 <dependency> 60 <groupId>org.springframework</groupId> 61 <artifactId>spring-orm</artifactId> 62 <version>${spring.version}</version> 63 </dependency> 64 <dependency> 65 <groupId>org.springframework</groupId> 66 <artifactId>spring-tx</artifactId> 67 <version>${spring.version}</version> 68 </dependency> 69 70 <dependency> 71 <groupId>org.springframework</groupId> 72 <artifactId>spring-jdbc</artifactId> 73 <version>${spring.version}</version> 74 </dependency> 75 76 <dependency> 77 <groupId>org.springframework</groupId> 78 <artifactId>spring-webmvc</artifactId> 79 <version>${spring.version}</version> 80 </dependency> 81 <dependency> 82 <groupId>org.springframework</groupId> 83 <artifactId>spring-aop</artifactId> 84 <version>${spring.version}</version> 85 </dependency> 86 87 <dependency> 88 <groupId>org.springframework</groupId> 89 <artifactId>spring-context-support</artifactId> 90 <version>${spring.version}</version> 91 </dependency> 92 93 <dependency> 94 <groupId>org.springframework</groupId> 95 <artifactId>spring-context</artifactId> 96 <version>${spring.version}</version> 97 </dependency> 98 99 <dependency> 100 <groupId>org.springframework</groupId> 101 <artifactId>spring-beans</artifactId> 102 <version>${spring.version}</version> 103 </dependency> 104 105 <dependency> 106 <groupId>org.springframework</groupId> 107 <artifactId>spring-test</artifactId> 108 <version>${spring.version}</version> 109 </dependency> 110 <!-- mybatis核心包 --> 111 <dependency> 112 <groupId>org.mybatis</groupId> 113 <artifactId>mybatis</artifactId> 114 <version>${mybatis.version}</version> 115 </dependency> 116 <!-- mybatis/spring包 --> 117 <dependency> 118 <groupId>org.mybatis</groupId> 119 <artifactId>mybatis-spring</artifactId> 120 <version>1.2.2</version> 121 </dependency> 122 <!-- 导入java ee jar 包 --> 123 <dependency> 124 <groupId>javax</groupId> 125 <artifactId>javaee-api</artifactId> 126 <version>7.0</version> 127 </dependency> 128 <!-- 导入mysql数据库链接jar包 --> 129 <dependency> 130 <groupId>mysql</groupId> 131 <artifactId>mysql-connector-java</artifactId> 132 <version>5.1.30</version> 133 </dependency> 134 <!-- 导入dbcp的jar包,用来在applicationContext.xml中配置数据库 --> 135 <dependency> 136 <groupId>commons-dbcp</groupId> 137 <artifactId>commons-dbcp</artifactId> 138 <version>1.2.2</version> 139 </dependency> 140 <!-- JSTL标签类 --> 141 <dependency> 142 <groupId>jstl</groupId> 143 <artifactId>jstl</artifactId> 144 <version>1.2</version> 145 </dependency> 146 <!-- 日志文件管理包 --> 147 <!-- log start --> 148 <dependency> 149 <groupId>log4j</groupId> 150 <artifactId>log4j</artifactId> 151 <version>${log4j.version}</version> 152 </dependency> 153 154 155 <!-- 格式化对象,方便输出日志 --> 156 <dependency> 157 <groupId>com.alibaba</groupId> 158 <artifactId>fastjson</artifactId> 159 <version>1.1.41</version> 160 </dependency> 161 162 <dependency> 163 <groupId>com.tydic.framework</groupId> 164 <artifactId>fw-log</artifactId> 165 <version>1.4</version> 166 </dependency> 167 168 <dependency> 169 <groupId>org.slf4j</groupId> 170 <artifactId>slf4j-api</artifactId> 171 <version>${slf4j.version}</version> 172 </dependency> 173 174 <dependency> 175 <groupId>org.aspectj</groupId> 176 <artifactId>aspectjweaver</artifactId> 177 <version>1.7.4</version> 178 </dependency> 179 180 <dependency> 181 <groupId>org.slf4j</groupId> 182 <artifactId>slf4j-log4j12</artifactId> 183 <version>${slf4j.version}</version> 184 </dependency> 185 <!-- log end --> 186 <!-- 映入JSON --> 187 <dependency> 188 <groupId>org.codehaus.jackson</groupId> 189 <artifactId>jackson-mapper-asl</artifactId> 190 <version>1.9.13</version> 191 </dependency> 192 <!-- 上传组件包 --> 193 <dependency> 194 <groupId>commons-fileupload</groupId> 195 <artifactId>commons-fileupload</artifactId> 196 <version>1.3.1</version> 197 </dependency> 198 <dependency> 199 <groupId>commons-io</groupId> 200 <artifactId>commons-io</artifactId> 201 <version>2.4</version> 202 </dependency> 203 <dependency> 204 <groupId>commons-codec</groupId> 205 <artifactId>commons-codec</artifactId> 206 <version>1.9</version> 207 </dependency> 208 209 <dependency> 210 <groupId>com.tydic.framework</groupId> 211 <artifactId>fw-lang</artifactId> 212 <version>1.4</version> 213 </dependency> 214 215 <dependency> 216 <groupId>commons-beanutils</groupId> 217 <artifactId>commons-beanutils</artifactId> 218 <version>1.8.1</version> 219 </dependency> 220 221 <dependency> 222 <groupId>joda-time</groupId> 223 <artifactId>joda-time</artifactId> 224 <version>2.1</version> 225 </dependency> 226 227 <dependency> 228 <groupId>org.slf4j</groupId> 229 <artifactId>jcl-over-slf4j</artifactId> 230 <version>1.7.6</version> 231 </dependency> 232 233 <dependency> 234 <groupId>org.apache.commons</groupId> 235 <artifactId>commons-lang3</artifactId> 236 <version>3.1</version> 237 </dependency> 238 239 <dependency> 240 <groupId>javax.annotation</groupId> 241 <artifactId>javax.annotation-api</artifactId> 242 <version>1.2</version> 243 </dependency> 244 245 </dependencies> 246 <build> 247 <finalName>testSSM3</finalName> 248 </build> 249 </project>
spring和mybatis的整合
jdbc.properties(注意行末不要有空格)
1 driver=com.mysql.jdbc.Driver 2 #貌似也可以用自己的ip地址 3 url=jdbc:mysql://127.0.0.1:3306/chenyu_db 4 username=root 5 password= 6 #定义初始连接数 7 initialSize=0 8 #定义最大连接数 9 maxActive=20 10 #定义最大空闲 11 maxIdle=20 12 #定义最小空闲 13 minIdle=1 14 #定义最长等待时间 15 maxWait=60000
log4j.properties
1 log4j.rootLogger=INFO,Console,File 2 #定义日志输出目的地为控制台 3 log4j.appender.Console=org.apache.log4j.ConsoleAppender 4 log4j.appender.Console.Target=System.out 5 #可以灵活地指定日志输出格式,下面一行是指定具体的格式 6 log4j.appender.Console.layout = org.apache.log4j.PatternLayout 7 log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n 8 9 #文件大小到达指定尺寸的时候产生一个新的文件 10 log4j.appender.File = org.apache.log4j.RollingFileAppender 11 #指定输出目录 12 log4j.appender.File.File = logs/ssm.log 13 #定义文件最大大小 14 log4j.appender.File.MaxFileSize = 10MB 15 # 输出所以日志,如果换成DEBUG表示输出DEBUG以上级别日志 16 log4j.appender.File.Threshold = ALL 17 log4j.appender.File.layout = org.apache.log4j.PatternLayout 18 log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\\:mm\\:ss}][%c]%m%n
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.tydic.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>
spring-mybatis.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 <!-- 自动扫描 --> 13 <context:component-scan base-package="com.tydic" /> 14 <!-- 引入配置文件 --> 15 <bean id="propertyConfigurer" 16 class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 17 <property name="location" value="classpath:jdbc.properties" /> 18 </bean> 19 20 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 21 destroy-method="close"> 22 <property name="driverClassName" value="${driver}" /> 23 <property name="url" value="${url}" /> 24 <property name="username" value="${username}" /> 25 <property name="password" value="${password}" /> 26 <!-- 初始化连接大小 --> 27 <property name="initialSize" value="${initialSize}"></property> 28 <!-- 连接池最大数量 --> 29 <property name="maxActive" value="${maxActive}"></property> 30 <!-- 连接池最大空闲 --> 31 <property name="maxIdle" value="${maxIdle}"></property> 32 <!-- 连接池最小空闲 --> 33 <property name="minIdle" value="${minIdle}"></property> 34 <!-- 获取连接最大等待时间 --> 35 <property name="maxWait" value="${maxWait}"></property> 36 </bean> 37 38 <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 --> 39 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 40 <property name="dataSource" ref="dataSource" /> 41 <!-- 自动扫描mapping.xml文件 --> 42 <property name="mapperLocations" value="classpath:com/tydic/mapping/*.xml"></property> 43 </bean> 44 45 <!-- DAO接口所在包名,Spring会自动查找其下的类 --> 46 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 47 <property name="basePackage" value="com.tydic.dao" /> 48 <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> 49 </bean> 50 51 <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx --> 52 <bean id="transactionManager" 53 class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 54 <property name="dataSource" ref="dataSource" /> 55 </bean> 56 57 </beans>
开启mysql数据库
创建表和数据
1 DROP TABLE IF EXISTS `user_t`; 2 3 CREATE TABLE `user_t` ( 4 `id` int(11) NOT NULL AUTO_INCREMENT, 5 `user_name` varchar(40) NOT NULL, 6 `password` varchar(255) NOT NULL, 7 `age` int(4) NOT NULL, 8 PRIMARY KEY (`id`) 9 ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; 10 11 /*Data for the table `user_t` */ 12 13 insert into `user_t`(`id`,`user_name`,`password`,`age`) values (1,‘测试‘,‘sfasgfaf‘,24);
完成了Spring和mybatis的整合,开始JUnit测试
利用MyBatis Generator自动创建代码http://blog.csdn.net/zhshulin/article/details/23912615。总之代码如下
UserMapper.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > 3 <mapper namespace="com.tydic.dao.IUserDao" > 4 <resultMap id="BaseResultMap" type="com.tydic.domain.User" > 5 <id column="id" property="id" jdbcType="INTEGER" /> 6 <result column="user_name" property="userName" jdbcType="VARCHAR" /> 7 <result column="password" property="password" jdbcType="VARCHAR" /> 8 <result column="age" property="age" jdbcType="INTEGER" /> 9 </resultMap> 10 <sql id="Base_Column_List" > 11 id, user_name, password, age 12 </sql> 13 <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" > 14 select 15 <include refid="Base_Column_List" /> 16 from user_t 17 where id = #{id,jdbcType=INTEGER} 18 </select> 19 <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" > 20 delete from user_t 21 where id = #{id,jdbcType=INTEGER} 22 </delete> 23 <insert id="insert" parameterType="com.tydic.domain.User" > 24 insert into user_t (id, user_name, password, 25 age) 26 values (#{id,jdbcType=INTEGER}, #{userName,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, 27 #{age,jdbcType=INTEGER}) 28 </insert> 29 <insert id="insertSelective" parameterType="com.tydic.domain.User" > 30 insert into user_t 31 <trim prefix="(" suffix=")" suffixOverrides="," > 32 <if test="id != null" > 33 id, 34 </if> 35 <if test="userName != null" > 36 user_name, 37 </if> 38 <if test="password != null" > 39 password, 40 </if> 41 <if test="age != null" > 42 age, 43 </if> 44 </trim> 45 <trim prefix="values (" suffix=")" suffixOverrides="," > 46 <if test="id != null" > 47 #{id,jdbcType=INTEGER}, 48 </if> 49 <if test="userName != null" > 50 #{userName,jdbcType=VARCHAR}, 51 </if> 52 <if test="password != null" > 53 #{password,jdbcType=VARCHAR}, 54 </if> 55 <if test="age != null" > 56 #{age,jdbcType=INTEGER}, 57 </if> 58 </trim> 59 </insert> 60 <update id="updateByPrimaryKeySelective" parameterType="com.tydic.domain.User" > 61 update user_t 62 <set > 63 <if test="userName != null" > 64 user_name = #{userName,jdbcType=VARCHAR}, 65 </if> 66 <if test="password != null" > 67 password = #{password,jdbcType=VARCHAR}, 68 </if> 69 <if test="age != null" > 70 age = #{age,jdbcType=INTEGER}, 71 </if> 72 </set> 73 where id = #{id,jdbcType=INTEGER} 74 </update> 75 <update id="updateByPrimaryKey" parameterType="com.tydic.domain.User" > 76 update user_t 77 set user_name = #{userName,jdbcType=VARCHAR}, 78 password = #{password,jdbcType=VARCHAR}, 79 age = #{age,jdbcType=INTEGER} 80 where id = #{id,jdbcType=INTEGER} 81 </update> 82 </mapper>
User.java
1 package com.tydic.domain; 2 3 public class User { 4 private Integer id; 5 6 private String userName; 7 8 private String password; 9 10 private Integer age; 11 12 public Integer getId() { 13 return id; 14 } 15 16 public void setId(Integer id) { 17 this.id = id; 18 } 19 20 public String getUserName() { 21 return userName; 22 } 23 24 public void setUserName(String userName) { 25 this.userName = userName == null ? null : userName.trim(); 26 } 27 28 public String getPassword() { 29 return password; 30 } 31 32 public void setPassword(String password) { 33 this.password = password == null ? null : password.trim(); 34 } 35 36 public Integer getAge() { 37 return age; 38 } 39 40 public void setAge(Integer age) { 41 this.age = age; 42 } 43 }
IUserDao.java
1 package com.tydic.dao; 2 3 import com.tydic.domain.User; 4 5 public interface IUserDao { 6 int deleteByPrimaryKey(Integer id); 7 8 int insert(User record); 9 10 int insertSelective(User record); 11 12 User selectByPrimaryKey(Integer id); 13 14 int updateByPrimaryKeySelective(User record); 15 16 int updateByPrimaryKey(User record); 17 }
IUserService.java
1 package com.tydic.service; 2 3 import com.tydic.domain.User; 4 5 public interface IUserService { 6 public User getUserById(int userId); 7 }
UserServiceImpl.java
1 package com.tydic.service.impl; 2 3 import javax.annotation.Resource; 4 5 import org.springframework.stereotype.Service; 6 7 import com.tydic.dao.IUserDao; 8 import com.tydic.domain.User; 9 import com.tydic.service.IUserService; 10 11 @Service("userService") 12 public class UserServiceImpl implements IUserService { 13 @Resource 14 private IUserDao userDao; 15 @Override 16 public User getUserById(int userId) { 17 return this.userDao.selectByPrimaryKey(userId); 18 } 19 }
测试类
SSMMybatisTest.java
1 import javax.annotation.Resource; 2 3 import org.apache.log4j.Logger; 4 import org.junit.Test; 5 import org.junit.Before; 6 import org.junit.runner.RunWith; 7 import org.springframework.context.ApplicationContext; 8 import org.springframework.context.support.ClassPathXmlApplicationContext; 9 import org.springframework.test.context.ContextConfiguration; 10 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 11 12 import com.alibaba.fastjson.JSON; 13 import com.tydic.domain.User; 14 import com.tydic.service.IUserService; 15 16 @RunWith(SpringJUnit4ClassRunner.class) //表示继承了SpringJUnit4ClassRunner类 17 @ContextConfiguration(locations = {"classpath:spring-mybatis.xml"}) 18 19 public class SSMMybatisTest { 20 21 private static Logger logger = Logger.getLogger(SSMMybatisTest.class); 22 23 @Resource 24 private IUserService userService = null; 25 //private IUserService userService; 26 27 /*//注释掉 28 private ApplicationContext ac = null; 29 //注释掉 30 @Before 31 public void before() { 32 ac = new ClassPathXmlApplicationContext("applicationContext.xml"); 33 userService = (IUserService) ac.getBean("userService"); 34 }*/ 35 36 @Test 37 public void test1() { 38 User user = userService.getUserById(1); 39 System.out.println(user.getUserName()); 40 logger.info("值:" + user.getUserName()); 41 logger.info(JSON.toJSONString(user)); 42 } 43 }
测试成功
然后整合SpringMVC
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://java.sun.com/xml/ns/javaee" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 5 version="3.0"> 6 <display-name>Archetype Created Web Application</display-name> 7 <!-- Spring和mybatis的配置文件 --> 8 <context-param> 9 <param-name>contextConfigLocation</param-name> 10 <param-value>classpath:spring-mybatis.xml</param-value> 11 </context-param> 12 <!-- 编码过滤器 --> 13 <filter> 14 <filter-name>encodingFilter</filter-name> 15 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 16 <async-supported>true</async-supported> 17 <init-param> 18 <param-name>encoding</param-name> 19 <param-value>UTF-8</param-value> 20 </init-param> 21 </filter> 22 <filter-mapping> 23 <filter-name>encodingFilter</filter-name> 24 <url-pattern>/*</url-pattern> 25 </filter-mapping> 26 <!-- Spring监听器 --> 27 <listener> 28 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 29 </listener> 30 <!-- 防止Spring内存溢出监听器 --> 31 <listener> 32 <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> 33 </listener> 34 35 <!-- Spring MVC servlet --> 36 <servlet> 37 <servlet-name>SpringMVC</servlet-name> 38 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 39 <init-param> 40 <param-name>contextConfigLocation</param-name> 41 <param-value>classpath:spring-mvc.xml</param-value> 42 </init-param> 43 <load-on-startup>1</load-on-startup> 44 <async-supported>true</async-supported> 45 </servlet> 46 <servlet-mapping> 47 <servlet-name>SpringMVC</servlet-name> 48 <!-- 此处可以可以配置成*.do,对应struts的后缀习惯 --> 49 <url-pattern>/</url-pattern> 50 </servlet-mapping> 51 <welcome-file-list> 52 <welcome-file>/index.jsp</welcome-file> 53 </welcome-file-list> 54 55 </web-app>
测试
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 </body> 11 </html>
UserController.java
1 package com.tydic.controller; 2 3 import javax.annotation.Resource; 4 import javax.servlet.http.HttpServletRequest; 5 6 import org.springframework.stereotype.Controller; 7 import org.springframework.ui.Model; 8 import org.springframework.web.bind.annotation.RequestMapping; 9 10 import com.tydic.domain.User; 11 import com.tydic.service.IUserService; 12 13 14 @Controller 15 @RequestMapping("/user") 16 public class UserController { 17 @Resource 18 private IUserService userService; 19 20 @RequestMapping("/showUser") 21 public String toIndex(HttpServletRequest request,Model model){ 22 int userId = Integer.parseInt(request.getParameter("id")); 23 User user = this.userService.getUserById(userId); 24 model.addAttribute("user", user); 25 return "showUser"; 26 } 27 }
打war包(先刷新、clean工程)
得到war包
拷到Tomcat下
启动服务器
顺利的话,成功开启
在浏览器测试
http://localhost:9080/testSSM3/index.jsp
http://localhost:9080/testSSM3/user/showUser?id=1
成功搭建SSM框架!
本文灵感来自
http://blog.csdn.net/gebitan505/article/details/44455235/
http://blog.csdn.net/qq_25730711/article/details/51094899
http://blog.csdn.net/chuyuqing/article/details/28879477
谢谢分享!
以上是关于完整搭建SSM框架之黑暗中的摸索的主要内容,如果未能解决你的问题,请参考以下文章