zs7456

haha!
随笔 - 4, 文章 - 1, 评论 - 31, 引用 - 0
数据加载中……

jsp生成静态页面遇到的一些问题

看见别人网站上都是静态页面,心里痒痒的,昨天晚上自己试着写了一下
不过只能写出非常简单的一部分,在静态页面里分页还不会做,还有待研究

NewsForm.java
  1/*
  2 * Generated by MyEclipse Struts
  3 * Template path: templates/java/JavaClass.vtl
  4 */

  5package com.news.form;
  6
  7import javax.servlet.http.HttpServletRequest;
  8import org.apache.struts.action.ActionErrors;
  9import org.apache.struts.action.ActionForm;
 10import org.apache.struts.action.ActionMapping;
 11
 12/** 
 13 * MyEclipse Struts
 14 * Creation date: 05-12-2008
 15 * 
 16 * XDoclet definition:
 17 * @struts.form name="newsForm"
 18 */

 19public class NewsForm extends ActionForm {
 20    /*
 21     * Generated fields
 22     */

 23
 24    /** title property */
 25    private String title;
 26
 27    /** content property */
 28    private String content;
 29
 30    /** author property */
 31    private String author;
 32
 33    /** id property */
 34    private Integer id;
 35    
 36    /** type property */
 37    private String type;
 38
 39    /*
 40     * Generated Methods
 41     */

 42
 43    /** 
 44     * Method validate
 45     * @param mapping
 46     * @param request
 47     * @return ActionErrors
 48     */

 49    public ActionErrors validate(ActionMapping mapping,
 50            HttpServletRequest request) {
 51        // TODO Auto-generated method stub
 52        return null;
 53    }

 54
 55    /** 
 56     * Method reset
 57     * @param mapping
 58     * @param request
 59     */

 60    public void reset(ActionMapping mapping, HttpServletRequest request) {
 61        // TODO Auto-generated method stub
 62    }

 63
 64    /** 
 65     * Returns the title.
 66     * @return String
 67     */

 68    public String getTitle() {
 69        return title;
 70    }

 71
 72    /** 
 73     * Set the title.
 74     * @param title The title to set
 75     */

 76    public void setTitle(String title) {
 77        this.title = title;
 78    }

 79
 80    /** 
 81     * Returns the content.
 82     * @return String
 83     */

 84    public String getContent() {
 85        return content;
 86    }

 87
 88    /** 
 89     * Set the content.
 90     * @param content The content to set
 91     */

 92    public void setContent(String content) {
 93        this.content = content;
 94    }

 95
 96    /** 
 97     * Returns the author.
 98     * @return String
 99     */

100    public String getAuthor() {
101        return author;
102    }

103
104    /** 
105     * Set the author.
106     * @param author The author to set
107     */

108    public void setAuthor(String author) {
109        this.author = author;
110    }

111
112    /** 
113     * Returns the id.
114     * @return Integer
115     */

116    public Integer getId() {
117        return id;
118    }

119
120    /** 
121     * Set the id.
122     * @param id The id to set
123     */

124    public void setId(Integer id) {
125        this.id = id;
126    }

127
128    public String getType() {
129        return type;
130    }

131
132    public void setType(String type) {
133        this.type = type;
134    }

135}

NewsAction.java
 1/*
 2 * Generated by MyEclipse Struts
 3 * Template path: templates/java/JavaClass.vtl
 4 */

 5package com.news.action;
 6
 7import java.io.FileInputStream;
 8import java.io.FileNotFoundException;
 9import java.io.FileOutputStream;
10import java.io.IOException;
11import java.util.Calendar;
12
13import javax.servlet.http.HttpServletRequest;
14import javax.servlet.http.HttpServletResponse;
15import org.apache.struts.action.Action;
16import org.apache.struts.action.ActionForm;
17import org.apache.struts.action.ActionForward;
18import org.apache.struts.action.ActionMapping;
19
20import com.news.Tools.Chinese;
21import com.news.dao.NewsDao;
22import com.news.form.NewsForm;
23
24/** 
25 * MyEclipse Struts
26 * Creation date: 05-12-2008
27 * 
28 * XDoclet definition:
29 * @struts.action path="/news" name="newsForm" input="/news_add.jsp" scope="request" validate="true"
30 * @struts.action-forward name="news_add_ok" path="/news_add_ok.html"
31 */

32public class NewsAction extends Action {
33    /*
34     * Generated Methods
35     */

36
37    /** 
38     * Method execute
39     * @param mapping
40     * @param form
41     * @param request
42     * @param response
43     * @return ActionForward
44     * @throws IOException 
45     */

46    public ActionForward execute(ActionMapping mapping, ActionForm form,
47            HttpServletRequest request, HttpServletResponse response) throws IOException {
48        NewsForm newsForm = (NewsForm) form;
49        NewsDao nd = new NewsDao();
50        Chinese c = new Chinese();
51        String title = c.toChinese(newsForm.getTitle());
52        String author = c.toChinese(newsForm.getAuthor());
53        String content = c.toChinese(newsForm.getContent());
54        
55        String filePath = "";
56        String template = "module/template.htm";
57        filePath = request.getRealPath("/")+template;
58           System.out.println(filePath);
59        String templateContent="";
60        FileInputStream fileinputstream = new FileInputStream(filePath);//读取模块文件
61            int lenght = fileinputstream.available();
62            byte bytes[] = new byte[lenght];
63        fileinputstream.read(bytes);
64        fileinputstream.close();
65        templateContent = new String(bytes);
66        templateContent=templateContent.replaceAll("###title###",title);
67        templateContent=templateContent.replaceAll("###content###",content);
68        templateContent=templateContent.replaceAll("###author###",author);//替换掉模块中相应的地方
69     // 根据时间得文件名
70        Calendar calendar = Calendar.getInstance();
71        String filename = String.valueOf(calendar.getTimeInMillis()) +".html";
72        filename = request.getRealPath("/")+"Html/news/"+filename;//生成的html文件保存路径
73        FileOutputStream fileoutputstream = new FileOutputStream(filename);//建立文件输出流
74        byte tag_bytes[] = templateContent.getBytes();
75        fileoutputstream.write(tag_bytes);
76        fileoutputstream.close();
77        String url = "Html/news/"+String.valueOf(calendar.getTimeInMillis())+".html";
78        String type = c.toChinese(newsForm.getType());
79        
80        nd.addNews(title, author, content,url,type);
81        return mapping.findForward("news_add_ok");
82    }

83}


