我是这样的一个思路:
1.首先我编辑了一个要生成的模板页面;
2.把该模板页面内容存入数据库中;
3.再通过读取数据库的内容,替换掉相应的可编辑部分;
4.最后生成JSP页面。现在,我已经做到第三步,是用一个action方法替换掉该模板页面中的可编辑的部分,但我不知道该怎么样生成JSP页面。action方法: public String load() throws Exception {
if(newsId!=null){
news = inewsTService.getNew(newsId);
}else{
newsId=inewsTService.getMaxIDN();
}
//System.out.println("########"+news.getNewsId());
String title=news.getNewsName();
String author=news.getNewsAuthor();
String content=news.getNewsContent();
String templateid=news.getTemplateId();
//System.out.println("########"+title+author+content+templateid);
template=inewsTService.getTemplate(templateid);
String templatecontent=template.getTemplateContent();
templatecontent=templatecontent.replaceAll("H_Title", title);
templatecontent=templatecontent.replaceAll("Fname", author);
templatecontent=templatecontent.replaceAll("H_Memo", content);
//System.out.println("#####替换后#####"+templatecontent);
String filename=news.getNewsId()+".htm";
//System.out.println(filename);
FileOutputStream out=new FileOutputStream(filename);
byte bytes[]=templatecontent.getBytes();
out.write(bytes);
//System.out.println(filename);
out.close();
return SUCCESS;
}struts.xml中的action:
<action name="staticpage" class="staticPageAction" method="load">
<result></result> //这里如何写?
</action>上面只是我的一个思路,上面的struts.xml中的action怎么写?或者有没有其他更好的办法?帮______wo !!!!
1.首先我编辑了一个要生成的模板页面;
2.把该模板页面内容存入数据库中;
3.再通过读取数据库的内容,替换掉相应的可编辑部分;
4.最后生成JSP页面。现在,我已经做到第三步,是用一个action方法替换掉该模板页面中的可编辑的部分,但我不知道该怎么样生成JSP页面。action方法: public String load() throws Exception {
if(newsId!=null){
news = inewsTService.getNew(newsId);
}else{
newsId=inewsTService.getMaxIDN();
}
//System.out.println("########"+news.getNewsId());
String title=news.getNewsName();
String author=news.getNewsAuthor();
String content=news.getNewsContent();
String templateid=news.getTemplateId();
//System.out.println("########"+title+author+content+templateid);
template=inewsTService.getTemplate(templateid);
String templatecontent=template.getTemplateContent();
templatecontent=templatecontent.replaceAll("H_Title", title);
templatecontent=templatecontent.replaceAll("Fname", author);
templatecontent=templatecontent.replaceAll("H_Memo", content);
//System.out.println("#####替换后#####"+templatecontent);
String filename=news.getNewsId()+".htm";
//System.out.println(filename);
FileOutputStream out=new FileOutputStream(filename);
byte bytes[]=templatecontent.getBytes();
out.write(bytes);
//System.out.println(filename);
out.close();
return SUCCESS;
}struts.xml中的action:
<action name="staticpage" class="staticPageAction" method="load">
<result></result> //这里如何写?
</action>上面只是我的一个思路,上面的struts.xml中的action怎么写?或者有没有其他更好的办法?帮______wo !!!!
解决方案 »
- hibernateTemplate的分页,为什么firstResult不起作用???
- action 访问出错啊。。求大师指导。
- 最近在做项目时出现了java.net.UnknownHostException: struts.apache.org错误,请问这是什么错误?怎么解决?
- jdom1.1 apidocs在官网上怎么下载啊?
- 在Spring中调用EJB,EJB服务重起的时候程序报错
- maven 如何把两个项目打成一个jar给其他项目引用?
- MM高分求用伪语言写个最简单的Junit程序,帮帮忙
- C++向Java转型之路. 给点建议! 谢谢
- 怎样从javaservlet到ejb 高分求助
- 如何在文件中查找某字符串出现的次数!waitting online!
- struts 生成静态页面问题
- JNI java.lang.UnsatisfiedLinkError: no KeyManager in java.library.path
只不过 没在Action去实现他 而是在一个util帮助类去实现他 然后在 Action 去调用他
怎么显示页面?我在action中是根据ID获得.htm文件名的,不是固定的一个页面。
2、<result name=“SUCCESS”>/${htm} </result>
public static final String BODY = "###body###";
public static final String REGTIME = "###regTime###";
public static final String DESCRIPTION = "###description###";
public static final String COPYFROM = "###copyFrom###";
// 根据NewsDataForm 的传来的值和读取的模板生成 jsp文件
public static boolean SaveNewsByTemplate(String newsdataname, String dir,
NewsData NewsDataForm) {
boolean issuccess = false;
try {
String templateContent = "";
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(dir
+ "\\admin\\news\\template\\templete.jsp"));
int lenght = bis.available();
byte bytes[] = new byte[lenght];
bis.read(bytes);
bis.close();
templateContent = new String(bytes);
templateContent = templateContent.replaceAll(TITLE, NewsDataForm
.getTitle());
templateContent = templateContent.replaceAll(BODY, NewsDataForm
.getContent());
templateContent = templateContent.replaceAll(REGTIME, NewsDataForm
.getRegTime().toString());
templateContent = templateContent.replaceAll(DESCRIPTION,
NewsDataForm.getDescription());
templateContent = templateContent.replaceAll(COPYFROM, NewsDataForm
.getCopyFrom());
String filename = newsdataname + ".jsp";
String file = dir + "\\news\\" + newsdataname.substring(0, 6)
+ "\\";// 生成的jsp文件保存路径
if (!(new File(file).isDirectory())) {
new File(file).mkdirs();
}
File f = new File(file);
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(f + "\\" + filename));
// 建立文件输出流
byte tag_bytes[] = templateContent.getBytes("utf-8");
bos.write(tag_bytes);
bos.flush();
bos.close();
issuccess = true;
;
} catch (Exception e) {
e.printStackTrace();
issuccess = false;
}
return issuccess;
}对应的模板为
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles"%>
<%@ taglib uri="/WEB-INF/c.tld" prefix="c"%>
<%@ taglib uri="/WEB-INF/fn.tld" prefix="fn"%>
<%@ taglib uri="/WEB-INF/fmt.tld" prefix="fmt"%>
<div class="text_body_left_b">
<div class="text_body_left_b_top">
<ul>
<span class="ttt">
<li>
###title###
</li> </span>
<span class="ttt2">
<li>
时间: ###regTime### 来源:###copyFrom###
</li> </span>
</ul>
</div>
<div class="miaoshu">
<ul>
<li>
###description###
</li>
</ul>
</div>
<div class="text_body_left_b_bottom">
<ul>
<li>
###body###
</li>
</ul>
</div>
</div>
templatecontent=templatecontent.replaceAll("H_Title", title);
templatecontent=templatecontent.replaceAll("Fname", author);
templatecontent=templatecontent.replaceAll("H_Memo", content); 很明显H_Title,Fname,H_Memo这个几个特殊标记就应该是变量啊,用一个jsp完全可以达到要求,为什么还要那么麻烦去动态生成呢?是不是需求没有彻底说清楚?