struts2 整合freemakrer 简单实例

一、需要的jar包  在struts2 已经集成了 导入就可以了 如果启动报错 去掉spring相关的jar包即可

二、配置

1、根目录下新建 index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
 </head>
 <body>
  <s:form action="login">
   <s:textfield label="用户名" name="userInfo.username"></s:textfield>
   <s:password label="密码" name="userInfo.password"></s:password>
   <s:submit></s:submit>
  </s:form>


 </body>
</html>

2.web.xml 配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
 http://java.sun.com/xml/ns/javaee/web-app_2_5.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>

3、新建index文件,建test.ftl文件 内容如下<#--
<#assign s=JspTaglibs["/WEB-INF/struts-tags.tld "]>
-->

<html>
<body>
<#list list as l>
name:${l.name}<br>
age:${l.age}<br>
sex:${l.sex}<br>
<#if l=='core java'>
有这本书
</#if>
</#list>

</body>

</html>

4、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="default" extends="struts-default">
  <action name="login" class="com.action.LoginAction">
   <result type="redirect">/${msg}</result>
  </action>
 </package>

</struts>

 

5、新建Person.java 、UserInfo.java

 

package com.entity;

public class Person {

 private String name;
 private String age;
 private String sex;

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public String getAge() {
  return age;
 }

 public void setAge(String age) {
  this.age = age;
 }

 public String getSex() {
  return sex;
 }

 public void setSex(String sex) {
  this.sex = sex;
 }

}

 

package com.entity;

public class UserInfo {

 private String username;
 private String password;

 public String getUsername() {
  return username;
 }

 public void setUsername(String username) {
  this.username = username;
 }

 public String getPassword() {
  return password;
 }

 public void setPassword(String password) {
  this.password = password;
 }

}

6、新建StaticFreemarker.java

package com.action;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
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 StaticFreemarker {

 public void init(String ftl, String htmlName, Map map, String fileName)
   throws IOException, TemplateException {

  Configuration freemarkerCfg = new Configuration();
  freemarkerCfg.setServletContextForTemplateLoading(ServletActionContext
    .getServletContext(), "/" + fileName);

  freemarkerCfg.setEncoding(Locale.getDefault(), "utf-8");
  Template template;

  template = freemarkerCfg.getTemplate(ftl);
  template.setEncoding("utf-8");
  String path = ServletActionContext.getServletContext().getRealPath("/");
//  System.out.println(path);
  BufferedWriter buff = new BufferedWriter(
    new FileWriter(path + htmlName));
  File htmlFile = new File(path + htmlName);

  Writer out = new BufferedWriter(new OutputStreamWriter(
    new FileOutputStream(htmlFile), "utf-8"));
  template.process(map, out);
  buff.close();
  out.flush();
  out.close();
 }
}
LoginAction.java

package com.action;

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

import com.entity.Person;
import com.entity.UserInfo;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {

 private UserInfo userInfo;

 

 private String msg;

 private List userList = new ArrayList();

  public UserInfo getUserInfo() {
  return userInfo;
  }
 
  public void setUserInfo(UserInfo userInfo) {
  this.userInfo = userInfo;
  }

 public String getMsg() {
  return msg;
 }

 public void setMsg(String msg) {
  this.msg = msg;
 }

 public List getUserList() {
  return userList;
 }

 public void setUserList(List userList) {
  this.userList = userList;
 }

 @Override
 public String execute() throws Exception {
  StaticFreemarker sf = new StaticFreemarker();

  List<Person> person = new ArrayList<Person>();
  Person p = new Person();
  p.setAge("20");
  p.setName("张三");
  p.setSex("man");
  Person p2 = new Person();
  p2.setAge("40");
  p2.setName("李四");
  p2.setSex("man");
  person.add(p);
  person.add(p2);
  Map map = new HashMap();
  map.put("list", person);
  // map.put("pass", "123");
  String htmlfile = "test.html";

  try {
   sf.init("test.ftl", htmlfile, map, "index");
  } catch (Exception e) {
   e.printStackTrace();
  }
  this.msg = htmlfile;
  return this.SUCCESS;
 }

 

}

运行该项目即可 在tomcat下 会自动生成test.html 文件 

 

希望对大家有帮助

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值