公司需完成一个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中即能达到相同的效果。