• springmvc结合freemarker,非自定义标签


     参考:http://viralpatel.net/blogs/spring-mvc-freemarker-ftl-example/

    上图:

    目录层级:

     启动后的访问地址:http://localhost:8080/Freemarker_SpringMVC_example/index.html

    源码:链接:http://pan.baidu.com/s/1bTtvEQ 密码:k00e

     User.java:

     1 package net.viralpatel;
     2 
     3 public class User {
     4     private String firstname;
     5     private String lastname;
     6 
     7     public User() {
     8     }
     9 
    10     public String getFirstname() {
    11         return firstname;
    12     }
    13 
    14     public void setFirstname(String firstname) {
    15         this.firstname = firstname;
    16     }
    17 
    18     public String getLastname() {
    19         return lastname;
    20     }
    21 
    22     public void setLastname(String lastname) {
    23         this.lastname = lastname;
    24     }
    25 
    26     public User(String firstname, String lastname) {
    27         this.firstname = firstname;
    28         this.lastname = lastname;
    29 
    30     }
    31 
    32     // Add Getter and Setter methods
    33 
    34 }

    UserController.java:

     1 package net.viralpatel;
     2 
     3 import java.util.ArrayList;
     4 import java.util.List;
     5 
     6 import org.springframework.stereotype.Controller;
     7 import org.springframework.ui.ModelMap;
     8 import org.springframework.web.bind.annotation.ModelAttribute;
     9 import org.springframework.web.bind.annotation.RequestMapping;
    10 import org.springframework.web.bind.annotation.RequestMethod;
    11 /**
    12  * 参考自:http://viralpatel.net/blogs/spring-mvc-freemarker-ftl-example/
    13  * 本地服务启动后访问地址:http://localhost:8080/Freemarker_SpringMVC_example/index.html
    14  * 
    15  * @author Wei
    16  * @time  2016年12月14日 下午4:15:43
    17  */
    18 @Controller
    19 public class UserController {
    20     /**
    21      * Static list of users to simulate Database
    22      */
    23     private static List<User> userList = new ArrayList<User>();
    24 
    25     //Initialize the list with some data for index screen
    26     static {
    27         userList.add(new User("Bill", "Gates"));
    28         userList.add(new User("Steve", "Jobs"));
    29         userList.add(new User("Larry", "Page"));
    30         userList.add(new User("Sergey", "Brin"));
    31         userList.add(new User("Larry", "Ellison"));
    32     }
    33 
    34     /**
    35      * Saves the static list of users in model and renders it 
    36      * via freemarker template.
    37      * 
    38      * @param model 
    39      * @return The index view (FTL)
    40      */
    41     @RequestMapping(value = "/index", method = RequestMethod.GET)
    42     public String index(@ModelAttribute("model") ModelMap model) {
    43 
    44         model.addAttribute("userList", userList);
    45 
    46         return "index";
    47     }
    48 
    49     /**
    50      * Add a new user into static user lists and display the 
    51      * same into FTL via redirect 
    52      * 
    53      * @param user
    54      * @return Redirect to /index page to display user list
    55      */
    56     @RequestMapping(value = "/add", method = RequestMethod.POST)
    57     public String add(@ModelAttribute("user") User user) {
    58 
    59         if (null != user && null != user.getFirstname()
    60                 && null != user.getLastname() && !user.getFirstname().isEmpty()
    61                 && !user.getLastname().isEmpty()) {
    62 
    63             synchronized (userList) {
    64                 userList.add(user);
    65             }
    66 
    67         }
    68 
    69         return "redirect:index.html";
    70     }
    71 
    72 }

     ftl:

     1 <html>
     2 <head><title>ViralPatel.net - FreeMarker Spring MVC Hello World</title>
     3 <body>
     4 <div id="header">
     5 <H2>
     6     <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>
     7     FreeMarker Spring MVC Hello World
     8 </H2>
     9 </div>
    10 
    11 <div id="content">
    12     
    13   <fieldset>
    14       <legend>Add User</legend>
    15   <form name="user" action="add.html" method="post">
    16       Firstname: <input type="text" name="firstname" />    <br/>
    17       Lastname: <input type="text" name="lastname" />    <br/>
    18       <input type="submit" value="   Save   " />
    19   </form>
    20   </fieldset>
    21   <br/>
    22   <table class="datatable">
    23       <tr>
    24           <th>Firstname</th>  <th>Lastname</th>
    25       </tr>
    26     <#list model["userList"] as user>
    27       <tr>
    28           <strong><td>${user.firstname}</td></strong> <td>${user.lastname}</td>
    29       </tr>
    30     </#list>
    31   </table>
    32 
    33 </div>  
    34 </body>
    35 </html>  

    spring-servlet.xml:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
     4     xmlns:context="http://www.springframework.org/schema/context"
     5     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">
     6 
     7     <!-- freemarker config -->
     8     <bean id="freemarkerConfig"
     9         class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
    10         <property name="templateLoaderPath" value="/WEB-INF/ftl/" />
    11     </bean>
    12 
    13     <!-- View resolvers can also be configured with ResourceBundles or XML files. 
    14         If you need different view resolving based on Locale, you have to use the 
    15         resource bundle resolver. -->
    16     <bean id="viewResolver"
    17         class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
    18         <property name="cache" value="true" />
    19         <property name="prefix" value="" />
    20         <property name="suffix" value=".ftl" />
    21     </bean>
    22 
    23     <context:component-scan base-package="net.viralpatel" />
    24 
    25 </beans>

    web.xml:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     3         xmlns="http://java.sun.com/xml/ns/javaee" 
     4         xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
     5         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
     6         id="WebApp_ID" version="3.0">
     7         
     8   <display-name>Freemarker_SpringMVC_example</display-name>
     9   <welcome-file-list>
    10     <welcome-file>index.html</welcome-file>
    11     <welcome-file>index.jsp</welcome-file>
    12   </welcome-file-list>
    13 
    14   <servlet>
    15         <servlet-name>spring</servlet-name>
    16         <servlet-class>
    17             org.springframework.web.servlet.DispatcherServlet
    18         </servlet-class>
    19         <load-on-startup>1</load-on-startup>
    20     </servlet>
    21     <servlet-mapping>
    22         <servlet-name>spring</servlet-name>
    23         <!-- <url-pattern>*.html</url-pattern> -->
    24         <url-pattern>/</url-pattern>
    25     </servlet-mapping>
    26 </web-app>
  • 相关阅读:
    在Visual Studio中启用对jquery等javascript框架的智能感知
    网页的复制和防复制
    发现数据库对象的依赖关系
    这个世界并不缺少创意,而是缺少发现
    如何在ASP.NET程序中使用报表查看器控件并传递用户凭据
    Jquery调用webService的四种方法 【转载】
    浏览器的多线程技术
    .NET Framework 2.0的客户端是否可以调用WCF?
    在页面中实现内容的展开和收缩
    面向接口的编程(WCF)
  • 原文地址:https://www.cnblogs.com/Sunnor/p/6180346.html
Copyright © 2020-2023  润新知