基于注解的SpringMVC+freemarker demo实例
 
web项目图

 
web.xml文件
- 
<?xml version="1.0" encoding="UTF-8"?>  
- 
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
- 
  <servlet>  
- 
      
- 
    <servlet-name>springmvc</servlet-name>  
- 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
- 
      
- 
      <init-param>     
- 
        <param-name>contextConfigLocation</param-name>     
- 
        <param-value>/WEB-INF/springmvc-servlet.xml</param-value>  
- 
        <!--默认:/WEB-INF/<servlet-name>-servlet.xml  
- 
        classpath方式:<param-value>classpath:/spring-xml/*.xml</param-value>      
- 
         -->     
- 
       </init-param>  
- 
      
- 
    <load-on-startup>1</load-on-startup>  
- 
  </servlet>  
- 
    
- 
  <servlet-mapping>  
- 
    <servlet-name>springmvc</servlet-name>  
- 
    <url-pattern>/</url-pattern>  
- 
  </servlet-mapping>  
- 
  <welcome-file-list>  
- 
    <welcome-file>index.jsp</welcome-file>  
- 
  </welcome-file-list>  
- 
</web-app>  
 
 
springmvc-servlet.xml文件
- 
<?xml version="1.0" encoding="UTF-8"?>  
- 
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"  
- 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
- 
    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.xsd  
- 
     http://www.springframework.org/schema/mvc   
- 
     http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd   
- 
     http://www.springframework.org/schema/aop   
- 
     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd   
- 
     http://www.springframework.org/schema/context   
- 
     http://www.springframework.org/schema/context/spring-context.xsd">  
- 
  
- 
       
- 
     <mvc:annotation-driven/>   
- 
        
- 
     <context:component-scan base-package="com.spring.freemarker" />  
- 
       
- 
          
- 
       
- 
       
- 
     <bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">  
- 
        <property name="templateLoaderPath" value="WEB-INF/ftl/" />  
- 
        <property name="defaultEncoding" value="UTF-8" />  
- 
     </bean>  
- 
       
- 
     <bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">  
- 
        <property name="suffix" value=".ftl" />  
- 
        <property name="contentType" value="text/html;charset=UTF-8" />  
- 
          
- 
        <property name="requestContextAttribute" value="rc" />  
- 
     </bean>  
- 
</beans>  
 
 
 FreeMarkerController类
- 
package com.spring.freemarker;  
- 
  
- 
import java.util.ArrayList;  
- 
import java.util.List;  
- 
  
- 
import javax.servlet.http.HttpServletRequest;  
- 
import javax.servlet.http.HttpServletResponse;  
- 
  
- 
import org.springframework.stereotype.Controller;  
- 
import org.springframework.web.bind.annotation.RequestMapping;  
- 
import org.springframework.web.servlet.ModelAndView;  
- 
  
- 
import com.spring.vo.User;  
- 
  
- 
@Controller  
- 
@RequestMapping("/home")  
- 
public class FreeMarkerController {  
- 
  
- 
    @RequestMapping("/index")  
- 
    public ModelAndView Add(HttpServletRequest request, HttpServletResponse response) {  
- 
  
- 
        User user = new User();  
- 
        user.setUsername("zhangsan");  
- 
        user.setPassword("1234");  
- 
        List<User> users = new ArrayList<User>();  
- 
        users.add(user);  
- 
        return new ModelAndView("index", "users", users);  
- 
    }  
- 
  
- 
}  
 
 User类
- 
package com.spring.vo;  
- 
  
- 
public class User {  
- 
  
- 
    private String username;  
- 
    private String password;  
- 
  
- 
    public String getUsername() {  
- 
        return username;  
- 
    }  
- 
  
- 
    public void setUsername(String username) {  
- 
        this.username = username;  
- 
    }  
- 
  
- 
    public String getPassword() {  
- 
        return password;  
- 
    }  
- 
  
- 
    public void setPassword(String password) {  
- 
        this.password = password;  
- 
    }  
- 
  
- 
}  
 
 
 index.ftl文件
- 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//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>  
- 
<#list users as user>  
- 
username : ${user.username}<br/>  
- 
password : ${user.password}  
- 
</#list>  
- 
</body>  
- 
</html>  
 
 部署到tomcat,运行:http://localhost:8080/springmvc/home/index
  显示结果:
  username : zhangsan
  password : 1234
基于注解SpringMVC+freemarker实例
原文:http://blog.csdn.net/u014723529/article/details/42107439