velocity和struts集成
公司需完成一個(gè)portal的快速開(kāi)發(fā),以下為筆記.所用技術(shù)為struts1.2+velocity
先完成一個(gè)簡(jiǎn)單示例(說(shuō)明相關(guān)velocity包到http://velocity.apache.org/download.cgi下載)
一.到Eclipse中new一個(gè)web工程,并修改web.xml如下
<welcome-file-list> ? ?? <servlet-name>action</servlet-name> ???? <servlet-class> ?????? org.apache.struts.action.ActionServlet ???? </servlet-class> ?????? <param-name>debug</param-name> ?????? <param-value>2</param-value> ???? </init-param> ???? <init-param> ?????? <param-name>detail</param-name> ?????? <param-value>2</param-value> ???? </init-param> ???? <init-param> ?????? <param-name>validate</param-name> ?????? <param-value>true</param-value> ???? </init-param> ???? <load-on-startup>2</load-on-startup> ? </servlet> ? <servlet-mapping> ? ?? <servlet-name>action</servlet-name> ? ?? <url-pattern>*.do</url-pattern> ? </servlet-mapping> |
二.到/WEB-INF目錄下放一個(gè)toolbox.xml文件
<?xml version="1.0"?> <toolbox> ? <tool> ???? <key>link</key> ???? <scope>request</scope> ???? <class> ?????? org.apache.velocity.tools.struts.StrutsLinkTool ???? </class> ? </tool> ? <tool> ???? <key>msg</key> ???? <scope>request</scope> ???? <class> ?????? org.apache.velocity.tools.struts.MessageTool ???? </class> ? </tool> ? <tool> ???? <key>errors</key> ???? <scope>request</scope> ???? <class> ?????? org.apache.velocity.tools.struts.ErrorsTool ???? </class> ? </tool> ? <tool> ???? <key>form</key> ???? <scope>request</scope> ???? <class> ?????? org.apache.velocity.tools.struts.FormTool ???? </class> ? </tool> ? <tool> ???? <key>tiles</key> ???? <scope>request</scope> ???? <class> ?????? org.apache.velocity.tools.struts.TilesTool ???? </class> ? </tool> ? <tool> ???? <key>validator</key> ???? <scope>request</scope> ???? <class> ?????? org.apache.velocity.tools.struts.ValidatorTool ???? </class> ? </tool> </toolbox> |
三.修改WEB-INF目錄下面的struts-config.xml文件為:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd"> <struts-config> <form-beans> <form-bean name="TestForm" type="org.junesky.form.TestForm"/> </form-beans> <action-mappings > <action name="TestForm" input="/error.jsp" scope="request" path="/test" validate="false" type="org.junesky.action.TestAction" > <forward name="success" path="/test.vm" /> </action-mappings> </struts-config> |
四.新建處理請(qǐng)求類
public class TestAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res) { TestForm cform = (TestForm)form; cform.setTest("hello struts and velocity"); //注意這里,將form置入request中,它的key是test //這個(gè)key也就是vm頁(yè)面中的test對(duì)象的引用 ${test.getTest()} req.setAttribute("test", cform); return mapping.findForward("success"); } } |
五.新建from類
public class TestForm extends ActionForm { private String test; public String getTest() { return test; } public void setTest(String test) { this.test = test; } } |
<%@ 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>
</body> </html> |
七.檢查沒(méi)有錯(cuò)誤后,運(yùn)行,就會(huì)在頁(yè)面上顯示出hello struts and velocity。
關(guān)于struts和velocity的配置是很容易理解的,最令初學(xué)者頭痛的是,velocity是如何取到數(shù)據(jù)的?我們?cè)趩为?dú)使用velocity進(jìn)行開(kāi)發(fā)時(shí),需要初始化,并為其設(shè)置想要返回的頁(yè)面。如下:
程序代碼
VelocityEngine ve = new VelocityEngine();
ve.init();
Template t = ve.getTemplate( "hellosite.vm" );
而當(dāng)與struts一同進(jìn)行開(kāi)發(fā)時(shí),這一步就可以省去了,并且我們不必在為context中賦值:
程序代碼
VelocityContext context = new VelocityContext();
context.put("test", "hello struts and velocity ");
我們只需在Struts的Action層中將值置入request或session中即能達(dá)到相同的效果。
posted on 2009-03-28 23:11 tobyxiong 閱讀(417) 評(píng)論(0) 編輯 收藏 所屬分類: java