template.html
 1<html>
 2<head>
 3<title>###title###</title>
 4<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
 5</head>
 6<body>
 7标题:###title###<br/>
 8作者:###author###<br/>
 9内容:###content###<br/>
10</body>
11</html>


还有部分文件就不贴出来了

这个确实能够把刚添加的数据变成.html文件,也能记录在数据库中,只要再写一个新的页面去读取数据库里存放的路径就能找到相对应的文件,不过还有一点没做好,我不知道怎么删除,如果在管理新闻信息的时候,我把数据库里的某一条记录给删除掉了,那么在服务器上想对应的某一个文件就要跟着删除,这个确实不知道怎么实现了。我有想过在生成html文件的时候把它在服务器的实际路径也给记录到数据库中去,然后删除的时候可以直接找到这个路径并且删除文件,因为还没时间试,所以不知道实现起来是否有困难。
在生成的静态页面中分页也是件麻烦事,判断用户是否登陆也不知道怎么用,毕竟是静态页面,它不能处理逻辑。。。哪个高手知道的,希望能帮小弟解决

posted on 2008-05-14 13:45 zs7456 阅读(2630) 评论(10)  编辑  收藏

评论

# re: jsp生成静态页面遇到的一些问题  回复  更多评论   

直接用像 Velocity 或 Freemarker 那样的 java 成熟的模板技术会简单些。
2008-05-14 14:54 | 隔叶黄莺

# re: jsp生成静态页面遇到的一些问题  回复  更多评论   

首先要定制规则,静态页面必须严格按照一定的规则来管理,这样路径可能动态的拼出来了。
静态分页建议参考一下sohu的列表页,做的是增量发布,可能需要仔细研究

判断用户是否登陆也不知道怎么用?为什么静态页面还要判断用户登录情况?如果确实要的话,可以用ajax
2008-05-14 18:36 | bjsuo

# re: jsp生成静态页面遇到的一些问题  回复  更多评论   

@bjsuo
因为有些信息必须要会员登陆后才能看的到,就像交友网站一样,用户在不注册登陆的情况下就看不到别人的联系方式等信息了
所以想在静态页面里判断用户是否登陆了,用ajax太麻烦了,请问还有其他的方式解决吗?
2008-05-14 18:41 | zs7456

# re: jsp生成静态页面遇到的一些问题  回复  更多评论   

@隔叶黄莺

这玩意值得研究
2008-05-14 18:42 | zs7456

# re: jsp生成静态页面遇到的一些问题  回复  更多评论   

呵呵.转到这里看了一下,我以前也整过类似的东西,感觉如果用那种方法整的话,再改一下,分页可能实现了,具体只有想法,没有整,呵呵,希望你看后能激发一点灵感出来http://www.blogjava.net/wyz191/archive/2008/04/17/120937.html#193892
2008-05-15 08:23 | java_蝈蝈

# re: jsp生成静态页面遇到的一些问题  回复  更多评论   

@zs7456
只是纯静态页面是不行的,首先你可以生成一个静态页面,页面内嵌一个动态页面,在动态页面里判断是否登录了,如果没有登录就输出脚本,让页面跳到注册页或者其它页,如果登录了就什么都不做了。
还有一个不好的方法,就是用cookie,但这样做不好
2008-05-15 09:20 | bjsuo

# re: jsp生成静态页面遇到的一些问题[未登录]  回复  更多评论   

@bjsuo
谢谢,我正准备使用这种方法呢,只是现在还没开始实现
2008-05-15 09:23 | zs7456

# re: jsp生成静态页面遇到的一些问题  回复  更多评论   

页面的静态化非常有价值,还节省服务器资源
2008-05-16 12:23 | 网上买书

# re: jsp生成静态页面遇到的一些问题  回复  更多评论   

你这根本不叫生成静态页面,简直扯淡,这是静态页面吗?
2008-05-19 11:24 | wangsoft

# re: jsp生成静态页面遇到的一些问题  回复  更多评论   

解决静态页面权限访问(是否登陆)的一种思想
用filter
1. 需要登陆后才能访问的*.html文件,放在一个文件夹里(如 hasLogin 文件下)

2. 一般的静态页面,不要放在hasLogin 文件夹里

3. 在web.xml里配置filter

<filter>
<filter-name>Test</filter-name>
<filter-class>包名.类名如(IsLoginFilter)</filter-class>
</filter>
<filter-mapping>
<filter-name>Test</filter-name>
<url-pattern>/hasLogin/*.html</url-pattern>
</filter-mapping>

4. 在 类 IsLoginFilter (implements Filter)做相应的处理就行了

5. OK
2008-08-03 10:05 | 273914440

只有注册用户登录后才能发表评论。


网站导航: