struts配置velocity-tools不能使用tools.xml中配置的工具类

money1991 发布于 2013/04/18 14:07
阅读 1K+
收藏 0

struts2集成velocity模版,不能使用tools.xml中配置的工具变量。以下是我的详细配置:

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.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>

       <package name="example" namespace="/example" extends="default">

        <action name="HelloWorld" class="example.HelloWorld">
            <result type="velocity">/index.vm</result>
        </action>
    </package>

    <!-- Add packages here -->

</struts>

velocity.properties

input.encoding=ISO-8859-1
output.encoding=ISO-8859-1

tools.xml

<?xml version="1.0" encoding="UTF-8"?>
<toolbox>
<!--     <tool> -->
<!--         <key>tiles</key> -->
<!--         <scope>request</scope> -->
<!--         <class>org.apache.velocity.tools.struts.TilesTool</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>date</key>
        <scope>application</scope>
        <class>org.apache.velocity.tools.generic.DateTool</class>
        <parameter name="format" value="yyyy-MM-dd"/>
    </tool>
    <tool>
        <key>list</key>
        <scope>application</scope>
        <class>org.apache.velocity.tools.generic.ListTool</class>
    </tool>
    <tool>
        <key>sort</key>
        <scope>application</scope>
        <class>org.apache.velocity.tools.generic.SortTool</class>
    </tool>
    <tool>
        <key>number</key>
        <scope>application</scope>
        <class>org.apache.velocity.tools.generic.NumberTool</class>
    </tool>
    <tool>
        <key>alternator</key>
        <scope>application</scope>
        <class>org.apache.velocity.tools.generic.AlternatorTool</class>
        <parameter name="auto-alternate" value="true"/>
    </tool>
    <tool>
        <key>esc</key>
        <scope>application</scope>
        <class>org.apache.velocity.tools.generic.EscapeTool</class>
    </tool>
</toolbox>
web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

	<display-name>Struts Blank</display-name>

	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>

	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<servlet>
		<servlet-name>velocity</servlet-name>
		<servlet-class>org.apache.velocity.tools.view.VelocityViewServlet</servlet-class> 
		<init-param>
			<param-name>org.apache.velocity.toolbox</param-name>
			<param-value>/WEB-INF/tools.xml</param-value>
		</init-param>
		<init-param>
			<param-name>org.apache.velocity.properties</param-name>
			<param-value>/WEB-INF/velocity.properties</param-value>
		</init-param>
	</servlet>
	<servlet-mapping>
		<servlet-name>velocity</servlet-name>
		<url-pattern>*.vm</url-pattern>
	</servlet-mapping>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
	</welcome-file-list>

</web-app>

index.vm

$date
如果输出一个日期就说明velocity tools.xml中配置的date工具生效了

以下是lib的jar包

以下是文件顺序:

当我运行 http://localhost:8080/struts2-blank/index.vm,直接访问vm文件时可以出现

而访问http://localhost:8080/struts2-blank/example/HelloWorld.action时,虽然给action配置的视图也是index.vm,但此时没成功解析

有哪位成功配置struts2+velocity1.7+velocity-tools2.0成功的前辈吗?帮我看看问题在什么地方。谢谢

加载中
0
money1991
money1991
闹好了,struts2中声明velocity模版要在struts.xml文件中声明struts.velocity.toolboxlocation和struts.velocity.configfile,而不用在web.xml中声明velocityfilter,这样就可以使用他的工具了
0
money1991
money1991
怎么不能往编辑器复制图片了呢
返回顶部
顶部