IntelliJ IDEA14.0.3+Maven+SpringMVC+Spring+Hibernate光速构建Java权限管理系统(二)

释放双眼,带上耳机,听听看~!

配置文件篇

–pom.xml、spring的applicationContext.xml、springMVC的dispatcher-servlet.xml以及web.xml文件的基本配置

从这篇博客起不再介绍基本概念,毕竟网上关于各种概念比如pom,spring之类的讲解非常详细。

一、pom.xml


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
1    <properties>
2        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
3        <spring.version>4.1.4.RELEASE</spring.version>
4        <hibernate.version>4.3.8.Final</hibernate.version>
5        <jackson.version>2.5.0</jackson.version>
6    </properties>
7
8    <dependencies>
9
10        <!-- junit -->
11        <dependency>
12            <groupId>junit</groupId>
13            <artifactId>junit</artifactId>
14            <version>4.12</version>
15            <scope>test</scope>
16        </dependency>
17
18        <!-- spring -->
19        <dependency>
20            <groupId>org.springframework</groupId>
21            <artifactId>spring-core</artifactId>
22            <version>${spring.version}</version>
23        </dependency>
24
25        <dependency>
26            <groupId>org.springframework</groupId>
27            <artifactId>spring-beans</artifactId>
28            <version>${spring.version}</version>
29        </dependency>
30
31        <dependency>
32            <groupId>org.springframework</groupId>
33            <artifactId>spring-context</artifactId>
34            <version>${spring.version}</version>
35        </dependency>
36
37        <!--<dependency>
38            <groupId>org.springframework</groupId>
39            <artifactId>spring-tx</artifactId>
40            <version>${spring.version}</version>
41        </dependency>-->
42
43        <dependency>
44            <groupId>org.springframework</groupId>
45            <artifactId>spring-web</artifactId>
46            <version>${spring.version}</version>
47        </dependency>
48
49        <!-- 使用SpringMVC需配置 -->
50        <dependency>
51            <groupId>org.springframework</groupId>
52            <artifactId>spring-webmvc</artifactId>
53            <version>${spring.version}</version>
54        </dependency>
55
56        <!-- 关系型数据库整合时需配置 如hibernate jpa等 -->
57        <dependency>
58            <groupId>org.springframework</groupId>
59            <artifactId>spring-orm</artifactId>
60            <version>${spring.version}</version>
61        </dependency>
62
63        <!-- hibernate -->
64        <dependency>
65            <groupId>org.hibernate</groupId>
66            <artifactId>hibernate-core</artifactId>
67            <version>${hibernate.version}</version>
68        </dependency>
69
70        <dependency>
71            <groupId>org.hibernate</groupId>
72            <artifactId>hibernate-ehcache</artifactId>
73            <version>${hibernate.version}</version>
74        </dependency>
75
76        <dependency>
77            <groupId>org.hibernate.javax.persistence</groupId>
78            <artifactId>hibernate-jpa-2.1-api</artifactId>
79            <version>1.0.0.Final</version>
80        </dependency>
81
82        <dependency>
83            <groupId>javassist</groupId>
84            <artifactId>javassist</artifactId>
85            <version>3.12.0.GA</version>
86        </dependency>
87
88        <dependency>
89            <groupId>antlr</groupId>
90            <artifactId>antlr</artifactId>
91            <version>2.7.6</version>
92        </dependency>
93
94        <dependency>
95            <groupId>dom4j</groupId>
96            <artifactId>dom4j</artifactId>
97            <version>1.6.1</version>
98        </dependency>
99
100        <dependency>
101            <groupId>org.slf4j</groupId>
102            <artifactId>slf4j-api</artifactId>
103            <version>1.6.1</version>
104        </dependency>
105
106        <!-- 二级缓存ehcache -->
107        <dependency>
108            <groupId>net.sf.ehcache</groupId>
109            <artifactId>ehcache</artifactId>
110            <version>2.9.0</version>
111        </dependency>
112
113        <!-- log4j -->
114        <dependency>
115            <groupId>log4j</groupId>
116            <artifactId>log4j</artifactId>
117            <version>1.2.17</version>
118        </dependency>
119
120        <!-- mysql连接 -->
121        <dependency>
122            <groupId>mysql</groupId>
123            <artifactId>mysql-connector-java</artifactId>
124            <version>5.1.34</version>
125        </dependency>
126
127        <!-- tomcat-jdbc-->
128        <dependency>
129            <groupId>org.apache.tomcat</groupId>
130            <artifactId>tomcat-jdbc</artifactId>
131            <version>8.0.33</version>
132        </dependency>
133
134        <!-- json -->
135        <dependency>
136            <groupId>com.alibaba</groupId>
137            <artifactId>fastjson</artifactId>
138            <version>1.2.3</version>
139        </dependency>
140
141        <dependency>
142            <groupId>com.fasterxml.jackson.core</groupId>
143            <artifactId>jackson-annotations</artifactId>
144            <version>${jackson.version}</version>
145        </dependency>
146
147        <dependency>
148            <groupId>com.fasterxml.jackson.core</groupId>
149            <artifactId>jackson-core</artifactId>
150            <version>${jackson.version}</version>
151        </dependency>
152
153        <dependency>
154            <groupId>com.fasterxml.jackson.core</groupId>
155            <artifactId>jackson-databind</artifactId>
156            <version>${jackson.version}</version>
157        </dependency>
158
159        <!-- aop -->
160        <dependency>
161            <groupId>org.aspectj</groupId>
162            <artifactId>aspectjweaver</artifactId>
163            <version>1.8.4</version>
164        </dependency>
165
166        <!-- servlet -->
167        <dependency>
168            <groupId>javax.servlet</groupId>
169            <artifactId>servlet-api</artifactId>
170            <version>3.0-alpha-1</version>
171            <scope>provided</scope>
172        </dependency>
173
174        <dependency>
175            <groupId>javax.servlet</groupId>
176            <artifactId>jstl</artifactId>
177            <version>1.2</version>
178        </dependency>
179
180        <!-- 文件上传 -->
181
182        <dependency>
183            <groupId>commons-io</groupId>
184            <artifactId>commons-io</artifactId>
185            <version>2.2</version>
186        </dependency>
187
188        <dependency>
189            <groupId>commons-fileupload</groupId>
190            <artifactId>commons-fileupload</artifactId>
191            <version>1.3.1</version>
192        </dependency>
193
194    </dependencies>
195

