Velocity与Struts 1.* -- VM展示
在Struts中有两种使用Velocity的方法,一种是利用Velocity的vm模板进行页面展示,一种则是利用Velocity来生成静态页面。以下介绍在Struts 1.*版本中使用Velocity的vm模板显示。
在Struts 1.*版本中,并未支持对vm模板的显示,所以当ActionForward指向一个vm模板时,只会将模板中的Velocit语句当做普通字符内容显示出来,而不对其中的Velocity语句进行任何解析及赋值。所以在Struts 1.*版本中使用Velocity,需要在web.xml中配置VelocityViewServlet,以处理后缀为.vm的模板文件。
在web.xml中进行如下配置:
<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><servlet-mapping><servlet-name>velocity</servlet-name><url-pattern>*.vm</url-pattern></servlet-mapping>
<html><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>StrutsSample_1</title></head><body><h3>StrutsSample</h3><table width="400" border="1">#foreach($name in $list)<tr> <td>$name</td> </tr>#end</table></body></html>
public class StrutsSample extends DispatchAction {public ActionForward showNameList(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) {ActionForward forward = new ActionForward();ActionMessages errors = new ActionMessages();List<String> list = new ArrayList<String>();String name = null;for(int i=0;i<20;i++){name = "Hoffman "+i+" Name";list.add(name);}request.setAttribute("list", list);if (!errors.isEmpty()) {saveErrors(request, errors);forward = new ActionForward(mapping.getInput());} else {forward = new ActionForward("/namelist.vm");}return forward;}}<struts-config><action-mappings><action path="/StrutsSample" parameter="dispatch" scope="request"type="com.mixele.velocity.struts.StrutsSample" validate="true"><forward name="namelist" path="/templates/namelist.vm" redirect="false"/></action></action-mappings></struts-config>
public void init(ServletConfig config) throws ServletException {super.init(config);initVelocity(config); //初始化VelocityinitToolbox(config); //初始化Servlet的toolbox//当Velocity初始化后,可以获取以下这些(默认内容类型、模板字符编码)defaultContentType = RuntimeSingleton.getString(CONTENT_TYPE, DEFAULT_CONTENT_TYPE);String encoding = RuntimeSingleton.getString(RuntimeSingleton.OUTPUT_ENCODING, DEFAULT_OUTPUT_ENCODING);if (!DEFAULT_OUTPUT_ENCODING.equalsIgnoreCase(encoding)) {int index = defaultContentType.lastIndexOf("charset");if (index < 0) {defaultContentType += "; charset=" + encoding;} else {Velocity.warn("VelocityViewServlet: Charset was already "+ "specified in the Content-Type property. "+ "Output encoding property will be ignored.");}}Velocity.info("VelocityViewServlet: Default content-type is: " + defaultContentType);}/** 初始化Velocity */protected void initVelocity(ServletConfig config) throws ServletException {Velocity.setApplicationAttribute(SERVLET_CONTEXT_KEY, getServletContext());// default to servletlogger, which logs to the servlet engines logVelocity.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS,ServletLogger.class.getName());// by default, load resources with webapp resource loaderVelocity.setProperty(RuntimeConstants.RESOURCE_LOADER, "webapp");Velocity.setProperty("webapp.resource.loader.class", WebappLoader.class.getName());// Try reading an overriding Velocity configurationtry {ExtendedProperties p = loadConfiguration(config); //读取配置文件(velocity.properties)Velocity.setExtendedProperties(p);} catch (Exception e) {……源码省略……}try {Velocity.init(); //now all is ready - init Velocity} catch (Exception e) {getServletContext().log("VelocityViewServlet: PANIC! unable to init() - " + e);throw new ServletException(e);}}/** 读取Velocity配置文件velocity.properties中的配置 * 若该文件不存在,使用默认配置,即org/apache/velocity/tools/view/servlet/velocity.properties */protected ExtendedProperties loadConfiguration(ServletConfig config) throws IOException {ServletContext servletContext = config.getServletContext();//获取Velocity文件的路径String propsFile = config.getInitParameter(INIT_PROPS_KEY);if (propsFile == null || propsFile.length() == 0) {propsFile = servletContext.getInitParameter(INIT_PROPS_KEY);}ExtendedProperties p = new ExtendedProperties();if (propsFile != null) {p.load(servletContext.getResourceAsStream(propsFile));Velocity.info("VelocityViewServlet: Custom Properties File: " + propsFile);} else {Velocity.info("VelocityViewServlet: No custom properties found. "+ "Using default Velocity configuration.");}return p;}/** 读取Velocity Tools配置文件toolbox.xml中的配置 * 若该文件不存在,使用Velocity Tools的默认配置 */protected void initToolbox(ServletConfig config) throws ServletException {ServletContext servletContext = config.getServletContext();/* 检查Servlet配置中关于toolbox的配置(即/WEB-INF/toolbox.xml) */String file = config.getInitParameter(TOOLBOX_KEY);System.out.println("VelocityViewServlet == initToolbox == file = "+file);/* check the servlet context for a toolbox */if (file == null || file.length() == 0) {file = servletContext.getInitParameter(TOOLBOX_KEY);}/* if we have a toolbox, get a manager for it */if (file != null) {toolboxManager = ServletToolboxManager.getInstance(servletContext, file);} else {Velocity.info("VelocityViewServlet: No toolbox entry in configuration.");}}