freemarker+struts2整合(四)

前言:前面已经讲过了freemarker的入门,还不了解的可以到我的博客里面找来看。

 

先展示我的项目目录结构,我是用IDEA开发的


1.导包

<!-- https://mvnrepository.com/artifact/org.freemarker/freemarker -->
<dependency>
  <groupId>org.freemarker</groupId>
  <artifactId>freemarker</artifactId>
  <version>2.3.23</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-core -->
<dependency>
  <groupId>org.apache.struts</groupId>
  <artifactId>struts2-core</artifactId>
  <version>2.3.4.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.struts.xwork/xwork-core -->
<dependency>
  <groupId>org.apache.struts.xwork</groupId>
  <artifactId>xwork-core</artifactId>
  <version>2.3.4.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
  <groupId>org.apache.commons</groupId>
  <artifactId>commons-lang3</artifactId>
  <version>3.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/ognl/ognl -->
<dependency>
  <groupId>ognl</groupId>
  <artifactId>ognl</artifactId>
  <version>3.0.5</version>
</dependency>


2.配置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"  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">

    <!--配置struts2核心过滤器-->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>*.action</url-pattern>
    </filter-mapping>

    <servlet>
        <servlet-name>freemarker</servlet-name>
        <servlet-class>freemarker.ext.servlet.FreemarkerServlet</servlet-class>
        <!--下面的配置freemarke的ftl文件或者html,jsp等页面的位置 -->
        <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>
        </init-param>

        <init-param>
            <param-name>template_update_delay</param-name>
            <param-value>0</param-value>
        </init-param>
        <init-param>
            <param-name>default_encoding</param-name>
            <param-value>ISO-8859-1</param-value>
        </init-param>
        <init-param>
            <param-name>number_format</param-name>
            <param-value>0.##########</param-value>
        </init-param>
         -->
        <!-- 是否和服务器(tommcat)一起启动。-->
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>freemarker</servlet-name>
        <url-pattern>*.html</url-pattern><!--页面的后缀,如果页面是ftl文件就写*.ftl,如果是html就写*.html-->
    </servlet-mapping>
</web-app>

3.配置struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <package name="freemarker" namespace="/user" extends="struts-default">
        <action name="*" class="com.rabbit.servlet.FreeMarkerAction" method="{1}">
            <result name="{1}">/{1}.html</result>
        </action>
    </package>
</struts>

4.编写静态页面


<html>
<head>
    <title>FreeMarker Hello World</title>
</head>
<body>
<table class="datatable">
    <#if users??>
        <#list users as user>
            <tr>
                <td>${user.firstname}</td> <td>${user.lastname}</td>
            </tr>
        </#list>
    </#if>
</table>
</body>
</html>


5.编写Action类

package com.rabbit.servlet;

import com.opensymphony.xwork2.ActionContext;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by vip on 2017/12/16.
 */
public class FreeMarkerAction {

    public String index() {
        ActionContext context = ActionContext.getContext();
        List<Map<String, Object>> users = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            Map<String, Object> user = new HashMap<>();
            user.put("firstname", "firstname" + i);
            user.put("lastname", "lastname" + i);
            users.add(user);
        }
        context.put("users", users);
        return "index";
    }

}

6.部署项目启动

访问地址:http://localhost:8080/user/index.action




展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: Age of Ai 设计师: meimeiellie
应支付0元
点击重新获取
扫码支付

支付成功即可阅读