请大家把以上代码复制到pom.xml文件的<project></project>标签内,如果设置了maven自动导包,项目所依赖的jar包就会由开发工具帮我们下载。如果没有设置,我们也可以点击IDEA右侧maven选项卡,然后点击刷新按钮(reimport all maven projects)激活下载。另外,并不是说以上dependency都是必需的,大家可以根据自己需要进行取舍。

二、applicationContext.xml

先给出log4j.properties和jdbc.properties的内容

log4j.properties


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
1log4j.rootLogger=WARN, stdout, R
2log4j.appender.stdout=org.apache.log4j.ConsoleAppender
3log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
4# Pattern to output the caller&#x27;s file name and line number.
5#log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
6# Print the date in ISO 8601 format
7log4j.appender.stdout.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
8log4j.appender.R=org.apache.log4j.RollingFileAppender
9log4j.appender.R.File=example.log
10log4j.appender.R.MaxFileSize=100KB
11# Keep one backup file
12log4j.appender.R.MaxBackupIndex=1
13log4j.appender.R.layout=org.apache.log4j.PatternLayout
14log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
15# Print only messages of level WARN or above in the package com.foo.
16log4j.logger.com.foo=WARN
17

jdbc.properties


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
1#zouy Tomcat JDBC
2
3jdbc.driverClass = com.mysql.jdbc.Driver
4jdbc.url = jdbc:mysql://127.0.0.1:3306/work
5jdbc.username = root
6jdbc.password = 772052352
7
8#Tomcat JDBC
9
10#hibernate
11hibernate.show_sql = true
12hibernate.format_sql = true
13hibernate.dialect = org.hibernate.dialect.MySQLDialect
14
15#hibernate cache
16
17

