按一下步骤走:
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 {
- <span style="white-space:pre"> </span>public void init(String ftl, String htmlName, Map map) throws IOException,TemplateException {
- <span style="white-space:pre"> </span>// 创建Configuration对象
- <span style="white-space:pre"> </span>Configuration cfg = new Configuration();
- <span style="white-space:pre"> </span>// 设置FreeMarker的模版文件位置
- <span style="white-space:pre"> </span>cfg.setServletContextForTemplateLoading(ServletActionContext.getServletContext(), "templates");
- <span style="white-space:pre"> </span>cfg.setEncoding(Locale.getDefault(), "utf-8");
- <span style="white-space:pre"> </span>// 创建Template对象
- <span style="white-space:pre"> </span>Template template = cfg.getTemplate(ftl);
- <span style="white-space:pre"> </span>template.setEncoding("utf-8");
- <span style="white-space:pre"> </span>// 生成静态页面
- <span style="white-space:pre"> </span>String path = ServletActionContext.getServletContext().getRealPath("/");
- <span style="white-space:pre"> </span>File fileName = new java.io.File(path + htmlName);
- <span style="white-space:pre"> </span>Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), "utf-8"));
- <span style="white-space:pre"> </span>template.process(map, writer);
- <span style="white-space:pre"> </span>writer.flush();
- <span style="white-space:pre"> </span>writer.close();
- <span style="white-space:pre"> </span>}
- }
- 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;
- }
- <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>
10.源文件实例下载 http://download.csdn.net/detail/liweifengwf/5470833
转自:https://i-blog.csdnimg.cn/blog_migrate/1d84662b6408566b4899d32101e2dc10.png