Maven+SpringMVC+Freemarker教程 Freemarker插值详解 FreeMarker生成静态页面中文乱码解决办法 配置Maven 1. 下载apache-maven-3.3.3(下载地址http://maven.apache.org/download.cgi)和mvnRespo放在D盘根目录下。 2. 修改setting.xml中的localRepository 修改D:\apache-maven-3.3.3\conf\setting.xml中的localRepository,将其值指定为mvnRespo的路径D:/mvnResp。 <localRepository>D:/mvnRespo</localRepository> 3. 添加MAVAN相关的环境变量 添加MAVEN_HOME 在PATH中添加%MAVEN_HOME%bin 重启系统后,在cmd窗口查看maven的版本号 看到上面信息,表示环境变量已经配置成功。 MyEclipse中添加maven 打开Window->Preferences->MyEclipse->Maven4MyEclipse->Installations进行设置 打开MyEclipse->Window->Preferences->MyEclipse->Maven4MyEclipse->User Setting进行设置 这样就设置成功了。 MyEclipse建立Maven项目 1. File-->New-->Project-->Maven Project,Next 2. 选择location位置设置存储目录,Next 3. 输入webapp进行过滤,选择maven-archetype-webapp,Next 4. 填入Group Id和Artifact Id,package会自动生成。 5. 在pom.xml中javaee-api <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>7.0</version> <scope>provided</scope> </dependency> 6. 将MavenDemo项目添加到Tomcat 7中并运行 7. 在浏览器中输入http://localhost:8080/MavenDemo 项目创建成功,文件结构图如下: 配置SpringMVC+Freemarker 1. pom.xml中添加springmvc和freemarker的依赖包 <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>mavenspringmvcfreemarker</groupId> <artifactId>mavenspringmvcfreemarker</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>mavenspringmvcfreemarker</name> <description /> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.20</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>3.2.9.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>3.2.9.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>3.2.9.RELEASE</version> </dependency> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>7.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.glassfish.web</groupId> <artifactId>javax.servlet.jsp.jstl</artifactId> <version>1.2.2</version> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>2.2</version> <configuration> <version>3.1</version> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> </plugins> </build> </project> 2. 配置web.xml <?xml version="1.0"encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://xmlns.jcp.org/xml/ns/javaee"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaeehttp://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>mavenspringmvcfreemarker</display-name> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app> 3. 配置spring-servlet.xml <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="com.demo" /> <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" /> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> <!-- 配置Freemarker属性文件路径 --> <bean id="freemarkerConfiguration" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="location" value="classpath:conf/freemarker.properties" /> </bean> <!-- 配置freeMarker模板加载地址 --> <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> <!-- 视图解析器在/WEB-INF/ftl/路径下扫描视图文件 --> <property name="templateLoaderPath" value="/WEB-INF/ftl/" /> <property name="freemarkerVariables"> <map> <entry key="xml_escape" value-ref="fmXmlEscape" /> </map> </property> </bean> <bean id="fmXmlEscape" class="freemarker.template.utility.XmlEscape" /> <!-- 配置freeMarker视图解析器 --> <bean id="freemakerViewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView" /> <!-- 扫描路径內所有以ftl結尾的文件 --> <property name="viewNames"> <array> <value>*.ftl</value> </array> </property> <property name="contentType" value="text/html; charset=UTF-8" /> <property name="exposeRequestAttributes" value="true" /> <property name="exposeSessionAttributes" value="true" /> <property name="exposeSpringMacroHelpers" value="true" /> <property name="requestContextAttribute" value="request" /> <!-- 给视图解析器配置优先級,你可以给之前jsp视图解析器的值配为2 --> <property name="order" value="1" /> </bean> </beans> web.xml中,有个为spring,所以这个文件起名为spring-servlet.xml。这样,程序自动会去加载spring-servlet.xml。也就说spring-servlet.xml中的必须与的值完全对应。 如果不对应需要在web.xml中显示指定加载。比如:把spring-servlet.xml改名为applicationContext.xml,则要在web.xml中的下方加代码: <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </init-param> 4. 设置freemarker.properties tag_syntax=auto_detect template_update_delay=2 default_encoding=UTF-8 output_encoding=UTF-8 locale=zh_CN date_format=yyyy-MM-dd time_format=HH:mm:ss datetime_format=yyyy-MM-dd HH\:mm\:ss 5. 编写控制器StudentController.java代码 import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.ui.Model; @Controller public class StudentController { @RequestMapping("/helloWorld") public String helloWorld(Model model) { String word0 = "Hello "; String word1 = "World!"; //将数据添加到视图数据容器中 model.addAttribute("word0",word0); model.addAttribute("word1",word1); return "Hello.ftl"; } } 6. 编写Hello.ftl模板文件 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type"content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h2>${word0}${word1}</h2> </body> </html> 7. 将项目添加进Tomcat7中并运行 8. 浏览器中输入http://localhost:8080/mavenspringmvcfreemarker/helloWorld Freemarker插值详解 FreeMarker生成静态页面中文乱码解决办法