maven + springmvc + freemarker ( 一)

第一步:

1.新建一个maven项目

maven + springmvc + freemarker ( 一)

2.确定之后默认下一步,跳转到如下图片所示的框中 选择maven-archetype-webapp骨架

maven + springmvc + freemarker ( 一)

3.填写group Id和artifact Id ,之后点击finish完成创建项目

maven + springmvc + freemarker ( 一)

 第二步:

1.在原有的pom.xml中添加一个spring-webmvc的jar

maven + springmvc + freemarker ( 一)

2.配置web.xml

<servlet>
        <servlet-name>demo</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
        <servlet-name>demo</servlet-name>
        <url-pattern>*.html</url-pattern>
</servlet-mapping>
 

3.编写 <demo>-servlet.xml (默认路径为WEB-INF目录下) 小括号中的最好跟DispathcerServlet的servlet-name一样,否则需要另行配置载入路径。配置文件内容参考官方文档。

url:http://docs.spring.io/spring/docs/3.1.4.RELEASE/spring-framework-reference/html/mvc.html

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com"/>

    <!-- ... -->

</beans>

 4.在java/main/resource目录下新增freemarker.properties配置文件

#\u8BBE\u7F6E\u6807\u7B7E\u7C7B\u578B\uFF1Asquare_bracket:[]     auto_detect:[]<>
tag_syntax=auto_detect
#\u6A21\u7248\u7F13\u5B58\u65F6\u95F4\uFF0C\u5355\u4F4D\uFF1A\u79D2
template_update_delay=0
default_encoding=UTF-8
output_encoding=UTF-8
locale=zh_CN
#\u8BBE\u7F6E\u6570\u5B57\u683C\u5F0F \uFF0C\u9632\u6B62\u51FA\u73B0 000.00
number_format=#
#\u53D8\u91CF\u4E3A\u7A7A\u65F6\uFF0C\u4E0D\u4F1A\u62A5\u9519
classic_compatible=true
#auto_import="/WEB-INF/templates/index.ftl" as do
 5.在<demo>-servlet.xml中增加freemarker视图配置
<!-- 设置freeMarker的配置文件路径 -->
	<bean id="freemarkerConfiguration" class="org.springframework.beans.factory.config
.PropertiesFactoryBean">
	    <property name="location" value="classpath:freemarker.properties"/>
	</bean>
	
	<!-- 配置freeMarker的模板路径 -->
	<bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
		<property name="freemarkerSettings" ref="freemarkerConfiguration"/>
	    <property name="templateLoaderPath">
	        <value>/WEB-INF/ftl/</value>
	    </property>
	</bean>
	
	<!-- 配置freeMarker视图解析器 -->
	<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.Free
MarkerViewResolver">
	    <property name="viewClass" value="org.springframework.web.servlet.view.freemark
er.FreeMarkerView"/>
	    <property name="contentType" value="text/html; charset=utf-8"/>
	    <property name="cache" value="true"/>
	</bean>
 6.加入freemarker的jar引用到pom.xml中

相关推荐