內(nèi)容提要:本文以一個“Hello World”的簡單Demo,開始struts2之旅。
開發(fā)環(huán)境:myeclipse5.0+eclipse3.2+jdk5.0+tomcat5.5+struts2+junit3.8
項目目錄結(jié)構(gòu):


其中l(wèi)ib目錄中,包含struts2相關(guān)的jar文件。本項目中,為簡單起見,選用了struts-2.0.1\lib中的所有jar文件(注意:不需要包含struts2-all-2.0.1.jar)。
項目文件:
1. Action類
com.cleversoft.struts2.demo.HelloWorld.java:
package?com.cleversoft.struts2.demo;

import?com.opensymphony.xwork2.ActionSupport;


public?class?HelloWorld?extends?ActionSupport?
{


????/**?*//**
?????*?
?????*/
????private?static?final?long?serialVersionUID?=?-758686623642845372L;

????public?static?final?String?MESSAGE?=?"Hello,?Struts2!";


????public?String?execute()?throws?Exception?
{
????????setMessage(MESSAGE);
????????return?SUCCESS;
????}

????private?String?message;


????public?void?setMessage(String?message)?
{
????????this.message?=?message;
????}


????public?String?getMessage()?
{
????????return?message;
????}
}
2. View頁面
HelloWorld.jsp:

<%
@?taglib?prefix="s"?uri="/struts-tags"%>

<html>
????<head>
????????<title>Hello?World!</title>
????</head>
????<body>
????????<h2>
????????????<s:property?value="message"?/>
????????</h2>
????</body>
</html>
3. 配置Action
struts.xml:
<!DOCTYPE?struts?PUBLIC
????"-//Apache?Software?Foundation//DTD?Struts?Configuration?2.0//EN"
????"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
????<package?name="com.cleversoft.struts2.demo"?extends="struts-default">
????????<action?name="HelloWorld"?class="com.cleversoft.struts2.demo.HelloWorld">
????????????<result>/HelloWorld.jsp</result>
????????</action>
????????<!--?Add?your?actions?here?-->
????</package>
</struts>
struts.properties(可選):
struts.devMode?=?true
struts.enable.DynamicMethodInvocation?=?false
4. web配置
web.xml:
<?xml?version="1.0"?encoding="ISO-8859-1"?>
<!DOCTYPE?web-app?PUBLIC?"-//Sun?Microsystems,?Inc.//DTD?Web?Application?2.3//EN"?"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
????<display-name>Struts?2.0?Demo</display-name>
????<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>/*</url-pattern>
????</filter-mapping>
????<welcome-file-list>
????????<welcome-file>index.html</welcome-file>
????</welcome-file-list>
</web-app>
5. 測試類
com.cleversoft.struts2.test.HelloWorldTest.java:
package?com.cleversoft.struts2.test;

import?junit.framework.TestCase;
import?com.opensymphony.xwork2.ActionSupport;
import?com.cleversoft.struts2.demo.*;


public?class?HelloWorldTest?extends?TestCase?
{

????public?void?testHelloWorld()?throws?Exception?
{

????????HelloWorld?hello_world?=?new?HelloWorld();
????????String?result?=?hello_world.execute();

????????assertTrue("Expected?a?success?result!",?ActionSupport.SUCCESS
????????????????.equals(result));

????????assertTrue("Expected?the?default?message!",?HelloWorld.MESSAGE
????????????????.equals(hello_world.getMessage()));

????}
}
6. 其他
index.html:
<!DOCTYPE?HTML?PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN">
<html>
??<head>
????<title>index.html</title>
????
????<meta?http-equiv="keywords"?content="keyword1,keyword2,keyword3">
????<meta?http-equiv="description"?content="this?is?my?page">
????<meta?http-equiv="content-type"?content="text/html;?charset=GB18030">
????
????<!--<link?rel="stylesheet"?type="text/css"?href="./styles.css">-->

??</head>
??
??<body>
????This?is?Struts?2.0?Demo?page.?<br>
??</body>
</html>
開發(fā)環(huán)境:myeclipse5.0+eclipse3.2+jdk5.0+tomcat5.5+struts2+junit3.8
項目目錄結(jié)構(gòu):


其中l(wèi)ib目錄中,包含struts2相關(guān)的jar文件。本項目中,為簡單起見,選用了struts-2.0.1\lib中的所有jar文件(注意:不需要包含struts2-all-2.0.1.jar)。
項目文件:
1. Action類
com.cleversoft.struts2.demo.HelloWorld.java:





































2. View頁面
HelloWorld.jsp:














3. 配置Action
struts.xml:











struts.properties(可選):


4. web配置
web.xml:


















5. 測試類
com.cleversoft.struts2.test.HelloWorldTest.java:
























6. 其他
index.html:

















7. 運行
訪問http://localhost:8080/struts2/HelloWorld.action
運行結(jié)果:
參考資料:
1. 為Struts 2.0做好準(zhǔn)備
2. Getting Started
|
|
歡迎大家訪問我的個人網(wǎng)站 萌萌的IT人
* 正在學(xué)習(xí):陷阱技(等級10)
* 剩余時間:571:31:50 完成時間:11-09 08:23
* 取消