web.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<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.servlet.VelocityViewServlet
</servlet-class>
<init-param>
<param-name>org.apache.velocity.toolbox</param-name>
<param-value>/WEB-INF/toolbox.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>
<load-on-startup>10</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>velocity</servlet-name>
<url-pattern>*.vm</url-pattern>
</servlet-mapping>
</web-app>
struts.xml文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<constant name="struts.devMode" value="false"/>
<constant name="struts.configuration.xml.reload" value="true"/>
<package namespace="/" name="" extends="struts-default">
<action name="velocity" class="com.sanjiawu.action.VelocityAction">
<result name="success">HelloVelocity.vm</result>
</action>
</package>
</struts>
velocity.properties文件:
input.encoding=UTF-8
output.encoding=UTF-8
file.resource.loader.path=D\:\\myworkspace\\struts\\WebRoot
webapp.root.url=http://192.168.12.48:8080
webapp.template.path=D\:\\myworkspace\\struts\\WebRoot
个人认为:
velocity.properties文件配置最重要,路径要配对。