1.atruts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<!--配置开发者模式-->
<constant name="struts.devMode" value="true"></constant>
<!--清空服务器缓存-->
<constant name="struts.serve.static" value="false"></constant>
<!--清空浏览器缓存-->
<constant name="struts.serve.static.browserCache" value="false"></constant>
<!--中文编码-->
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<!--动态方法调用-->
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
<!--前台是否允许OGNL静态方法调用-->
<constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>
<!--启用rest风格的请求-->
<constant name="struts.enable.SlashesInActionNames" value="true"></constant>
<!--真实做开发的时候 需要将struts2的标签模式修改为simple-->
<constant name="struts.ui.theme" value="xhtml"></constant>
<!--修改可以匹配的后缀-->
<constant name="struts.action.extension" value="do,action,xhtml,,"></constant>
<package name="core" extends="json-default">
</package>
</struts>
2.web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="starter" 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">
<display-name>Struts 2 - Maven Archetype - Starter</display-name>
<!-- Filters -->
<filter>
<filter-name>struts2-execute</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2-execute</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Welcome file lists -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
3.pom.xml
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- Struts 2 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>${struts2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-convention-plugin</artifactId>
<version>${struts2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-json-plugin</artifactId>
<version>${struts2.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-jfreechart-plugin -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-jfreechart-plugin</artifactId>
<version>${struts2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-jasperreports-plugin</artifactId>
<version>${struts2.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/net.sf.jasperreports/jasperreports -->
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>5.6.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jfree/jfreechart -->
<dependency>
<groupId>org.jfree</groupId>
<artifactId>jfreechart</artifactId>
<version>1.0.19</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.18</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-config-browser-plugin</artifactId>
<version>${struts2.version}</version>
</dependency>
<!-- Servlet & Jsp -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
4.代码
@Action(value = "doFreeMark",results = {@Result(name="success",type="freemarker",location = "/template/index.htm")})
public String doFreeMarker(){
System.out.println("helloworld");
news=new NewsBean();
news.setNewsid(1);
news.setTitle("未来的美好就靠大家了...");
news.setSendTime("2018-04-19-14:28:30");
news.setAuthor("新华社");
news.setContent("小明说:交给我把....");
return "success";
}
@Action(value = "buildHTML",results = {@Result(name="build",location = "/success.jsp")})
public String BuildHTML() throws IOException, TemplateException {
String npath= ServletActionContext.getServletContext().getRealPath("/n");
//准备好数据
Calendar calendar=Calendar.getInstance();
int year= calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH)+1;
int day=calendar.get(Calendar.DAY_OF_MONTH);
String smonth="";
if(month<10)
{
smonth="0"+month;
}
else {
smonth=month+"";
}
String dirPath=npath+"//"+year+"//"+smonth+day;
List<NewsBean>newsList=new ArrayList<NewsBean>();
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
for(int i=0;i<100;i++)
{
String uuidhtml=UUID.randomUUID().toString().replaceAll("-","");
Date d=new Date();
news=new NewsBean();
news.setNewsid(i);
news.setTitle("未来的美好就靠大家了..."+i);
news.setSendTime(format.format(d));
news.setAuthor("新华社"+i);
news.setContent("小明:交给我把....");
Map<String,Object> map=new HashMap<String,Object>();
map.put("news",news);
TemplateUtils.buildHtml("index.htm","/template",map,dirPath,uuidhtml+".html");
//假的数据
}
return "build";
}
public class TemplateUtils {
public static void buildHtml(String templateName,String templateDir,Map data,String dir,String htmlName) throws IOException, TemplateException {
//1.初始化配置
Configuration cfg=new Configuration();
//2.设置模板的文件
cfg.setServletContextForTemplateLoading(ServletActionContext.getServletContext(),templateDir);
//3.设置编码格式
cfg.setDefaultEncoding("UTF-8");
//4.准备好数据
Template template = cfg.getTemplate(templateName);
template.setEncoding("UTF-8");
File filedir=new File(dir);
if(!filedir.exists())
{
filedir.mkdirs();
}
Writer writer=new PrintWriter(new File(filedir,htmlName),"UTF-8");
template.process(data,writer);
//5.生成页面
writer.flush();
writer.close();
}
}