<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    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 閱讀(2647) 評論(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.tkk7.com/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

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 亚洲国产成人久久精品99| 亚洲综合一区二区国产精品| 中国国产高清免费av片| 亚洲国产精品自在在线观看| 一二三四在线观看免费高清中文在线观看| 青青在线久青草免费观看| 亚洲一区二区三区乱码在线欧洲| a毛片视频免费观看影院| 亚洲一级视频在线观看| 精品亚洲视频在线观看| 欧洲精品成人免费视频在线观看| 亚洲A∨无码一区二区三区| 天天摸天天操免费播放小视频| 亚洲精品视频在线播放| 国产一级理论免费版| 成全视频免费观看在线看| 亚洲国产成人久久一区二区三区| 成年女人毛片免费视频| 国产午夜无码精品免费看动漫| 狠狠亚洲婷婷综合色香五月排名| 男女啪啪免费体验区| 亚洲人成综合在线播放| 亚洲精品久久久www| 成人免费看片又大又黄| 爱丫爱丫影院在线观看免费 | 亚洲最大在线视频| 亚洲视频一区二区| 最近最好的中文字幕2019免费| 国产亚洲综合视频| 亚洲影院在线观看| 国产亚洲精品影视在线产品 | 免费在线观看的黄色网址| 免费成人福利视频| 中国一级毛片免费看视频| 亚洲国产成人AV网站| 亚洲深深色噜噜狠狠网站| 亚洲伦理一区二区| 国产成人麻豆亚洲综合无码精品| 久久精品视频免费看| sss日本免费完整版在线观看| 久久久久久久综合日本亚洲|