速動(dòng)畫(huà)教程第二十八集
Struts+Velocity
簡(jiǎn)單集成
下載請(qǐng)到:http://this.oksonic.cn??討論請(qǐng)到 http://www.oksonic.com.cn? 注冊(cè)用戶后再轉(zhuǎn)到論壇
?
一.??
開(kāi)發(fā)環(huán)境:
1.??????
jdk1.5.x
?????????????????
http://java.sun.com/j2se/1.5.0/download.jsp
2.??????
Eclipse3.2.1??????????? http://www.eclipse.org
3.??????
MyEclipse5.1.0?????? http://www.myeclipseide.com
4.??????
Tomcat5.5.20? ??????
http://tomcat.apache.org/
5.??????
Velocity1.4
????????????? http://velocity.apache.org/
6.??????
velocity-tools-1.3???
http://velocity.apache.org/site/tools/
?
二.??
開(kāi)發(fā)步驟
1.??????
新建一個(gè)
web
項(xiàng)目
vm
2.??????
添加
struts
框架到項(xiàng)目中,使用
struts1.2
3.??????
拷貝
Velocity
包中的
velocity-1.4.jar
、
velocity-tools-view-1.3.jar
、
commons-collections-3.2.jar
、
velocity-tools-1.3.jar
文件到項(xiàng)目的
lib
目錄下,并刷新項(xiàng)目以載入包
4.??????
修改
web.xml
文件讓它識(shí)別
Velocity servlet
<
servlet
>
??????
<
servlet-name
>
velocity
</
servlet-name
>
??????
<
servlet-class
>
?????????? org.apache.velocity.tools.view.servlet.VelocityViewServlet
??????
</
servlet-class
>
???
</
servlet
>
???
<
servlet-mapping
>
??????
<
servlet-name
>
velocity
</
servlet-name
>
??????
<
url-pattern
>
*.vm
</
url-pattern
>
???
</
servlet-mapping
>
?
5.??????
創(chuàng)建一個(gè)
test
結(jié)構(gòu)(
test.jsp
、
testForm.java
、
testAction.java
)
6.??????
修改
struts
配置文件,加入導(dǎo)航配置,跳專到
test.vm
文件,內(nèi)容如下:
<
action-mappings
>
???
<
action
?????
attribute
=
"testForm"
?????
input
=
"/test.jsp"
?????
name
=
"testForm"
?????
path
=
"/test"
?????
scope
=
"request"
?
????
type
=
"com.oksonic.struts.action.TestAction"
>
?????
<forward name="success" path="/test.vm" />
?
</
action
>
?????
?
?
</
action-mappings
>
?
?
?
7.??????
修改
testAction.java
文件,代碼如下:
public
ActionForward execute(ActionMapping mapping, ActionForm form,
?????????? HttpServletRequest request, HttpServletResponse response) {
?????? TestForm testForm = (TestForm) form;
??????
//
對(duì)模型中的
test
屬性負(fù)值
?????? testForm.setTest(
"hello struts and velocity"
);
??????
//
將
form
對(duì)像存放到
request
對(duì)像中
?????? request.setAttribute(
"test"
, testForm);
??????
//
調(diào)用導(dǎo)航配置進(jìn)行跳轉(zhuǎn)
??????
return
mapping.findForward(
"success"
);
?
??? }
?
8.??????
根據(jù)
struts-config.xml
文件中的
<
forward
name
=
"success"
path
=
"/test.vm"
/>
配置內(nèi)容,需要在
webroot
目錄中新建一個(gè)
test.vm
文件此文件為
Velocity
模板文件,文件內(nèi)容如下:
<%@
page
pageEncoding=
"utf-8"
%>
<%
request.setCharacterEncoding(
"utf-8"
);
%>
<html>
<head>
<meta
http-equiv=
"Content-Type"
content=
"text/html; charset=utf-8"
>
<title>
struts velocity
六月天
</title>
</head>
<body>
?
${test.getTest()}
?
</body>
</html>
?
其中
${test.getTest()}
為取得
testForm
對(duì)像中的
test
屬性值
?
三.??
測(cè)試
部署項(xiàng)目
在地址欄中輸入
http://localhost/vm/test.do
,頁(yè)面中顯示
hello struts and velocity
字樣
?
四.??
參考資料
《簡(jiǎn)單
Velocity
實(shí)踐》來(lái)源于
internet
網(wǎng)絡(luò)
《
Struts
與
Velocity
的集成》來(lái)源于
internet
網(wǎng)絡(luò)
?
Velocity
模板的基本入門(mén)就到此,謝謝收看!
?