速动画教程第二十八集
				Struts+Velocity
				简单集成
		
		
				
						
						下载请到:http://this.oksonic.cn  讨论请到 http://www.oksonic.com.cn  注册用户后再转到论坛
 
				
		
		
				
						
								一.   
						
				
				
						开发环境:
						
								
								
						
				
		
		
				
						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/
		
		
				
						 
				
		
		
				
						
								二.   
						
				
				
						开发步骤
						
								
								
						
				
		
		
				
						1.       
				
				新建一个
				web
				项目
				vm
		
		
				
						2.       
				
				添加
				struts
				框架到项目中,使用
				struts1.2
		
		
				
						3.       
				
				拷贝
				Velocity
				包中的
				velocity-1.4.jar
				、
				velocity-tools-view-1.3.jar
				、
				commons-collections-3.2.jar
				、
				velocity-tools-1.3.jar
				文件到项目的
				lib
				目录下,并刷新项目以载入包
				
						
						
				
		
		
				
						4.       
				
				修改
				web.xml 
				文件让它识别
				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.       
				
				创建一个
				test
				结构(
				test.jsp
				、
				testForm.java
				、
				testAction.java
				)
				
						
						
				
		
		
				
						6.       
				
				修改
				struts
				配置文件,加入导航配置,跳专到
				test.vm
				文件,内容如下:
				
						
						
				
		
		
				<
				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;
				
						
						
				
		
		
				
						       
				
				// 
				对模型中的
				test
				属性负值
				
						
						
				
		
		
				
						       testForm.setTest(
				"hello struts and velocity"
				);
				
						
						
				
		
		
				
						       
				
				// 
				将
				form
				对像存放到
				request
				对像中
				
						
						
				
		
		
				
						       request.setAttribute(
				"test"
				, testForm);
				
						
						
				
		
		
				
						       
				
				// 
				调用导航配置进行跳转
				
						
						
				
		
		
				
						       
				
				
						return
				
				 mapping.findForward(
				"success"
				);
				
						
						
				
		
		
				
						 
				
		
		
				
						    }
		
		
				
						 
				
		
		
				
						8.       
				
				根据
				struts-config.xml
				文件中的
				<
				forward 
				name
				=
				"success" 
				path
				=
				"/test.vm" 
				/>
				配置内容,需要在
				webroot
				目录中新建一个
				test.vm
				文件此文件为
				Velocity
				模板文件,文件内容如下:
				
						
						
				
		
		
				<%@
				
				
				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
				对像中的
				test
				属性值
				
						
						
				
		
		
				
						 
				
		
		
				
						
								三.   
						
				
				
						测试
						
								
								
						
				
		
		
				部署项目
		
		
				在地址栏中输入
				
						http://localhost/vm/test.do
				
				,页面中显示
				hello struts and velocity
				字样
		
		
				
						 
				
		
		
				
						
								四.   
						
				
				
						参考资料
						
								
								
						
				
		
		
				《简单
				Velocity
				实践》来源于
				internet
				网络
		
		
				《
				Struts 
				与
				 Velocity 
				的集成》来源于
				internet
				网络
		
		
				
						 
				
		
		
				Velocity
				模板的基本入门就到此,谢谢收看!