上面代码中jdbc.url填写自己本机的数据库地址或远程数据库地址,而jdbc.username和jdbc.password则填写相应的用于登录数据库的用户名和密码。

至于hibernate cache大家根据需要选择性配置。

applicationContext.xml


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
1&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
2&lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;
3       xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
4       xmlns:context=&quot;http://www.springframework.org/schema/context&quot;
5       xmlns:tx=&quot;http://www.springframework.org/schema/tx&quot;
6       xmlns:aop=&quot;http://www.springframework.org/schema/aop&quot;
7       xsi:schemaLocation=&quot;http://www.springframework.org/schema/beans
8        http://www.springframework.org/schema/beans/spring-beans.xsd
9        http://www.springframework.org/schema/tx
10        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
11        http://www.springframework.org/schema/context
12        http://www.springframework.org/schema/context/spring-context.xsd
13        http://www.springframework.org/schema/aop
14        http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd&quot;&gt;
15        
16        &lt;context:annotation-config/&gt;
17        &lt;!--扫描注解包--&gt;
18        &lt;context:component-scan base-package=&quot;com.whut.work&quot; /&gt;
19        
20         &lt;!--配置文件加载 init.properties--&gt;
21         &lt;bean id=&quot;placeholderConfig&quot; class=&quot;org.springframework.beans.factory.config.PropertyPlaceholderConfigurer&quot;&gt;
22          &lt;property name=&quot;locations&quot; value=&quot;classpath:jdbc.properties&quot; /&gt;
23         &lt;/bean&gt;
24         &lt;!-- 使用Tomcat JDBC连接(池) --&gt;
25         &lt;bean id=&quot;dataSource&quot; class=&quot;org.apache.tomcat.jdbc.pool.DataSource&quot; destroy-method=&quot;close&quot;&gt;
26          &lt;property name=&quot;driverClassName&quot; value=&quot;${jdbc.driverClass}&quot;&gt;&lt;/property&gt;
27          &lt;property name=&quot;url&quot; value=&quot;${jdbc.url}&quot;&gt;&lt;/property&gt;
28          &lt;property name=&quot;username&quot; value=&quot;${jdbc.username}&quot;&gt;&lt;/property&gt;
29          &lt;property name=&quot;password&quot; value=&quot;${jdbc.password}&quot;&gt;&lt;/property&gt;
30         &lt;/bean&gt;
31        
32         &lt;!-- 配置sessionFactory--&gt;
33         &lt;bean id=&quot;sessionFactory&quot; class=&quot;org.springframework.orm.hibernate4.LocalSessionFactoryBean&quot;&gt;
34          &lt;property name=&quot;packagesToScan&quot;&gt;
35              &lt;list&gt;
36                  &lt;value&gt;com.whut.work.*.model&lt;/value&gt;
37              &lt;/list&gt;
38          &lt;/property&gt;
39          &lt;property name=&quot;dataSource&quot; ref=&quot;dataSource&quot;&gt;&lt;/property&gt;
40          &lt;property name=&quot;hibernateProperties&quot;&gt;
41              &lt;props&gt;
42                  &lt;prop key=&quot;hibernate.dialect&quot;&gt;${hibernate.dialect}&lt;/prop&gt;
43                  &lt;prop key=&quot;hibernate.show_sql&quot;&gt;${hibernate.show_sql}&lt;/prop&gt;
44                  &lt;prop key=&quot;hibernate.format_sql&quot;&gt;${hibernate.format_sql}&lt;/prop&gt;
45                  &lt;prop key=&quot;hibernate.current_session_context_class&quot;&gt;thread&lt;/prop&gt;&lt;!-- 使用getCurrentSession()方法,必须配置此属性 --&gt;
46              &lt;/props&gt;
47          &lt;/property&gt;
48         &lt;/bean&gt;
49&lt;/beans&gt;
50

