公司需完成一個portal的快速開發,以下為筆記.所用技術為struts1.2+velocity
先完成一個簡單示例(說明相關velocity包到http://velocity.apache.org/download.cgi下載)
一.到Eclipse中new一個web工程,并修改web.xml如下
<welcome-file-list> ??<welcome-file>index.jsp</welcome-file> </welcome-file-list>
<servlet> ? ?? <servlet-name>action</servlet-name> ???? <servlet-class> ?????? org.apache.struts.action.ActionServlet ???? </servlet-class> ???? <init-param> ?????? <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>
<servlet> ? <servlet-name>velocity</servlet-name>? ? <servlet-class>? ???????? org.apache.velocity.tools.view.servlet.VelocityViewServlet ? </servlet-class>???????????????????????????????????????????
? <init-param>? ??? <param-name>org.apache.velocity.toolbox</param-name> ??? <param-value>/WEB-INF/toolbox.xml</param-value>????? </init-param>????????????????????????????????????????????????
<load-on-startup>10</load-on-startup> </servlet>
<!-- Map *.vm files to Velocity --> <servlet-mapping>? ? <servlet-name>velocity</servlet-name> ? <url-pattern>*.vm</url-pattern>????? </servlet-mapping>
|
二.到/WEB-INF目錄下放一個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> |
四.新建處理請求類
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 //這個key也就是vm頁面中的test對象的引用 ${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; } } ? |
六.新建頁面test.vm
<%@ 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()} //注意這里使用了velocity代碼,從test對象中取數據。
</body> </html> ? |
七.檢查沒有錯誤后,運行,就會在頁面上顯示出hello struts and velocity。
關于struts和velocity的配置是很容易理解的,最令初學者頭痛的是,velocity是如何取到數據的?我們在單獨使用velocity進行開發時,需要初始化,并為其設置想要返回的頁面。如下:
程序代碼
VelocityEngine ve = new VelocityEngine();
ve.init();
Template t = ve.getTemplate( "hellosite.vm" );
而當與struts一同進行開發時,這一步就可以省去了,并且我們不必在為context中賦值:
程序代碼
VelocityContext context = new VelocityContext();
context.put("test", "hello struts and velocity ");
我們只需在Struts的Action層中將值置入request或session中即能達到相同的效果。