Struts+Velocity整合示例(含源码)

1、新建Web程序


2、添加以下Jar包到lib下(11个)
commons-collections-3.1.jar
commons-digester-2.0.jar
commons-fileupload-1.2.2.jar
commons-lang-2.5.jar
freemarker-2.3.16.jar
ognl-3.0.1.jar
oro-2.0.8.jar
struts2-core-2.2.3.1.jar
velocity-1.7.jar
velocity-tools-1.4.jar
xwork-core-2.1.6.jar




3、修改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">
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>*.action</url-pattern>
	</filter-mapping>
</web-app>



4、添加struts.xml到src下,并修改
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
	<!-- Configuration for the default package. -->
	<package
		name="default"
		extends="struts-default"
		namespace="/">
		<!-- to welPage -->
		<action
			name="test"
			class="com.ape.action.TestAction">
			<result
			type="velocity">/test.vm</result>
		</action>
	</package>
</struts>


5、新建包:com.ape.action,新建TestAction
package com.ape.action;

import com.opensymphony.xwork2.ActionSupport;

public class TestAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 9061932498262928875L;
	private String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String execute() {
		name = "Happy";
		return SUCCESS;
	}

}


6、新建test.vm到WebRoot根目录
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
${name},我啊king!


7、运行:http://localhost:8080/velocity/test.action

你可能感兴趣的:(struts2,velocity)