maven+springMVC+mybatis+junit详细搭建过程
2014-04-15 16:41
369 查看
http://www.open-open.com/lib/view/open1392252233301.html
(假设效果:项目目录结构清晰,能够查询到本地数据库中的内容。。)
cn.springmvc.dao(存放spring与mybatis连接接口),
cn.springmvc.service(service接口),
cn.springmvc.service.impl(service接口的实现),
cn.springmvc.controller(存放控制层controller)
在src/main/resource文件夹中,新建包conf(存放配置文件),
mapper(mybatis的mapper文件)
在src/test/java文件夹中,新建包cn.springmvc.test(存放测试文件)
在WEB-INF文件夹下新建jsp文件夹(存放jsp文件)
这样项目结构基本完成了
查找依赖结构有个不错的网站,http://search.maven.org/只要输入包名即可查找引来关系
pom.xml(包依赖)
conf/spring-mybatis.xml(spring与mybatis连接属性)
cn.springmvc.dao/UserDAO.java(dao操作接口)
cn.springmvc.service/UserService.java(service层接口)
cn.springmvc.service.impl/UserServiceImpl.java(service层接口实现)
mapper/UserMapper.xml(mybatis的实现)
这样就完成了基本插入用户的功能!
测试结果如图:
即完成插入功能!
conf/spring-mvc.xml(mvc配置文件)
cn.springmvc.controller/UserComtroller.java(controller层控制)
将该项目部署到本地tomcat6中
右击该项目选择Runas,选择RunonServer
选择自己建立的tomcat6运行如图:
控制台打印出日志如图(启动成功):
在浏览器中输入:http://localhost:8080/springmvc/index.do进行测试
效果如图(返回成功):
springMVC+mybatis框架搭建
首先我们先要弄清搭建项目的一般流程,需要注意哪些方面,想要什么样的效果,自己的功能有哪些?(假设效果:项目目录结构清晰,能够查询到本地数据库中的内容。。)
1.工程目录结构整理清楚
在src/main/java文件夹中,新建包cn.springmvc.model(存放javabean),cn.springmvc.dao(存放spring与mybatis连接接口),
cn.springmvc.service(service接口),
cn.springmvc.service.impl(service接口的实现),
cn.springmvc.controller(存放控制层controller)
在src/main/resource文件夹中,新建包conf(存放配置文件),
mapper(mybatis的mapper文件)
在src/test/java文件夹中,新建包cn.springmvc.test(存放测试文件)
在WEB-INF文件夹下新建jsp文件夹(存放jsp文件)
这样项目结构基本完成了
2.引入依赖包
打开maven的pom文件,对本次开发所需使用的架包依次导入(maven项目管理的优势)查找依赖结构有个不错的网站,
pom.xml(包依赖)
001 | < project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" |
002 | xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0 | 'target='_blank'>http://maven.apache.org/maven-v4_0_0.xsd"[/code]
003 | < modelVersion >4.0.0</ modelVersion > |
004 | < groupId >eyas.springmvc</ groupId > |
005 | < artifactId >springmvc</ artifactId > |
006 | < packaging >war</ packaging > |
007 | < version >0.0.1-SNAPSHOT</ version > |
008 | < name >springmvcMavenWebapp</ name > |
009 | < url >http://maven.apache.org</ url > |
010 | < properties > |
011 | <!--spring版本号--> |
012 | < spring.version >3.2.4.RELEASE</ spring.version > |
013 | <!--mybatis版本号--> |
014 | < mybatis.version >3.2.4</ mybatis.version > |
015 | <!--log4j日志文件管理包版本--> |
016 | < slf4j.version >1.6.6</ slf4j.version > |
017 | < log4j.version >1.2.9</ log4j.version > |
018 | </ properties > |
019 | < dependencies > |
020 | <!--spring核心包--> |
021 | <!--springframestart--> |
022 | < dependency > |
023 | < groupId >org.springframework</ groupId > |
024 | < artifactId >spring-core</ artifactId > |
025 | < version >${spring.version}</ version > |
026 | </ dependency > |
027 |
028 | < dependency > |
029 | < groupId >org.springframework</ groupId > |
030 | < artifactId >spring-web</ artifactId > |
031 | < version >${spring.version}</ version > |
032 | </ dependency > |
033 |
034 | < dependency > |
035 | < groupId >org.springframework</ groupId > |
036 | < artifactId >spring-oxm</ artifactId > |
037 | < version >${spring.version}</ version > |
038 | </ dependency > |
039 |
040 | < dependency > |
041 | < groupId >org.springframework</ groupId > |
042 | < artifactId >spring-tx</ artifactId > |
043 | < version >${spring.version}</ version > |
044 | </ dependency > |
045 |
046 | < dependency > |
047 | < groupId >org.springframework</ groupId > |
048 | < artifactId >spring-jdbc</ artifactId > |
049 | < version >${spring.version}</ version > |
050 | </ dependency > |
051 |
052 | < dependency > |
053 | < groupId >org.springframework</ groupId > |
054 | < artifactId >spring-webmvc</ artifactId > |
055 | < version >${spring.version}</ version > |
056 | </ dependency > |
057 |
058 | < dependency > |
059 | < groupId >org.springframework</ groupId > |
060 | < artifactId >spring-aop</ artifactId > |
061 | < version >${spring.version}</ version > |
062 | </ dependency > |
063 |
064 | < dependency > |
065 | < groupId >org.springframework</ groupId > |
066 | < artifactId >spring-context-support</ artifactId > |
067 | < version >${spring.version}</ version > |
068 | </ dependency > |
069 |
070 | < dependency > |
071 | < groupId >org.springframework</ groupId > |
072 | < artifactId >spring-aop</ artifactId > |
073 | < version >${spring.version}</ version > |
074 | </ dependency > |
075 |
076 | < dependency > |
077 | < groupId >org.springframework</ groupId > |
078 | < artifactId >spring-test</ artifactId > |
079 | < version >${spring.version}</ version > |
080 | </ dependency > |
081 | <!--springframeend--> |
082 |
083 | <!--mybatis核心包--> |
084 | < dependency > |
085 | < groupId >org.mybatis</ groupId > |
086 | < artifactId >mybatis</ artifactId > |
087 | < version >${mybatis.version}</ version > |
088 | </ dependency > |
089 | <!--mybatis/spring包--> |
090 | < dependency > |
091 | < groupId >org.mybatis</ groupId > |
092 | < artifactId >mybatis-spring</ artifactId > |
093 | < version >1.2.2</ version > |
094 | </ dependency > |
095 | <!--mysql驱动包--> |
096 | < dependency > |
097 | < groupId >mysql</ groupId > |
098 | < artifactId >mysql-connector-java</ artifactId > |
099 | < version >5.1.29</ version > |
100 | </ dependency > |
101 | <!--junit测试包--> |
102 | < dependency > |
103 | < groupId >junit</ groupId > |
104 | < artifactId >junit</ artifactId > |
105 | < version >4.11</ version > |
106 | < scope >test</ scope > |
107 | </ dependency > |
108 | <!--阿里巴巴数据源包--> |
109 | < dependency > |
110 | < groupId >com.alibaba</ groupId > |
111 | < artifactId >druid</ artifactId > |
112 | < version >1.0.2</ version > |
113 | </ dependency > |
114 |
115 | <!--json数据--> |
116 | < dependency > |
117 | < groupId >org.codehaus.jackson</ groupId > |
118 | < artifactId >jackson-mapper-asl</ artifactId > |
119 | < version >1.9.13</ version > |
120 | </ dependency > |
121 |
122 | <!--日志文件管理包--> |
123 | <!--logstart--> |
124 | < dependency > |
125 | < groupId >log4j</ groupId > |
126 | < artifactId >log4j</ artifactId > |
127 | < version >${log4j.version}</ version > |
128 | </ dependency > |
129 | < dependency > |
130 | < groupId >org.slf4j</ groupId > |
131 | < artifactId >slf4j-api</ artifactId > |
132 | < version >${slf4j.version}</ version > |
133 | </ dependency > |
134 | < dependency > |
135 | < groupId >org.slf4j</ groupId > |
136 | < artifactId >slf4j-log4j12</ artifactId > |
137 | < version >${slf4j.version}</ version > |
138 | </ dependency > |
139 | <!--logend--> |
140 | </ dependencies > |
141 | < build > |
142 | < finalName >springmvc</ finalName > |
143 | </ build > |
144 | </ project > |
3.配置数据库连接属性
conf/jdbc.properties(jdbc配置文件)1 | jdbc_driverClassName=com.mysql.jdbc.Driver |
2 | jdbc_url=jdbc:mysql: //localhost:3306/mydays?useUnicode=true&characterEncoding=utf-8 |
3 | jdbc_username=root |
4 | jdbc_password=root |
4.配置spring配置文件
conf/spring.xml(spring配置文件的扫描)01 | <? xml version = "1.0" encoding = "UTF-8" ?> |
02 | < beans xmlns = "http://www.springframework.org/schema/beans" |
03 | xmlns:context = "http://www.springframework.org/schema/context" |
04 | xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" |
05 | xsi:schemaLocation="http://www.springframework.org/schema/beans |
06 | http://www.springframework.org/schema/beans/spring-beans-3.0.xsd |
07 | http://www.springframework.org/schema/context |
08 | http://www.springframework.org/schema/context/spring-context.xsd"> |
09 |
10 | <!--引入jdbc配置文件--> |
11 | < context:property-placeholder location = "classpath:conf/jdbc.properties" /> |
12 |
13 | <!--扫描文件(自动将servicec层注入)--> |
14 | < context:component-scan base-package = "cn.springmvc.service" /> |
15 | </ beans > |
01 | <? xml version = "1.0" encoding = "UTF-8" ?> |
02 | < beans xmlns = "http://www.springframework.org/schema/beans" |
03 | xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" |
04 | xmlns:p = "http://www.springframework.org/schema/p" |
05 | xmlns:context = "http://www.springframework.org/schema/context" |
06 | xmlns:aop = "http://www.springframework.org/schema/aop" |
07 | xmlns:tx = "http://www.springframework.org/schema/tx" |
08 | xmlns:util = "http://www.springframework.org/schema/util" |
09 | xsi:schemaLocation="http://www.springframework.org/schema/beans |
10 | http://www.springframework.org/schema/beans/spring-beans-3.2.xsd |
11 | http://www.springframework.org/schema/context |
12 | http://www.springframework.org/schema/context/spring-context-3.2.xsd |
13 | http://www.springframework.org/schema/tx |
14 | http://www.springframework.org/schema/tx/spring-tx-3.2.xsd |
15 | http://www.springframework.org/schema/aop |
16 | http://www.springframework.org/schema/aop/spring-aop-3.2.xsd |
17 | http://www.springframework.org/schema/util |
18 | http://www.springframework.org/schema/util/spring-util-3.2.xsd"> |
19 |
20 | < bean id = "dataSource" class = "com.alibaba.druid.pool.DruidDataSource" init-method = "init" |
21 | destroy-method = "close" > |
22 | < property name = "driverClassName" > |
23 | < value >${jdbc_driverClassName}</ value > |
24 | </ property > |
25 | < property name = "url" > |
26 | < value >${jdbc_url}</ value > |
27 | </ property > |
28 | < property name = "username" > |
29 | < value >${jdbc_username}</ value > |
30 | </ property > |
31 | < property name = "password" > |
32 | < value >${jdbc_password}</ value > |
33 | </ property > |
34 | <!--连接池最大使用连接数--> |
35 | < property name = "maxActive" > |
36 | < value >20</ value > |
37 | </ property > |
38 | <!--初始化连接大小--> |
39 | < property name = "initialSize" > |
40 | < value >1</ value > |
41 | </ property > |
42 | <!--获取连接最大等待时间--> |
43 | < property name = "maxWait" > |
44 | < value >60000</ value > |
45 | </ property > |
46 | <!--连接池最大空闲--> |
47 | < property name = "maxIdle" > |
48 | < value >20</ value > |
49 | </ property > |
50 | <!--连接池最小空闲--> |
51 | < property name = "minIdle" > |
52 | < value >3</ value > |
53 | </ property > |
54 | <!--自动清除无用连接--> |
55 | < property name = "removeAbandoned" > |
56 | < value >true</ value > |
57 | </ property > |
58 | <!--清除无用连接的等待时间--> |
59 | < property name = "removeAbandonedTimeout" > |
60 | < value >180</ value > |
61 | </ property > |
62 | <!--连接属性--> |
63 | < property name = "connectionProperties" > |
64 | < value >clientEncoding=UTF-8</ value > |
65 | </ property > |
66 | </ bean > |
67 |
68 | <!--mybatis文件配置,扫描所有mapper文件--> |
69 | < bean id = "sqlSessionFactory" |
70 | class = "org.mybatis.spring.SqlSessionFactoryBean" |
71 | p:dataSource-ref = "dataSource" |
72 | p:configLocation = "classpath:conf/mybatis-config.xml" |
73 | p:mapperLocations = "classpath:mapper/*.xml" /> <!--configLocation为mybatis属性 |
74 |
75 | <!--spring与mybatis整合配置,扫描所有dao--> |
76 | < bean class = "org.mybatis.spring.mapper.MapperScannerConfigurer" |
77 | p:basePackage = "cn.springmvc.dao" |
78 | p:sqlSessionFactoryBeanName = "sqlSessionFactory" /> |
79 |
80 | <!--对数据源进行事务管理--> |
81 | < bean id = "transactionManager" |
82 | class = "org.springframework.jdbc.datasource.DataSourceTransactionManager" |
83 | p:dataSource-ref = "dataSource" /> |
84 | </ beans > |
5.java代码编写(model,dao,service层代码)
cn.springmvc.model/User.java(用户基本信息)01 | package cn.springmvc.model; |
02 |
03 |
04 | /** |
05 | *用户表 |
06 | */ |
07 | public class User{ |
08 |
09 | private int id; |
10 | private int state; |
11 | private Stringnickname; |
12 | public int getId(){ |
13 | return id; |
14 | } |
15 | public void setId( int id){ |
16 | this .id=id; |
17 | } |
18 | public int getState(){ |
19 | return state; |
20 | } |
21 | public void setState( int state){ |
22 | this .state=state; |
23 | } |
24 | public StringgetNickname(){ |
25 | return nickname; |
26 | } |
27 | public void setNickname(Stringnickname){ |
28 | this .nickname=nickname; |
29 | } |
30 | } |
01 | package cn.springmvc.dao; |
02 |
03 | import cn.springmvc.model.User; |
04 |
05 |
06 | public interface UserDAO{ |
07 |
08 | /** |
09 | *添加新用户 |
10 | *@paramuser |
11 | *@return |
12 | */ |
13 | public int insertUser(Useruser); |
14 |
15 |
16 | } |
1 | package cn.springmvc.service; |
2 |
3 | import cn.springmvc.model.User; |
4 |
5 |
6 | public interface UserService{ |
7 |
8 | public int insertUser(Useruser); |
9 | } |
01 | package cn.springmvc.service.impl; |
02 |
03 | import org.springframework.beans.factory.annotation.Autowired; |
04 | import org.springframework.stereotype.Service; |
05 |
06 | import cn.springmvc.dao.UserDAO; |
07 | import cn.springmvc.model.User; |
08 | import cn.springmvc.service.UserService; |
09 |
10 |
11 | @Service |
12 | public class UserServiceImpl implements UserService{ |
13 |
14 | @Autowired |
15 | private UserDAOuserDAO; |
16 |
17 | @Override |
18 | public int insertUser(Useruser){ |
19 | //TODOAuto-generatedmethodstub |
20 | return userDAO.insertUser(user); |
21 | } |
22 |
23 | } |
6.mybatis配置
conf/mybatis-config.xml(mybatis配置的基本文件)01 | <? xml version = "1.0" encoding = "UTF-8" ?> |
02 | <!DOCTYPEconfiguration |
03 | PUBLIC"-//mybatis.org//DTDConfig3.0//EN" |
04 | "http://mybatis.org/dtd/mybatis-3-config.dtd"> |
05 | < configuration > |
06 | <!--命名空间--> |
07 | < typeAliases > |
08 | < typeAlias alias = "User" type = "cn.springmvc.model.User" /> |
09 | </ typeAliases > |
10 |
11 | <!--映射map--> |
12 | < mappers > |
13 | </ mappers > |
14 | </ configuration > |
01 | <? xml version = "1.0" encoding = "UTF-8" ?> |
02 | <!DOCTYPEmapper |
03 | PUBLIC"-//mybatis.org//DTDMapper3.0//EN" |
04 | "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> |
05 | < mapper namespace = "cn.springmvc.dao.UserDAO" > |
06 |
07 | < insert id = "insertUser" parameterType = "User" keyProperty = "id" > |
08 | insertintodays_user( |
09 | state, |
10 | nickname) |
11 | values |
12 | ( |
13 | #{state}, |
14 | #{nickname}) |
15 | </ insert > |
16 |
17 | </ mapper > |
7.junit测试插入功能
cn.springmvc.test/UserTest.java(用户测试模块)01 | package cn.springmvc.test; |
02 |
03 | import org.junit.Before; |
04 | import org.junit.Test; |
05 | import org.springframework.context.ApplicationContext; |
06 | import org.springframework.context.support.ClassPathXmlApplicationContext; |
07 |
08 | import cn.springmvc.model.User; |
09 | import cn.springmvc.service.UserService; |
10 |
11 |
12 |
13 | public class UserTest{ |
14 |
15 | private UserServiceuserService; |
16 |
17 | @Before |
18 | public void before(){ |
19 | @SuppressWarnings ( "resource" ) |
20 | ApplicationContextcontext= new ClassPathXmlApplicationContext( new String[]{ "classpath:conf/spring.xml" |
21 | , "classpath:conf/spring-mybatis.xml" }); |
22 | userService=(UserService)context.getBean( "userServiceImpl" ); |
23 | } |
24 |
25 | @Test |
26 | public void addUser(){ |
27 | Useruser= new User(); |
28 | user.setNickname( "你好" ); |
29 | user.setState( 2 ); |
30 | System.out.println(userService.insertUser(user)); |
31 | } |
32 | } |
即完成插入功能!
8.springMVC模块搭建
web.xml(web功能配置)01 | <? xml version = "1.0" encoding = "UTF-8" ?> |
02 | < web-app xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" |
03 | xmlns = "http://java.sun.com/xml/ns/javaee" xmlns:web = "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" |
04 | xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee | 'target='_blank'>http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"[/code]
05 | id = "WebApp_ID" version = "2.5" > |
06 | < display-name >ArchetypeCreatedWebApplication</ display-name > |
07 |
08 | <!--读取spring配置文件--> |
09 | < context-param > |
10 | < param-name >contextConfigLocation</ param-name > |
11 | < param-value >classpath:conf/spring.xml; |
12 | classpath:conf/spring-mybatis.xml |
13 | </ param-value > |
14 | </ context-param > |
15 | <!--设计路径变量值--> |
16 | < context-param > |
17 | < param-name >webAppRootKey</ param-name > |
18 | < param-value >springmvc.root</ param-value > |
19 | </ context-param > |
20 |
21 |
22 | <!--Spring字符集过滤器--> |
23 | < filter > |
24 | < filter-name >SpringEncodingFilter</ filter-name > |
25 | < filter-class >org.springframework.web.filter.CharacterEncodingFilter</ filter-class > |
26 | < init-param > |
27 | < param-name >encoding</ param-name > |
28 | < param-value >UTF-8</ param-value > |
29 | </ init-param > |
30 | < init-param > |
31 | < param-name >forceEncoding</ param-name > |
32 | < param-value >true</ param-value > |
33 | </ init-param > |
34 | </ filter > |
35 | < filter-mapping > |
36 | < filter-name >SpringEncodingFilter</ filter-name > |
37 | < url-pattern >/*</ url-pattern > |
38 | </ filter-mapping > |
39 |
40 | <!--日志记录--> |
41 | < context-param > |
42 | <!--日志配置文件路径--> |
43 | < param-name >log4jConfigLocation</ param-name > |
44 | < param-value >classpath:conf/log4j.properties</ param-value > |
45 | </ context-param > |
46 | < context-param > |
47 | <!--日志页面的刷新间隔--> |
48 | < param-name >log4jRefreshInterval</ param-name > |
49 | < param-value >6000</ param-value > |
50 | </ context-param > |
51 | < listener > |
52 | < listener-class >org.springframework.web.util.Log4jConfigListener</ listener-class > |
53 | </ listener > |
54 |
55 | < listener > |
56 | < listener-class >org.springframework.web.context.ContextLoaderListener</ listener-class > |
57 | </ listener > |
58 |
59 | <!--springMVC核心配置--> |
60 | < servlet > |
61 | < servlet-name >spring</ servlet-name > |
62 | < servlet-class >org.springframework.web.servlet.DispatcherServlet</ servlet-class > |
63 | < init-param > |
64 | < param-name >contextConfigLocation</ param-name > |
65 | < param-value >classpath:conf/spring-mvc.xml</ param-value > |
66 | </ init-param > |
67 | < load-on-startup >2</ load-on-startup > |
68 | </ servlet > |
69 | < servlet-mapping > |
70 | < servlet-name >spring</ servlet-name > |
71 | < url-pattern >*.do</ url-pattern > |
72 | </ servlet-mapping > |
73 |
74 | < welcome-file-list > |
75 | < welcome-file >index.jsp</ welcome-file > |
76 | </ welcome-file-list > |
77 |
78 | <!--错误跳转页面--> |
79 | < error-page > |
80 | <!--路径不正确--> |
81 | < error-code >404</ error-code > |
82 | < location >/WEB-INF/errorpage/404.jsp</ location > |
83 | </ error-page > |
84 | < error-page > |
85 | <!--没有访问权限,访问被禁止--> |
86 | < error-code >405</ error-code > |
87 | < location >/WEB-INF/errorpage/405.jsp</ location > |
88 | </ error-page > |
89 | < error-page > |
90 | <!--内部错误--> |
91 | < error-code >500</ error-code > |
92 | < location >/WEB-INF/errorpage/500.jsp</ location > |
93 | </ error-page > |
94 | </ web-app > |
01 | <? xml version = "1.0" encoding = "UTF-8" ?> |
02 | < beans xmlns = "http://www.springframework.org/schema/beans" |
03 | xmlns:p = "http://www.springframework.org/schema/p" |
04 | xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" |
05 | xmlns:context = "http://www.springframework.org/schema/context" |
06 | xmlns:mvc = "http://www.springframework.org/schema/mvc" |
07 | xsi:schemaLocation=" |
08 | http://www.springframework.org/schema/beans |
09 | http://www.springframework.org/schema/beans/spring-beans-3.2.xsd |
10 | http://www.springframework.org/schema/context |
11 | http://www.springframework.org/schema/context/spring-context-3.2.xsd |
12 | http://www.springframework.org/schema/mvc |
13 | http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> |
14 |
15 | <!--扫描controller(controller层注入)--> |
16 | < context:component-scan base-package = "cn.springmvc.controller" /> |
17 |
18 | <!--避免IE在ajax请求时,返回json出现下载--> |
19 | < bean id = "jacksonMessageConverter" class = "org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" > |
20 | < property name = "supportedMediaTypes" > |
21 | < list > |
22 | < value >text/html;charset=UTF-8</ value > |
23 | </ list > |
24 | </ property > |
25 | </ bean > |
26 |
27 | <!--对模型视图添加前后缀--> |
28 | < bean id = "viewResolver" class = "org.springframework.web.servlet.view.InternalResourceViewResolver" |
29 | p:prefix = "/WEB-INF/jsp/" p:suffix = ".jsp" /> |
30 | </ beans > |
9.log4j日志记录搭建
conf/log4j.properties(日志记录的配置文件)01 | ###setloglevels### |
02 | #log4j.rootLogger=debug,stdout,D,E |
03 | log4j.rootLogger=debug,stdout,D |
04 |
05 | ###outputtotheconsole### |
06 | log4j.appender.stdout=org.apache.log4j.ConsoleAppender |
07 | log4j.appender.stdout.Target=System.out |
08 | log4j.appender.stdout.layout=org.apache.log4j.PatternLayout |
09 | #log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE}%5p%c{1}:%L-%m%n |
10 | log4j.appender.stdout.layout.ConversionPattern=%-d{yyyy-MM-ddHH:mm:ss}[%c]-[%p]%m%n |
11 |
12 | ###Outputtothelogfile### |
13 | log4j.appender.D=org.apache.log4j.DailyRollingFileAppender |
14 | log4j.appender.D.File=${springmvc.root}/WEB-INF/logs/log.log |
15 | log4j.appender.D.Append=true |
16 | log4j.appender.D.Threshold=DEBUG |
17 | log4j.appender.D.layout=org.apache.log4j.PatternLayout |
18 | log4j.appender.D.layout.ConversionPattern=%-d{yyyy-MM-ddHH:mm:ss}[%t:%r]-[%p]%m%n |
19 |
20 | ###Saveexceptioninformationtoseparatefile### |
21 | log4j.appender.D=org.apache.log4j.DailyRollingFileAppender |
22 | log4j.appender.D.File=${springmvc.root}/WEB-INF/logs/error.log |
23 | log4j.appender.D.Append=true |
24 | log4j.appender.D.Threshold=ERROR |
25 | log4j.appender.D.layout=org.apache.log4j.PatternLayout |
26 | log4j.appender.D.layout.ConversionPattern=%-d{yyyy-MM-ddHH:mm:ss}[%t:%r]-[%p]%m%n |
10.测试运行
WEB-INF/jsp/index.jsp(测试文件)01 | <%@pagelanguage= "java" contentType= "text/html;charset=UTF-8" |
02 | pageEncoding= "UTF-8" %> |
03 | <!DOCTYPEhtmlPUBLIC "-//W3C//DTDHTML4.01Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" > |
04 | <html> |
05 | <head> |
06 | <metahttp-equiv= "Content-Type" content= "text/html;charset=UTF-8" > |
07 | <title>Inserttitlehere</title> |
08 | </head> |
09 | <body> |
10 | <h1>欢迎你!!!</h1> |
11 | </body> |
12 | </html> |
01 | package cn.springmvc.controller; |
02 |
03 | import org.springframework.stereotype.Controller; |
04 | import org.springframework.web.bind.annotation.RequestMapping; |
05 |
06 | @Controller |
07 | @RequestMapping ( "/" ) |
08 | public class UserController{ |
09 |
10 | @RequestMapping ( "index" ) |
11 | public Stringindex(){ |
12 | return "index" ; |
13 | } |
14 |
15 | } |
右击该项目选择Runas,选择RunonServer
选择自己建立的tomcat6运行如图:
控制台打印出日志如图(启动成功):
在浏览器中输入:http://localhost:8080/springmvc/index.do进行测试
效果如图(返回成功):
相关文章推荐
- maven+springMVC+mybatis+junit详细搭建过程
- maven+springMVC+mybatis+junit详细搭建过程
- maven+springMVC+mybatis+junit详细搭建过程 【转】
- maven+springMVC+mybatis+junit详细搭建过程 ***
- maven+springMVC+mybatis+junit详细搭建过程
- maven+springMVC+mybatis+junit详细搭建过程
- maven+springMVC+mybatis+junit详细搭建过程
- maven+springMVC+mybatis+junit详细搭建过程
- maven+springMVC+mybatis+junit详细搭建过程
- maven+springMVC+mybatis+junit详细搭建过程
- maven+springMVC+mybatis+junit详细搭建过程
- maven+springMVC+mybatis+junit详细搭建过程
- maven+springMVC+mybatis+junit详细搭建过程
- maven+springMVC+mybatis+junit详细搭建过程
- maven+springMVC+mybatis+junit详细搭建过程
- maven+springMVC+mybatis+junit详细搭建过程
- maven+springMVC+mybatis+junit详细搭建过程
- maven+springMVC+mybatis+junit详细搭建过程
- maven+springMVC+mybatis+junit详细搭建过程
- maven+springMVC+mybatis+junit详细搭建过程