这里我们用spring来管理hibernate的DataSource和sessionFactory,而hibernate的配置我们使用注解(Annotation)在源代码中进行。

关于这条配置:<context:component-scan base-package="com.whut.work" />,我们在以后项目中建立的基本包结构就是com.whut.work.*。

三、dispatcher-servlet.xml


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
1&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
2&lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;
3       xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
4       xmlns:mvc=&quot;http://www.springframework.org/schema/mvc&quot;
5       xmlns:context=&quot;http://www.springframework.org/schema/context&quot;
6       xmlns:aop=&quot;http://www.springframework.org/schema/aop&quot;
7       xsi:schemaLocation=&quot;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       http://www.springframework.org/schema/aop
14       http://www.springframework.org/schema/aop/spring-aop.xsd&quot;&gt;
15      
16        &lt;!--扫描注解包 配置这条便可移除 &lt;context:annotation-config/&gt; --&gt;
17        &lt;context:component-scan base-package=&quot;com.whut.work&quot; /&gt;
18
19        &lt;!--MVC驱动 controller--&gt;
20        &lt;mvc:annotation-driven /&gt;
21
22        &lt;!--MVC注解--&gt;
23        &lt;!--针对注解配置@RequestMapping :
24        RequestMappingHandlerMapping 替代了 DefaultAnnotationHandlerMapping--&gt;
25        &lt;bean class=&quot;org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping &quot; /&gt;
26      
27        &lt;!--视图解析--&gt;
28        &lt;bean class=&quot;org.springframework.web.servlet.view.InternalResourceViewResolver&quot;&gt;
29            &lt;property name=&quot;prefix&quot; value=&quot;/jsp&quot;/&gt;
30            &lt;property name=&quot;suffix&quot; value=&quot;.jsp&quot;/&gt;
31        &lt;/bean&gt;
32
33        &lt;!--servlet在找页面时,走的是dispatcherServlet路线。找不到的时候会报404
34            加上这个默认的servlet时候,servlet在找不到的时候会去找静态的内容。--&gt;
35        &lt;mvc:default-servlet-handler /&gt;
36
37        &lt;!-- 用于将对象转换为 JSON  转换器--&gt;
38        &lt;!-- 避免IE执行AJAX时,返回JSON出现下载文件 --&gt;
39        &lt;bean id=&quot;stringConverter&quot;
40              class=&quot;org.springframework.http.converter.StringHttpMessageConverter&quot;&gt;
41            &lt;property name=&quot;supportedMediaTypes&quot;&gt;
42                &lt;list&gt;
43                    &lt;value&gt;text/plain;charset=UTF-8&lt;/value&gt;
44                &lt;/list&gt;
45            &lt;/property&gt;
46        &lt;/bean&gt;
47        &lt;bean id=&quot;jsonConverter&quot; class=&quot;org.springframework.http.converter.json.MappingJackson2HttpMessageConverter&quot;&gt;
48            &lt;property name=&quot;supportedMediaTypes&quot;&gt;
49                &lt;list&gt;
50                    &lt;value&gt;text/plain;charset=UTF-8&lt;/value&gt;
51                &lt;/list&gt;
52            &lt;/property&gt;
53        &lt;/bean&gt;
54        &lt;!--RequestMappingHandlerAdapter :
55            和上面的RequestMappingHandlerMapping配对使用--&gt;
56        &lt;bean class=&quot;org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter &quot;&gt;
57            &lt;property name=&quot;messageConverters&quot;&gt;
58                &lt;list&gt;
59                    &lt;ref bean=&quot;stringConverter&quot; /&gt;
60                    &lt;ref bean=&quot;jsonConverter&quot; /&gt;
61                &lt;/list&gt;
62            &lt;/property&gt;
63        &lt;/bean&gt;
64     
65&lt;/beans&gt;
66

