freemarker + servlet实现 list 显示 及 页面跳转

说明:该实例解决.ftl模板页面跳转问题。

         解决servlet作用域存值动态改变模板内容。

         doGet方法 和 doPost方法的灵活应用。

         List 遍历显示 和 动态添加内容。

 

1. 首先我们来看一下项目结构图:

2. 新建User实体类:

package net.viralpatel.freemarker;

public class User {
    private String firstname;
    private String lastname;
    
    public User(){
    }
    
    public User(String firstname,String lastname){
        this.firstname=firstname;
        this.lastname=lastname;
    }

    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }
    
    
}

3. 新建HelloServlet类(继承 HttpServlet):

package net.viralpatel.freemarker;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloServlet  extends HttpServlet {
    private static final long serialVersionUID = 1L;
    
    private static List<User> userList=new ArrayList<User>();
    
    static{
        userList.add(new User("Bill","王恒"));
        userList.add(new User("Steve","王恒"));
        userList.add(new User("Larry","王恒"));
        userList.add(new User("Sergey","王恒"));
        userList.add(new User("Larry","王恒"));
    }
    
    protected void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {
        req.setAttribute("users", userList);
        
        req.getRequestDispatcher("/index.ftl").forward(req, resp);
    }
    
    
    protected void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {
        String firstname=req.getParameter("firstname");
        String lastname=req.getParameter("lastname");
        
        if(null !=firstname && null != lastname
                && !firstname.isEmpty() && !lastname.isEmpty()){
            synchronized (userList) {
                userList.add(new User(firstname,lastname));
            }
        }
        doGet(req, resp);
    }
    
    
    
}

注意:代码中标红内容 理解巧妙之处。

4. 新建index.ftl:

<html>
<head><title>ViralPatel.net - FreeMarker Hello World</title>

<body>
<form name="user" action="hello" method="post">
XiaoGongZhu: <input type="text" name="firstname" /> <br/>
XiaoXianNv: <input type="text" name="lastname" /> <br/>
<input type="submit" value="Save" />
</form>

<table class="datatable">
<tr>
<th>Firstname</th> <th>Lastname</th>
</tr>
<#list users as user>
<tr>
<td>${user.firstname}</td> <td>${user.lastname}</td>
</tr>
</#list>
</table>
</body>
</html>

5. 配置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" 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_Hello_World</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
<servlet>
        <servlet-name>freemarker</servlet-name>
        <servlet-class>freemarker.ext.servlet.FreemarkerServlet</servlet-class>

        <!-- FreemarkerServlet settings: -->
        <init-param>
            <param-name>TemplatePath</param-name>
            <param-value>/</param-value>
        </init-param>
        <init-param>
            <param-name>NoCache</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>ContentType</param-name>
            <param-value>text/html; charset=UTF-8</param-value>
            <!-- Forces UTF-8 output encoding! -->
        </init-param>

        <!-- FreeMarker settings: -->
        <init-param>
            <param-name>template_update_delay</param-name>
            <param-value>0</param-value>
            <!-- 0 is for development only! Use higher value otherwise. -->
        </init-param>
        <init-param>
            <param-name>default_encoding</param-name>
            <param-value>ISO-8859-1</param-value>
            <!-- The encoding of the template files. -->
        </init-param>
        <init-param>
            <param-name>number_format</param-name>
            <param-value>0.##########</param-value>
        </init-param>

        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>freemarker</servlet-name>
        <url-pattern>*.ftl</url-pattern>
    </servlet-mapping>


    <servlet>
        <servlet-name>hello_servlet</servlet-name>
        <servlet-class>net.viralpatel.freemarker.HelloServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>hello_servlet</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>

    <!--
        Prevent the visiting of MVC Views from outside the servlet container.
        RequestDispatcher.forward/include should and will still work. Removing
        this may open security holes!
    -->
    <security-constraint>
        <web-resource-collection>
            <web-resource-name>FreeMarker MVC Views</web-resource-name>
            <url-pattern>*.ftl</url-pattern>
        </web-resource-collection>
        <auth-constraint>
            <!-- Nobody is allowed to visit these -->
        </auth-constraint>
    </security-constraint>

</web-app>

注意:代码中红色标识为必须配置内容,其他可以根据自己需要进行配置。

 

6. 部署项目,测试 (http://localhost:8080/Freemarker_Hello_World/hello):

 

 

备注:可能遇到的错误,如:

解决办法:检查web.xml是否配置:

<servlet>
        <servlet-name>freemarker</servlet-name>
        <servlet-class>freemarker.ext.servlet.FreemarkerServlet</servlet-class>

        <!-- FreemarkerServlet settings: -->
        <init-param>
            <param-name>TemplatePath</param-name>
            <param-value>/</param-value>
        </init-param>
        <init-param>
            <param-name>NoCache</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>ContentType</param-name>
            <param-value>text/html; charset=UTF-8</param-value>
            <!-- Forces UTF-8 output encoding! -->
        </init-param>

        <!-- FreeMarker settings: -->
        <init-param>
            <param-name>template_update_delay</param-name>
            <param-value>0</param-value>
            <!-- 0 is for development only! Use higher value otherwise. -->
        </init-param>
        <init-param>
            <param-name>default_encoding</param-name>
            <param-value>ISO-8859-1</param-value>
            <!-- The encoding of the template files. -->
        </init-param>
        <init-param>
            <param-name>number_format</param-name>
            <param-value>0.##########</param-value>
        </init-param>

        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>freemarker</servlet-name>
        <url-pattern>*.ftl</url-pattern>
    </servlet-mapping>

 

posted on 2017-06-13 10:19  forever_2h  阅读(3226)  评论(0编辑  收藏  举报

导航