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

[color=red][size=medium]1、新建Web程序[/size][/color]


[color=red][size=medium]2、添加以下Jar包到lib下(11个)[/size][/color]
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



[color=red][size=medium]3、修改web.xml[/size][/color]
<?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>



[color=red][size=medium]4、添加struts.xml到src下,并修改[/size][/color]
<!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>


[color=red][size=medium]5、新建包:com.ape.action,新建TestAction[/size][/color]
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;
}

}


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


[color=red][size=medium]7、运行:[url]http://localhost:8080/velocity/test.action[/url][/size][/color]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值