FreeMarker与SpringMVC整合

This is FreeMarker-2.3.19 and Spring-framework-3.1.1.RELEASE


This is the index.jsp

<%@ page language="java" pageEncoding="UTF-8"%>
<a href="<%=request.getContextPath()%>/getUser">访问FreeMarker效果页面</a>
<br/>
<br/>
<a href="<%=request.getContextPath()%>/getUserName">访问JSP效果页面</a>

This is the web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
						http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	<servlet>
		<servlet-name>jadyer</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>jadyer</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>

This is SpringMVC Config File----jadyer-servlet.xml

<?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:mvc="http://www.springframework.org/schema/mvc"
	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/mvc
						http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
						http://www.springframework.org/schema/context 
						http://www.springframework.org/schema/context/spring-context-3.0.xsd">
	<context:component-scan base-package="com.jadyer"/>
	
	<mvc:annotation-driven/>
	
	<!-- 注意:FreeMarker与SpringMVC集成时,其配置一定要放在SpringMVC的配置之前 -->
	<!-- 否则:在访问"/getUser"时服务器就会报告[HTTP Status 404 - /WEB-INF/jsp/myfree.jsp] -->
	<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
		<!-- 指定FreeMarker模板文件目录 -->
		<property name="templateLoaderPath" value="/WEB-INF/ftl/"/>
	</bean>
	<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
		<property name="prefix" value=""/>
		<property name="suffix" value=".ftl"/>
		<property name="contentType" value="text/html; charset=UTF-8"/>
	</bean>
	
	<!-- 访问"/getUserName"时服务器发现找不到/WEB-INF/ftl/username.ftl,于是它就到这里来找了,结果自然是找到了 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/"/>
		<property name="suffix" value=".jsp"/>
	</bean>
</beans>

This is //WEB-INF//jsp//username.jsp

<%@ page language="java" pageEncoding="UTF-8"%>
<h2>${myname}</h2>

这是FreeMarker效果页面//WEB-INF//ftl//myfree.ftl

<h2>${myname}</h2>

最后是SpringMVC的控制器UserController.java

package com.jadyer.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class UserController {
	@RequestMapping("/getUser")
	public String getUser(Model model){
		model.addAttribute("myname", "I am 玄玉");
		return "myfree";
	}
	
	@RequestMapping("/getUserName")
	public String getUserName(Model model){
		model.addAttribute("myname", "I am Jadyer");
		return "username";
	}
}

你可能感兴趣的:(java,freemarker,String,服务器,Class,encoding)