struts配置velocity-tools不能使用tools.xml中配置的工具类
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成功的前辈吗?帮我看看问题在什么地方。谢谢
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
怎么不能往编辑器复制图片了呢
jar包
jar包