SpringMVC+FreeMarker(FTL)Integration example

来源:互联网 发布:免费报税软件 编辑:程序博客网 时间:2024/06/05 13:12

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

<?xmlversion="1.0"encoding="UTF-8"?>
<web-appxmlns: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

<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="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 -->
    <beanid="freemarkerConfig"class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
      <propertyname="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.
    -->
    <beanid="viewResolver"class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
      <propertyname="cache"value="true"/>
      <propertyname="prefix"value=""/>
      <propertyname="suffix"value=".ftl"/>
    </bean>
 
    <context:component-scan
        base-package="com.dufeng.core"/>
          
</beans>
File: com/dufeng/core/UserController.java

package com.dufeng.core;
 
importjava.util.ArrayList;
importjava.util.List;
 
importorg.springframework.stereotype.Controller;
importorg.springframework.ui.ModelMap;
importorg.springframework.web.bind.annotation.ModelAttribute;
importorg.springframework.web.bind.annotation.RequestMapping;
importorg.springframework.web.bind.annotation.RequestMethod;
 
@Controller
publicclass UserController {
    /**
     * Static list of users to simulate Database
     */
    privatestatic List<User> userList = newArrayList<User>();
 
    //Initialize the list with some data for index screen
    static{
        userList.add(newUser("Bill","Gates"));
        userList.add(newUser("Steve","Jobs"));
        userList.add(newUser("Larry","Page"));
        userList.add(newUser("Sergey","Brin"));
        userList.add(newUser("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)
    publicString 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)
    publicString 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;
 
publicclass User {
    privateString firstname;
    privateString lastname;
 
    publicUser() {
    }
 
    publicUser(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>
<divid="header">
<H2>
    <ahref="http://viralpatel.net"><imgheight="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>
 
<divid="content">
     
  <fieldset>
    <legend>Add User</legend>
  <formname="user"action="add.html"method="post">
    Firstname: <inputtype="text"name="firstname"/> <br/>
    Lastname: <inputtype="text"name="lastname"/>   <br/>
    <inputtype="submit"value="   Save   " />
  </form>
  </fieldset>
  <br/>
  <tableclass="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