我是这样的一个思路:
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.   

    我去前几天 还在些你这样的一个东西 现在已经成功了
    只不过 没在Action去实现他 而是在一个util帮助类去实现他 然后在 Action 去调用他
      

  2.   

    <result name=“SUCCESS”>/显示页面 </result>  
      

  3.   


    怎么显示页面?我在action中是根据ID获得.htm文件名的,不是固定的一个页面。
      

  4.   

     <result name=“SUCCESS”>/solid.jsp</result>  solid为一个固定页面,然后在这个固定的页面中forward 去那个 动态生成的页面。
      

  5.   

    我用的是struts2,好像没forward这个吧,况且关键就是像你说的如何forward进去那个动态生成的页面呢?帮____wo !!
      

  6.   

    <jsp:forward page="dynamic***.jsp"></jsp:forward>这个标签就是jsp的,和你用什么样的struts版本没有关系。
      

  7.   

    迩仔细看我上面的程序,我是用编号来命名.htm文件的,我如何从action方法load中获得这个文件?
      

  8.   

    迩所应用的dynamic***.jsp这个动态页面如何把我的这个.htm页面包含进来?
      

  9.   

    1、在action中定义一个get 方法,如:public String getHtm(){return this.fileName};
    2、<result name=“SUCCESS”>/${htm} </result>  
      

  10.   

    public static final String TITLE = "###title###";
    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>
      

  11.   

    不明白你说的:你可以直接转向那个htm文件啊。。我那个dynamic***.jsp只是一个简单的例子而已,根据你的程序的这几行:
    templatecontent=templatecontent.replaceAll("H_Title", title);
    templatecontent=templatecontent.replaceAll("Fname", author);
    templatecontent=templatecontent.replaceAll("H_Memo", content); 很明显H_Title,Fname,H_Memo这个几个特殊标记就应该是变量啊,用一个jsp完全可以达到要求,为什么还要那么麻烦去动态生成呢?是不是需求没有彻底说清楚?