在之后的web.xml文件中,DispatcherServlet会默认加载[servlet-name]-servlet.xml文件,所以关于springMVC配置文件的命名也可以命名成sping-servlet.xml。

四、web.xml


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
1&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
2&lt;web-app xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
3        xmlns=&quot;http://java.sun.com/xml/ns/javaee&quot;
4        xsi:schemaLocation=&quot;http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd&quot;
5        id=&quot;WebApp_ID&quot; version=&quot;3.0&quot;&gt;
6   &lt;display-name&gt;minissh&lt;/display-name&gt;
7   &lt;welcome-file-list&gt;
8        &lt;welcome-file&gt;login.html&lt;/welcome-file&gt;
9       &lt;welcome-file&gt;/jsp/index.jsp&lt;/welcome-file&gt;
10  &lt;/welcome-file-list&gt;
11
12    &lt;!-- 把 Spring 容器集成到 Web 应用里面 --&gt;
13  &lt;listener&gt;
14      &lt;listener-class&gt;
15          org.springframework.web.context.ContextLoaderListener
16      &lt;/listener-class&gt;
17  &lt;/listener&gt;
18 
19  &lt;!-- spring配置文件 --&gt;
20  &lt;context-param&gt;
21      &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
22      &lt;param-value&gt;classpath:/applicationContext.xml&lt;/param-value&gt;
23  &lt;/context-param&gt;
24
25    &lt;!--DispatcherServlet是前端控制器设计模式的实现,提供Spring Web MVC的集中访问点,而且负责职责的分派,
26    而且与Spring IoC容器无缝集成,从而可以获得Spring的所有好处。--&gt;
27    &lt;!--DispatcherServlet会默认加载[servlet-name]-servlet.xml文件--&gt;
28  &lt;servlet&gt;
29      &lt;servlet-name&gt;spring&lt;/servlet-name&gt;
30      &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
31      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
32  &lt;/servlet&gt;
33  &lt;servlet-mapping&gt;
34      &lt;servlet-name&gt;spring&lt;/servlet-name&gt;
35      &lt;url-pattern&gt;/&lt;/url-pattern&gt;
36  &lt;/servlet-mapping&gt;
37
38    &lt;!--解决springmvc传递给后台的中文数据乱码问题--&gt;
39    &lt;filter&gt;
40        &lt;filter-name&gt;CharacterEncodingFilter&lt;/filter-name&gt;
41        &lt;filter-class&gt;org.springframework.web.filter.CharacterEncodingFilter&lt;/filter-class&gt;
42        &lt;init-param&gt;
43            &lt;param-name&gt;encoding&lt;/param-name&gt;
44            &lt;param-value&gt;utf-8&lt;/param-value&gt;
45        &lt;/init-param&gt;
46    &lt;/filter&gt;
47    &lt;filter-mapping&gt;
48        &lt;filter-name&gt;CharacterEncodingFilter&lt;/filter-name&gt;
49        &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
50    &lt;/filter-mapping&gt;
51
52&lt;/web-app&gt;
53

web.xml中对applicationContext.xml和dispatcher-servlet.xml进行了集成,并配置了解决中文乱码问题的filter。

五、补充

我们在上一篇博客的基础上继续截图演示。

按上图所示,在module文件夹上右键,然后单击add framework support。弹出如下图所示对话框:

之后我们看到,在项目中会多出一个web文件夹,如下图所示:

接着,我们把本文一开始介绍的几个配置文件放到我们的项目里去。放完之后项目结构如下图所示:

至此,项目的地基已经打好。在下一篇博客中我将以注册登录系统为例来打通前端到后台的数据传输路径。

给TA打赏
共{{data.count}}人
人已打赏
安全技术

C++回调函数

2022-1-11 12:36:11

气候事件

今年第7号台风“海贝思”15日将正面袭击粤东

2014-6-15 11:21:33

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索