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>

 

 

转载于:https://www.cnblogs.com/yamadie/archive/2013/03/12/2955406.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值