jsp生成靜態(tài)頁(yè)面遇到的一些問題
看見別人網(wǎng)站上都是靜態(tài)頁(yè)面,心里癢癢的,昨天晚上自己試著寫了一下
不過只能寫出非常簡(jiǎn)單的一部分,在靜態(tài)頁(yè)面里分頁(yè)還不會(huì)做,還有待研究
1
/*
2
* Generated by MyEclipse Struts
3
* Template path: templates/java/JavaClass.vtl
4
*/
5
package com.news.form;
6
7
import javax.servlet.http.HttpServletRequest;
8
import org.apache.struts.action.ActionErrors;
9
import org.apache.struts.action.ActionForm;
10
import 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
*/
19
public 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
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

NewsAction.java
1
/*
2
* Generated by MyEclipse Struts
3
* Template path: templates/java/JavaClass.vtl
4
*/
5
package com.news.action;
6
7
import java.io.FileInputStream;
8
import java.io.FileNotFoundException;
9
import java.io.FileOutputStream;
10
import java.io.IOException;
11
import java.util.Calendar;
12
13
import javax.servlet.http.HttpServletRequest;
14
import javax.servlet.http.HttpServletResponse;
15
import org.apache.struts.action.Action;
16
import org.apache.struts.action.ActionForm;
17
import org.apache.struts.action.ActionForward;
18
import org.apache.struts.action.ActionMapping;
19
20
import com.news.Tools.Chinese;
21
import com.news.dao.NewsDao;
22
import 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
*/
32
public 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);//替換掉模塊中相應(yīng)的地方
69
// 根據(jù)時(shí)間得文件名
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
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

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
標(biāo)題:###title###<br/>
8
作者:###author###<br/>
9
內(nèi)容:###content###<br/>
10
</body>
11
</html>

2

3

4

5

6

7

8

9

10

11

還有部分文件就不貼出來了
這個(gè)確實(shí)能夠把剛添加的數(shù)據(jù)變成.html文件,也能記錄在數(shù)據(jù)庫(kù)中,只要再寫一個(gè)新的頁(yè)面去讀取數(shù)據(jù)庫(kù)里存放的路徑就能找到相對(duì)應(yīng)的文件,不過還有一點(diǎn)沒做好,我不知道怎么刪除,如果在管理新聞信息的時(shí)候,我把數(shù)據(jù)庫(kù)里的某一條記錄給刪除掉了,那么在服務(wù)器上想對(duì)應(yīng)的某一個(gè)文件就要跟著刪除,這個(gè)確實(shí)不知道怎么實(shí)現(xiàn)了。我有想過在生成html文件的時(shí)候把它在服務(wù)器的實(shí)際路徑也給記錄到數(shù)據(jù)庫(kù)中去,然后刪除的時(shí)候可以直接找到這個(gè)路徑并且刪除文件,因?yàn)檫€沒時(shí)間試,所以不知道實(shí)現(xiàn)起來是否有困難。
在生成的靜態(tài)頁(yè)面中分頁(yè)也是件麻煩事,判斷用戶是否登陸也不知道怎么用,畢竟是靜態(tài)頁(yè)面,它不能處理邏輯。。。哪個(gè)高手知道的,希望能幫小弟解決
posted on 2008-05-14 13:45 zs7456 閱讀(2650) 評(píng)論(10) 編輯 收藏