SpringMVC+FreeMarker(FTL)Integration example

File: /WebContent/WEB-INF/web.xml

<? xml version = "1.0" encoding = "UTF-8" ?>
< web-app xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
         xmlns = "http://java.sun.com/xml/ns/javaee"
         xmlns:web = "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         id = "WebApp_ID" version = "3.0" >
         
   < display-name >Freemarker_SpringMVC_example</ display-name >
   < welcome-file-list >
     < welcome-file >index.html</ welcome-file >
     < welcome-file >index.jsp</ welcome-file >
   </ welcome-file-list >
 
   < 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 >*.html</ url-pattern >
     </ servlet-mapping >
</ web-app >
File: /WebContent/WEB-INF/spring-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: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-3.0.xsd                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd" >
 
     <!-- freemarker config -->
     < bean id = "freemarkerConfig" class = "org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer" >
       < property name = "templateLoaderPath" value = "/WEB-INF/ftl/" />
     </ bean >
     
     <!--
       View resolvers can also be configured with ResourceBundles or XML files. If you need
       different view resolving based on Locale, you have to use the resource bundle resolver.
     -->
     < bean id = "viewResolver" class = "org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver" >
       < property name = "cache" value = "true" />
       < property name = "prefix" value = "" />
       < property name = "suffix" value = ".ftl" />
     </ bean >
 
     < context:component-scan
         base-package = "com.dufeng.core" />
          
</ beans >
File: com/dufeng/core/UserController.java

package com.dufeng.core ;
 
import java.util.ArrayList;
import java.util.List;
 
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
 
@Controller
public class UserController {
     /**
      * Static list of users to simulate Database
      */
     private static List<User> userList = new ArrayList<User>();
 
     //Initialize the list with some data for index screen
     static {
         userList.add( new User( "Bill" , "Gates" ));
         userList.add( new User( "Steve" , "Jobs" ));
         userList.add( new User( "Larry" , "Page" ));
         userList.add( new User( "Sergey" , "Brin" ));
         userList.add( new User( "Larry" , "Ellison" ));
     }
 
     /**
      * Saves the static list of users in model and renders it
      * via freemarker template.
      *
      * @param model
      * @return The index view (FTL)
      */
     @RequestMapping (value = "/index" , method = RequestMethod.GET)
     public String index( @ModelAttribute ( "model" ) ModelMap model) {
 
         model.addAttribute( "userList" , userList);
 
         return "index" ;
     }
 
     /**
      * Add a new user into static user lists and display the
      * same into FTL via redirect
      *
      * @param user
      * @return Redirect to /index page to display user list
      */
     @RequestMapping (value = "/add" , method = RequestMethod.POST)
     public String add( @ModelAttribute ( "user" ) User user) {
 
         if ( null != user && null != user.getFirstname()
                 && null != user.getLastname() && !user.getFirstname().isEmpty()
                 && !user.getLastname().isEmpty()) {
 
             synchronized (userList) {
                 userList.add(user);
             }
 
         }
 
         return "redirect:index.html" ;
     }
 
}
File:com/dufeng/core/User.java

package com.dufeng.core ;
 
public class User {
     private String firstname;
     private String lastname;
 
     public User() {
     }
 
     public User(String firstname, String lastname) {
         this .firstname = firstname;
         this .lastname = lastname;
 
     }
 
     //Add Getter and Setter methods
 
}
File: /WebContent/WEB-INF/ftl/index.ftl


< html >
< head >< title >ViralPatel.net - FreeMarker Spring MVC Hello World</ title >
< body >
< div id = "header" >
< H2 >
     < a href = "http://viralpatel.net" >< img height = "37" width = "236" border = "0px" src = "http://viralpatel.net/blogs/wp-content/themes/vp/images/logo.png" align = "left" /></ a >
     FreeMarker Spring MVC Hello World
</ H2 >
</ div >
 
< div id = "content" >
     
   < fieldset >
     < legend >Add User</ legend >
   < form name = "user" action = "add.html" method = "post" >
     Firstname: < input type = "text" name = "firstname" /> < br />
     Lastname: < input type = "text" name = "lastname" />   < br />
     < input type = "submit" value = "   Save   " />
   </ form >
   </ fieldset >
   < br />
   < table class = "datatable" >
     < tr >
         < th >Firstname</ th >  < th >Lastname</ th >
     </ tr >
     <#list model["userList"] as user>
     < tr >
         < td >${user.firstname}</ td > < td >${user.lastname}</ td >
     </ tr >
     </#list>
   </ table >
 
</ div
</ body >
</ html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值