zoukankan      html  css  js  c++  java
  • struts2和velocity整合问题

    我的博客:www.while0.com

    我的博客:www.shishangguan.net

    以下是我第一次使用velocity的时候写的 2012-03-12

    话说struts真够懒的,都把velocity-1.6.4.jar放到他自己的lib里边了,就不给放全了,搞得新手太郁闷了。struts2想要用velocity模板需要如下几个包:

    velocity-1.7.jar
    velocity-tools-2.0.jar
    commons-collections-3.2.jar

    然后只需要在web.xml中配置result type=velocity就可以了。

    另外eclipse如果做web项目,必须把jar包引入到WEBINF目录下,如果是使用userlibraries的话,webapp容器无法识别.classpath文件中指定的classpath从而导致类未找见的异常。

    以下是2013-04-18参加工作后发现的问题:

    在实际工作中,发现不能在vm模板中使用tools.xml配置文件中定义的工具类。查了很久才发现需要在struts.xml中定义两个常量,来帮助struts寻找到tools.xml和velocity.properties的位置

    web.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
     5 
     6     <display-name>Struts Blank</display-name>
     7 
     8     <filter>
     9         <filter-name>struts2</filter-name>
    10         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    11     </filter>
    12 
    13     <filter-mapping>
    14         <filter-name>struts2</filter-name>
    15         <url-pattern>/*</url-pattern>
    16     </filter-mapping>
    17     <servlet>
    18         <servlet-name>velocity</servlet-name>
    19         <servlet-class>org.apache.velocity.tools.view.VelocityViewServlet</servlet-class> 
    20         <init-param>
    21             <param-name>org.apache.velocity.toolbox</param-name>
    22             <param-value>/WEB-INF/tools.xml</param-value>
    23         </init-param>
    24         <init-param>
    25             <param-name>org.apache.velocity.properties</param-name>
    26             <param-value>/WEB-INF/velocity.properties</param-value>
    27         </init-param>
    28     </servlet>
    29     <servlet-mapping>
    30         <servlet-name>velocity</servlet-name>
    31         <url-pattern>*.vm</url-pattern>
    32     </servlet-mapping>
    33     <welcome-file-list>
    34         <welcome-file>index.html</welcome-file>
    35     </welcome-file-list>
    36 
    37 </web-app>

    struts.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">
    
    <struts>
        <constant name="struts.velocity.toolboxlocation" value="WEB-INF/tools.xml" /> 
        <constant name="struts.velocity.configfile" value="WEB-INF/velocity.properties" />  
        <constant name="struts.enable.DynamicMethodInvocation" value="false" />
        <constant name="struts.devMode" value="true" />
    
    
        <package name="default" namespace="/" extends="struts-default">
    
            <default-action-ref name="index" />
    
            <global-results>
                <result name="error">/error.jsp</result>
            </global-results>
    
            <global-exception-mappings>
                <exception-mapping exception="java.lang.Exception" result="error"/>
            </global-exception-mappings>
    
        </package>
    
        <include file="example.xml"/>
    
        <!-- Add packages here -->
    
    </struts>
  • 相关阅读:
    self 和 super 关键字
    NSString类
    函数和对象方法的区别
    求两个数是否互质及最大公约数
    TJU Problem 1644 Reverse Text
    TJU Problem 2520 Quicksum
    TJU Problem 2101 Bullseye
    TJU Problem 2548 Celebrity jeopardy
    poj 2586 Y2K Accounting Bug
    poj 2109 Power of Cryptography
  • 原文地址:https://www.cnblogs.com/yamadie/p/2955406.html
Copyright © 2011-2022 走看看