Quantcast
Channel: CSDN博客推荐文章
Viewing all articles
Browse latest Browse all 35570

struts2中使用freemarker 生成静态页面

$
0
0

按一下步骤走:

1.创建项目

2.导入struts2的相关jar文件

3.在web.xml中配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
	http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>


	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

4.创建struts.xml文件,具体内容如下:

在配置视图类型时,也可以直接用type="freemarker"这个访问指定的模板,在这里我用的是动态访问生成的html页面

<?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="wf" extends="struts-default">
		<action name="userList" class="org.Struts2">
			<!-- <result name="success" type="freemarker">/templates/template.ftl</result> -->
			<result type="redirect">${url}</result>
		</action>
	</package>
</struts>

5,创建javaBean  User.java

package org;

public class User {
	private  String name;
	private int id;
	private int age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	
	
}

6.然后创建CreatHtml.java,执行时调用模板类生成指定的页面

package util;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Locale;
import java.util.Map;
import org.apache.struts2.ServletActionContext;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
public class CreatHtml {
	public void init(String ftl, String htmlName, Map map) throws IOException,TemplateException {
		// 创建Configuration对象
		Configuration cfg = new Configuration();
		// 设置FreeMarker的模版文件位置
		cfg.setServletContextForTemplateLoading(ServletActionContext.getServletContext(), "templates");
		cfg.setEncoding(Locale.getDefault(), "utf-8");


		// 创建Template对象
		Template template = cfg.getTemplate(ftl);
		template.setEncoding("utf-8");


		// 生成静态页面
		String path = ServletActionContext.getServletContext().getRealPath("/");
		File fileName = new java.io.File(path + htmlName);
		Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), "utf-8"));
		template.process(map, writer);
		writer.flush();
		writer.close();
	}
}
7.创建Action文件Struts2.java 具体内容如下:

package org;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import util.CreatHtml;
import com.opensymphony.xwork2.ActionSupport;
public class Struts2 extends ActionSupport {
	private String url;
	private List<User> list;

	public String execute() throws Exception {
		CreatHtml creatHtml = new CreatHtml();
		list = new ArrayList<User>();
		User user = null;
		for (int i = 0; i < 10; i++) {
			user = new User();
			user.setId(i);
			user.setName("name" + i);
			user.setAge(i + i);
			list.add(user);
		}
		Map<String, List<User>> map = new HashMap<String, List<User>>();
		map.put("userList", list);
		String fileName = "user.html";
		String ftl = "temalate.ftl";
		creatHtml.init(ftl, fileName, map);
		// url作为struts.xml中的result的视图返回页面
		this.url = fileName;
		return SUCCESS;
	}
	public List<User> getList() {
		return list;
	}
	public void setList(List<User> list) {
		this.list = list;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	private static final long serialVersionUID = 1L;
}
8.创建模板文件在根目录templates文件夹下template.ftl

<table style="text-align:center;FONT-SIZE: 11pt; WIDTH: 600px; FONT-FAMILY: 宋体; BORDER-COLLAPSE: collapse" borderColor=#3399ff cellSpacing=0 cellPadding=0 align=center border=1>   
    <tr>   
        <td><b>编号</b></td>   
        <td><b>用户名</b></td>  
        <td><b>年龄</b></td>    
    </tr>
    <#list userList as user>   
    <tr>   
        <td>${user.id}</td>   
        <td>${user.name}</td>
        <td>${user.age}</td>
    </tr>
	</#list>
</table>
9.浏览器输入http://localhost/Struts2/user.html,访问action  页面如下:







作者:liweifengwf 发表于2013-5-29 1:53:15 原文链接
阅读:98 评论:0 查看评论

Viewing all articles
Browse latest Browse all 35570

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>