使用struts1.2
首先在Lisener中设置ServletGroupTemplate相关配置。
模版文件位于WEB-INF/template下
public class InitLisener implements ServletContextListener{ static Log log = LogFactory.getLog(InitLisener.class); public void contextDestroyed(ServletContextEvent arg0) { ActionServletContext.destroyed(); } public void contextInitialized(ServletContextEvent event) { log.info("加载资源文件"); ServletContext context = event.getServletContext(); String configPath = context.getRealPath("//"); ServletGroupTemplate.intance().init(context); ServletGroupTemplate.intance().getGroup().config("", "${", "}"); ServletGroupTemplate.intance().getGroup().setCharset("UTF-8"); ServletGroupTemplate.intance().getGroup().registerFunction("fn.substring",new SubStringFunction()); ServletGroupTemplate.intance().getGroup().registerFunction("fn.length",new LengthFunction()); //context.setAttribute("templatepath", context.getRealPath("WEB-INF/template")); System.out.println(context.getRealPath("WEB-INF/template")); ActionServletContext.setServletContext(context); //PropertiesUtil.loadConfig(); } }
Action类,其中模拟了一些数据,输出路径为跟路径,文件名为index.html
public class StaticPageAction extends Action { /* * Generated Methods */ /** * Method execute * * @param mapping * @param form * @param request * @param response * @return ActionForward */ public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { PrintWriter pWriter = null; ActionForward forward = new ActionForward(); response.setCharacterEncoding("UTF-8"); String templatePath = (String) ActionServletContext.getServletContext() .getAttribute("templatepath"); String rootpath = ActionServletContext.getServletContext().getRealPath( "/"); rootpath = rootpath + "/index.html"; ListchannelList = getNewsData(); try { pWriter = response.getWriter(); Template template = ServletGroupTemplate.intance().getTemplate( "/index.html", request, response); template.set("channelList", channelList); template.set("user", "chenlei"); template.set("Chal", new Channel(1, "栏目test", "栏目说明", 0, 10, 30)); StringWriter writer = new StringWriter(); template.getText(writer); // false为从文件开始处写入 OutputStreamWriter outWriter = new OutputStreamWriter( new FileOutputStream(rootpath, false), "UTF-8"); System.out.println(writer.toString()); Writer out = new BufferedWriter(outWriter); out.write(writer.toString()); out.flush(); out.close(); pWriter.print("输出首页成功!返回>"); } catch (IOException e) { pWriter.print("输出首页失败!"); } catch (BeeException e) { // e.printStackTrace(); } return null; } private List getNewsData() { List newList = new ArrayList (20); for (int i = 1; i <= 10; i++) { Channel chnnel = new Channel(i, "栏目" + i, "栏目说明" + i, 0, 10, 30); for (int j = 1; j <= 11; j++) { News news = new News( j + i, "新闻消息" + j + i, "新华社消息: 控制语句占位符号是/ 你好!${user}
${Chal.chnnelTitle} ${channelList.~size} 栏目列表${Channel.chnnelTitle}没有数据
- [${fn.substring(News.channelTitle,0,2)}…${News.channelTitle} ]-${News.title}---[${News.newdate,dateFormat='yyyy-MM-dd'}]