linux怎么查看本机内存大小
321
2022-09-07
struts2+freemarker 生成静态页面
struts2+freemarker 生成静态页面
1.创建项目
2.导入struts2的相关jar文件
3.在web.xml中配置如下:
?xml version= "1.0" encoding= "UTF-8" ?> < web-app id= "WebApp_ID" version= "2.4" xmlns= " xmlns:xsi= " xsi:schemaLocation= "> < display-name>MyFreemark< /display-name> < filter> < filter-name>struts< /filter-name> < filter-class>org.apache.struts2.dispatcher.FilterDispatcher< /filter-class> < /filter> < filter-mapping> < filter-name>struts< /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文件,且内容如下:
?xml version= "1.0" encoding= "GBK" ?> < !DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "> < struts> < constant name= "struts.devMode" value= "true" /> < constant name= "struts.i18n.encoding" value= "GBK" /> < package name= "default" namespace= "/" extends= "struts-default" > < action name= "list" class= "com.zsw.action.ListAction" > < result type= "redirect" >/${msg}< /result> < /action> < /package> < /struts>
5.创建CreateHtml.java用来生成静态页面
package com.zsw.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 CreateHtml { /** * * @param ftl 模板文件 * @param htmlName html文件名称 * @param map map保存数据 * @param relaPath //在这里没有用到 * @throws IOException * @throws TemplateException */ public void init( String ftl, String htmlName, Map map, String relaPath) throws IOException, TemplateException { //创建Configuration对象 Configuration cfg = new Configuration(); cfg.setServletContextForTemplateLoading(ServletActionContext.getServletContext(), "/"); cfg.setEncoding(Locale.getDefault(), "gbk"); //创建Template对象 Template template = cfg.getTemplate(ftl); template.setEncoding( "gbk"); //生成静态页面 String path = ServletActionContext.getServletContext().getRealPath( "/"); File fileName = new File(path + htmlName); Writer out = new BufferedWriter( new OutputStreamWriter( new FileOutputStream(fileName), "gbk")); template.process(map, out); out.flush(); out.close(); }
6.创建Action文件ListAction.java,如下:
package com.zsw.action; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import com.opensymphony.xwork2.ActionSupport; import com.zsw.util.CreateHtml; import com.zsw.vo.Person; import freemarker.template.TemplateException; public class ListAction extends ActionSupport { private String msg; public String getMsg() { return msg; } public void setMsg( String msg) { this.msg = msg; } public String execute() { CreateHtml createHtml = new CreateHtml(); List
7.创建模板文件person.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> < td>< b>性别< /b>< /td> < td>< b>年龄< /b>< /td> < /tr> < #list personlist as person> < tr> < td>${person.id}< /td> < td>${person.name}< /td> < td>${person.sex}< /td> < td>${person.age}< /td> < /tr> < /#list>
< /table>
8.启动服务器,访问地址:http://localhost:8080/MyFreemark/list
效果图如下:
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~