Chinese => English
『一只小蚂蚁』的专栏
困难无其数,从来不动摇。
BlogJava
|
首页
|
发新随笔
|
发新文章
|
联系
|
聚合
|
管理
随笔:135 文章:0 评论:223 引用:0
实战struts2——Hello World
内容提要
:本文以一个“Hello World”的简单Demo,开始struts2之旅。
开发环境
:myeclipse5.0+eclipse3.2+jdk5.0+tomcat5.5+struts2+junit3.8
项目目录结构
:
其中lib目录中,包含struts2相关的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
>
7. 运行
访问
http://localhost:8080/struts2/HelloWorld.action
运行结果:
参考资料:
1.
为Struts 2.0做好准备
2.
Getting Started
发表于 2007-01-24 22:34
Thomas
阅读(2256)
评论(0)
编辑
收藏
IT新闻
新用户注册
刷新评论列表
标题
姓名
主页
验证码
*
内容(请不要发表任何与政治相关的内容)
Remember Me?
登录
使用高级评论
新用户注册
返回页首
恢复上次提交
[使用Ctrl+Enter键可以直接提交]
相关链接:
网站导航:
博客园
BlogJava
博客生活
IT博客网
C++博客
PHP博客
博客园社区
管理博客
教师博客
天文博客
汽车博客
足球博客
股票博客
电子博客
管理
<
2007年1月
>
日
一
二
三
四
五
六
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
5
6
7
8
9
10
公告
Feedjit live traffic map
常用链接
我的随笔
我的评论
我的参与
最新评论
留言簿
给我留言
查看公开留言
查看私人留言
随笔分类
(138)
Agile(3)
(rss)
AppFuse(1)
(rss)
Apusic OperaMasks(1)
(rss)
Books(1)
(rss)
Business & Finance(1)
(rss)
DDD(2)
(rss)
Design Patterns(1)
(rss)
Eclipse(4)
(rss)
EJB3.0
(rss)
Groovy & Grails
(rss)
Hibernate(3)
(rss)
Internet
(rss)
J2EE核心模式(2)
(rss)
Java ABC(4)
(rss)
Java EE(6)
(rss)
JavaScript & Ajax(2)
(rss)
JBoss Seam(5)
(rss)
JPA(1)
(rss)
JSF(2)
(rss)
Life(15)
(rss)
Open Source(3)
(rss)
Other(40)
(rss)
Portal(1)
(rss)
Pylons学习(3)
(rss)
Python(2)
(rss)
Security(2)
(rss)
Spring(3)
(rss)
TDD(3)
(rss)
Writing(1)
(rss)
一周阅读推荐(3)
(rss)
蚂蚁学模式(23)
(rss)
随笔档案
(135)
2007年11月 (9)
2007年10月 (5)
2007年9月 (10)
2007年8月 (13)
2007年7月 (8)
2007年6月 (7)
2007年5月 (10)
2007年4月 (10)
2007年3月 (4)
2007年2月 (5)
2007年1月 (12)
2006年12月 (13)
2006年11月 (7)
2006年10月 (2)
2006年9月 (15)
2006年8月 (4)
2006年7月 (1)
相册
Technology
收藏夹
(38)
Design Patterns(3)
(rss)
InfoQ(9)
(rss)
Java(1)
(rss)
JBoss Seam(6)
(rss)
My Articles(11)
(rss)
Reading(8)
(rss)
About me
my "douban" on douban
my "fanfou" on fanfou
my blog on baidu
my bookmarks on del.icio.us
my photos on picasaweb
Blogs
Anders小明的Blog
BlueDavy之技术Blog
Chris Richardson
gigix
Martin Fowler
Michael Yuan
robbin
梦想风暴
江南白衣
简单就是美!
胡长城(银狐999)BLOG
良葛格學習筆記
蛟龍居
键盘上的舞者
Classmates
碧涛居
Coworkers
一个基督徒的在世历程
English
BBC China
普特英语听力
Friends
BeanSoft
main()
刘玉辉
双鱼瓶
小献
曹江华
阿蜜果
Java
BlogJava
Blogs Summary
DevX: Java Zone
DZone
Huihoo
IBM developerWorks 中国
InfoQ
JAVA DEVELOPER'S JOURNAL
javablogs
JavaEye
javapassion.com
JavaWorld
Java站点
JBoss Seam中文站
JCP
Java Community Process
J道
Object Mentor Blog
ONJava
Raible Designs | Java
TSS
Open Source
appfuse
EasyJF
Java开源大全
JBoss Seam
OperaMasks.org
OSGi.org.cn
PicoContainer
projecttrack
Project Track - a JSF/Spring/Hibernate/Acegi example application
Redsaga
SpringSide
最新随笔
1. 【转载】你的时间在那里,成就就在那里
2. Seam能否取代Struts?
3. 【一周阅读推荐】2007.11.05—2007.11.11
4. 选择Seam的十大理由
5. 《JBoss Seam:一个深度集成框架》pdf文件下载
6. 在JBoss AS下部署Seam示例程序
7. 【翻译】Spring 2.5架构图
8. 【一周阅读推荐】2007.10.29—2007.11.04
9. JBoss Seam中文站发布
10. web2.0最全的国外API应用集合
11. 【一周阅读推荐】2007.10.22—2007.10.28
12. 记住,你的兜里只有100块!
13. 【转载】我最恐惧的事情是竞争力的丧失
14. 【翻译】RIA入门知识
15. 【翻译】比较NetBeans 6.0 M10和IntelliJ IDEA 7.0 M1
16. 【转载】Web2.0@China!
17. 【转载】正确地做事与做正确的事同样重要
18. 【转载】你必须要有一样拿得出手
19. 极限编程
20. 敏捷软件开发
搜索
积分与排名
积分 - 84834
排名 - 96
最新评论
1. re: 【翻译】比较NetBeans 6.0 M10和IntelliJ IDEA 7.0 M1[未登录]
netbeans 6.5 beta 针对键盘输入经常没响应,必须手动重启或“还原>最大化窗口”一次才行,这点用起来很不方便。
--x
2. re: 【转载】你的时间在那里,成就就在那里
评论内容较长,点击标题查看
--黃慧傑
3. re: 【转载】你的时间在那里,成就就在那里
變異狂犬病疫情
--黃慧傑
4. re: 《JBoss Seam:一个深度集成框架》pdf文件下载
8错
--***
5. re: 在JBoss AS下部署Seam示例程序
评论内容较长,点击标题查看
--someone
阅读排行榜
1. 实战struts2——用户登录(3749)
2. 《JBoss Seam:一个深度集成框架》pdf文件下载(3303)
3. 【翻译】Spring 2.5架构图(2458)
4. Heritrix使用笔记——Heritrix运行脚本(2441)
5. Hibernate经验集(2343)
评论排行榜
1. 【翻译】不使用Spring的5个理由(25)
2. J2EE without AJAX——Apusic OperaMasks框架简介(16)
3. 【翻译】Spring 2.5架构图(14)
4. 《JBoss Seam:一个深度集成框架》pdf文件下载(8)
5. 【转载】你的时间在那里,成就就在那里(8)