web项目(使用html视图解析器)
完整配置springmvc4,最终视图选择的是html,非静态文件。
最近自己配置spring的时候,遇到很多问题,由于开发环境和版本的变化导致网友们给出的建议很多还是不能用的,可能还会有很多人会遇到和我一样的问题,希望能帮到遇到困难的人。
环境:jdk8,spring4.2.0,tomcat8,使用idea14编辑器,maven管理,git版本控制器,项目远程库地址github:https://github.com/627135316/on_line。欢迎大家clone。中间的用户名也是我qq号。
基本的环境搭建就不啰嗦了,这个网上资料一般还是很全的。下面会介绍很多细节的东西,都是我自己搭建环境的时候遇到的头疼的问题,主要是第一次自己搭建,没有经验。不要笑哈。我就直接贴代码出来吧。关于maven的信息我放在了最后面,因为可能有的包你们并不需要
这个是本次项目的文件目录。
接下来进入大名鼎鼎的web.xml。网上的配置大同小异。目录下我优先配置的spring的监听器——ContextLoaderListener,这个类我之前是在该文件的servlet标签下方配置的,但是一直报错,试了很多方法都不行。有人说监听器类的配置是为了能让controller拦截到url地址请求,其实没有的事,我不配置监听的时候controller照样可以进去。web.xml里还配置另外两个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
|
<?
xml
version="1.0" encoding="UTF-8"?>
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">
<!-- 加载Spring容器配置,配置监听器 -->
<
listener
>
<
listener-class
>org.springframework.web.context.ContextLoaderListener</
listener-class
>
</
listener
>
<!-- 设置Spring容器加载配置文件路径 -->
<
context-param
>
<
param-name
>contextConfigLocation</
param-name
>
<
param-value
>/WEB-INF/config/applicationContext.xml</
param-value
>
</
context-param
>
<
servlet
>
<
servlet-name
>spring</
servlet-name
>
<
servlet-class
>org.springframework.web.servlet.DispatcherServlet</
servlet-class
>
<
init-param
>
<
param-name
>contextConfigLocation</
param-name
>
<
param-value
>/WEB-INF/spring-servlet.xml</
param-value
>
</
init-param
>
<
load-on-startup
>1</
load-on-startup
>
</
servlet
>
<
servlet-mapping
>
<
servlet-name
>spring</
servlet-name
>
<
url-pattern
>/</
url-pattern
>
</
servlet-mapping
>
</
web-app
>
|
接下来是applicationContext.xml。这里是对freemarker的基本配置,没有错,我们就是使用freemarker作为html解析器的。网上很多人都是org.springframework.web.servlet.view.InternalResourceViewResolver这个类,它只能解析jsp,并且它的配置非常简单。需要注意的是我优先在applicationContext里配置了FreeMarkerConfigurer 这个类,很多文章都是没有配置它的,这个类不配置也会报错,而且这个类必须优先在FreeMarkerViewResolver 之前置 ,关于FreeMarkerViewResolver 会在下面介绍。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<?
xml
version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
<
bean
id="freemarkerConfig"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<
property
name="templateLoaderPath" value="/WEB-INF/views/" />
<
property
name="freemarkerSettings">
<
props
>
<
prop
key="template_update_delay">0</
prop
>
<
prop
key="default_encoding">UTF-8</
prop
>
<
prop
key="number_format">0.##########</
prop
>
<
prop
key="datetime_format">yyyy-MM-dd HH:mm:ss</
prop
>
<
prop
key="classic_compatible">true</
prop
>
<
prop
key="template_exception_handler">ignore</
prop
>
</
props
>
</
property
>
</
bean
>
</
beans
>
|
接着是spring-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
|
<?
xml
version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:task="http://www.springframework.org/schema/task" xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--<context:annotation-config />-->
<!-- 把标记了@Controller注解的类转换为bean -->
<
context:component-scan
base-package="controller" />
<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
<
bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<!-- 设置freeMarker的配置文件路径 -->
<
bean
id="freemarkerConfiguration"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<!--注释掉的下方代码是指引freemarker的基本信息的配置位置,因为我已经将配置信息移到了applicationContext文件下,所以这里就没必要存在了,不注释也不会有问题的 -->
<!--<property name="location" value="classpath:/WEB-INF/config/freemarker.properties" />-->
</
bean
>
<
bean
id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<
property
name="exposeRequestAttributes" value="true" />
<
property
name="exposeSessionAttributes" value="true" />
<
property
name="viewClass">
<
value
>org.springframework.web.servlet.view.freemarker.FreeMarkerView</
value
>
</
property
>
<
property
name="cache"><
value
>true</
value
></
property
>
<!--这里需要注意一下,我注释了下面这样一行代码,这行代码的意思就是指引freemarker需要解析的文件的位置。注释掉原因是因为
applicationContext.xml里有这样一行代码:<property name="templateLoaderPath" value="/WEB-INF/views/" /> 已经指定了视图位置。如果我们这里依然保留下方代码,页面回报406的找不到的错误 -->
<!--<property name="prefix"><value>/WEB-INF/views/</value></property>-->
<
property
name="suffix"><
value
>.html</
value
></
property
>
<
property
name="contentType">
<
value
>text/html; charset=UTF-8</
value
>
</
property
>
</
bean
>
<!--这里为可选项-->
<
bean
id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
p:defaultEncoding="utf-8" />
</
beans
>
|
下面是我的maven的配置信息,大家可根据需要选择。不过基本就保留这些吧。
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
|
<
dependencies
>
<!--spring4 begin-->
<
dependency
>
<
groupId
>org.springframework</
groupId
>
<
artifactId
>spring-context</
artifactId
>
<
version
>4.2.0.BUILD-SNAPSHOT</
version
>
</
dependency
>
<
dependency
>
<
groupId
>org.springframework</
groupId
>
<
artifactId
>spring-webmvc</
artifactId
>
<
version
>4.2.0.BUILD-SNAPSHOT</
version
>
<!--<version>${org.springframework-version}</version>-->
</
dependency
>
<
dependency
>
<
groupId
>org.springframework</
groupId
>
<
artifactId
>spring-web</
artifactId
>
<
version
>4.2.0.BUILD-SNAPSHOT</
version
>
</
dependency
>
<
dependency
>
<
groupId
>org.springframework</
groupId
>
<
artifactId
>spring-test</
artifactId
>
<
version
>4.2.0.BUILD-SNAPSHOT</
version
>
</
dependency
>
<
dependency
>
<
groupId
>org.springframework</
groupId
>
<
artifactId
>spring-aop</
artifactId
>
<
version
>4.2.0.BUILD-SNAPSHOT</
version
>
</
dependency
>
<
dependency
>
<
groupId
>org.springframework</
groupId
>
<
artifactId
>spring-core</
artifactId
>
<
version
>4.2.0.BUILD-SNAPSHOT</
version
>
</
dependency
>
<
dependency
>
<
groupId
>org.springframework</
groupId
>
<
artifactId
>spring-oxm</
artifactId
>
<
version
>4.2.0.BUILD-SNAPSHOT</
version
>
</
dependency
>
<
dependency
>
<
groupId
>org.springframework</
groupId
>
<
artifactId
>spring-tx</
artifactId
>
<
version
>4.2.0.BUILD-SNAPSHOT</
version
>
</
dependency
>
<
dependency
>
<
groupId
>org.springframework</
groupId
>
<
artifactId
>spring-jdbc</
artifactId
>
<
version
>4.2.0.BUILD-SNAPSHOT</
version
>
</
dependency
>
<!--spring end-->
<
dependency
>
<
groupId
>junit</
groupId
>
<
artifactId
>junit</
artifactId
>
<
version
>3.8.1</
version
>
<
scope
>test</
scope
>
</
dependency
>
<!-- log4j -->
<
dependency
>
<
groupId
>log4j</
groupId
>
<
artifactId
>log4j</
artifactId
>
<
version
>1.2.17</
version
>
</
dependency
>
<!-- servlet api -->
<
dependency
>
<
groupId
>javax.servlet</
groupId
>
<
artifactId
>javax.servlet-api</
artifactId
>
<
version
>3.0.1</
version
>
<
scope
>provided</
scope
>
</
dependency
>
<!-- start apache -->
<
dependency
>
<
groupId
>commons-logging</
groupId
>
<
artifactId
>commons-logging</
artifactId
>
<
version
>1.1.3</
version
>
</
dependency
>
<
dependency
>
<
groupId
>commons-collections</
groupId
>
<
artifactId
>commons-collections</
artifactId
>
<
version
>3.2.1</
version
>
</
dependency
>
<
dependency
>
<
groupId
>commons-fileupload</
groupId
>
<
artifactId
>commons-fileupload</
artifactId
>
<
version
>1.3</
version
>
</
dependency
>
<!-- end apache -->
<
dependency
>
<
groupId
>org.freemarker</
groupId
>
<
artifactId
>freemarker</
artifactId
>
<
version
>2.3.22</
version
>
</
dependency
>
<
dependency
>
<
groupId
>org.springframework</
groupId
>
<
artifactId
>spring-context-support</
artifactId
>
<
version
>4.1.6.RELEASE</
version
>
</
dependency
>
|
大家需要注意这一段maven信息,之前我也是没有配置,结果greemarker一直报org.springframework.ui.freemarker.FreeMarkerConfigurationFactory这个类找不到,原因就是因为FreeMarkerConfigurationFactory这个类实际是在下面的包里的。感觉中了彩蛋。
1
2
3
4
5
|
<
dependency
>
<
groupId
>org.springframework</
groupId
>
<
artifactId
>spring-context-support</
artifactId
>
<
version
>4.1.6.RELEASE</
version
>
</
dependency
>
|
controller:只做了一步跳转的工作。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
package
controller;
import
org.springframework.web.bind.annotation.RequestMapping;
import
org.springframework.web.bind.annotation.ResponseBody;
import
org.springframework.web.bind.annotation.RestController;
import
org.springframework.web.servlet.ModelAndView;
/**
* Created by Porco on 2015/5/21 0021.
*/
@RestController
public
class
LoginController {
@RequestMapping
(
"/login"
)
public
ModelAndView login(){
return
new
ModelAndView(
"login"
);
}
}
|
html:哈哈,porco就是我的花名,其实这不算是英文,算是意大利文吧,屌丝装逼
1
2
3
4
5
6
7
8
9
10
|
<!
DOCTYPE
html>
<
html
>
<
head
lang="en">
<
meta
charset="UTF-8">
<
title
>welcome Porco</
title
>
</
head
>
<
body
>
<
h1
>welcome Porco zhang</
h1
>
</
body
>
</
html
>
|
最终启动项目,运行结果:
此次spring以及freemarker的配置虽然波折多多,但是也从中学到了很多配置信息的意义,我一位大学同学说过,在学习投入的精力财力都是值得的。