Chinaunix
标题:
struts傻瓜式学习(一天篇)
[打印本页]
作者:
cuichangdashi
时间:
2006-02-10 15:13
标题:
struts傻瓜式学习(一天篇)
中国IT动力,最新最全的IT技术教程
最新100篇
|
推荐100篇
|
专题100篇
|
排行榜
|
搜索
|
在线API文档
首 页
|
程序开发
|
操作系统
|
软件应用
|
图形图象
|
网络应用
|
精文荟萃
|
教育认证
|
未整理篇
|
技术讨论
ASP
JS
PHP
工程
ASP.NET
网站建设
UML
J2EE
SUN
.NET
VC
VB
VFP
网络维护
数据库
DB2
SQL2000
Oracle
Mysql
服务器
Win2000
Office
C
DreamWeaver
FireWorks
Flash
PhotoShop
上网宝典
CorelDraw
协议大全
网络安全
微软认证
硬件维护
CPU
主板
硬盘
内存
显卡
显示器
键盘鼠标
声卡音箱
打印机
机箱电源
BIOS
网卡
C#
Java
Delphi
vs.net2005
当前位置: >
程序开发
>
编程语言
>
Java
>
Struts/Hibernate
struts傻瓜式学习(一天篇)
作者:
未知
时间:
2005-07-24 21:22
出处:
JR
责编:
chinaitpower
摘要:struts傻瓜式学习(一天篇)
作者:林佩雯
或许有人觉得struts不容易学,似乎里面的一些概念让未接触过的人迷惑,M
VC
1、M
VC
2、模式……我写这篇文章是想让从来没有接触过struts的人,能有个简单的入门指引,当然,
系统
地学习struts是必要的,里面有很多让人心醉的东东,那是后话了。
该案例包括首页,用户登陆、网站向导页面。就这么简单,没有深奥的struts概念,主要靠动手,然后用心体会~!@#$%^&
WEB Server用tomcat4。到
http://jakarta.apache.org
下载struts1.1,把zip
文件
释放到c:\struts,拷贝C:\struts\webapps\struts-example.war到c:\tomcat4\webapps中,启动tomcat,war包被释放为struts-example
文件
夹,删除war包,把struts-example
文件
夹更名为test。
一、把WEB-INF\web.xml改成:
action
org.apache.struts.action.ActionServlet
config
/WEB-INF/struts-config.xml
1
action
*.cool
index.jsp
二、把test\WEB-INF\ struts-config.xml改成:
三、增加一个FormBean,类路径为test.UserForm,以下是这个类的内容:
package
test;
import
org.apache.struts.action.ActionForm;
public
class
UserForm
extends
ActionForm
{
private
[url= target=_blank http: www.chinaitpower.com Dev Programme Java index.html?>
java
/lang/String.
[url=]
java
[/url]
.html]
String
[/url]
name=
"lpw"
;
//用户名
private
[url= target=_blank http: www.chinaitpower.com Dev Programme Java index.html?>
java
/lang/String.
[url=]
java
[/url]
.html]
String
[/url]
ps=
"1111"
;
//密码
public
UserForm(){}
public
void
setName(
[url= target=_blank http: www.chinaitpower.com Dev Programme Java index.html?>
java
/lang/String.
[url=]
java
[/url]
.html]
String
[/url]
s) {name=s;}
public
[url= target=_blank http: www.chinaitpower.com Dev Programme Java index.html?>
java
/lang/String.
[url=]
java
[/url]
.html]
String
[/url]
getName() {
return
name;}
public
void
setPs(
[url= target=_blank http: www.chinaitpower.com Dev Programme Java index.html?>
java
/lang/String.
[url=]
java
[/url]
.html]
String
[/url]
s) {ps=s;}
public
[url= target=_blank http: www.chinaitpower.com Dev Programme Java index.html?>
java
/lang/String.
[url=]
java
[/url]
.html]
String
[/url]
getPs() {
return
ps;}
}
四、增加一个Action的子类,类路径为test. RegistAction,以下是这个类的内容:
package
test;
import
java
.lang.reflect.
[url= target=_blank http: www.chinaitpower.com Dev Programme Java index.html?>
java
/lang/reflect/InvocationTargetException.
[url=]
java
[/url]
.html]
InvocationTargetException
[/url]
;
import
java
.util.
[url= target=_blank http: www.chinaitpower.com Dev Programme Java index.html?>
java
/util/Locale.
[url=]
java
[/url]
.html]
Locale
[/url]
;
import
java
x.servlet.
[url= target=_blank http: www.chinaitpower.com Dev Programme Java index.html?>
java
x/servlet/ServletException.
[url=]
java
[/url]
.html]
ServletException
[/url]
;
import
java
x.servlet.http.
[url= target=_blank http: www.chinaitpower.com Dev Programme Java index.html?>
java
x/servlet/http/HttpServletRequest.
[url=]
java
[/url]
.html]
HttpServletRequest
[/url]
;
import
java
x.servlet.http.
[url= target=_blank http: www.chinaitpower.com Dev Programme Java index.html?>
java
x/servlet/http/HttpSession.
[url=]
java
[/url]
.html]
HttpSession
[/url]
;
import
java
x.servlet.http.
[url= target=_blank http: www.chinaitpower.com Dev Programme Java index.html?>
java
x/servlet/http/HttpServletResponse.
[url=]
java
[/url]
.html]
HttpServletResponse
[/url]
;
import
org.apache.struts.action.
Action
;
import
org.apache.struts.action.ActionError;
import
org.apache.struts.action.ActionErrors;
import
org.apache.struts.action.ActionForm;
import
org.apache.struts.action.ActionForward;
import
org.apache.struts.action.ActionMapping;
import
org.apache.struts.util.MessageResources;
import
test.UserForm;
public
final
class
RegistAction
extends
Action
{
public
ActionForward execute(ActionMapping mapping,ActionForm form,
[url= target=_blank http: www.chinaitpower.com Dev Programme Java index.html?>
java
x/servlet/http/HttpServletRequest.
[url=]
java
[/url]
.html]
HttpServletRequest
[/url]
request,
[url= target=_blank http: www.chinaitpower.com Dev Programme Java index.html?>
java
x/servlet/http/HttpServletResponse.
[url=]
java
[/url]
.html]
HttpServletResponse
[/url]
response)
throws
[url= target=_blank http: www.chinaitpower.com Dev Programme Java index.html?>
java
/lang/Exception.
[url=]
java
[/url]
.html]
Exception
[/url]
{
[url= target=_blank http: www.chinaitpower.com Dev Programme Java index.html?>
java
/util/Locale.
[url=]
java
[/url]
.html]
Locale
[/url]
locale = getLocale(request);
MessageResources messages = getResources(request);
[url= target=_blank http: www.chinaitpower.com Dev Programme Java index.html?>
java
x/servlet/http/HttpSession.
[url=]
java
[/url]
.html]
HttpSession
[/url]
session = request.getSession();
UserForm userform = (UserForm) form;
//此处可以调用其他类来执行
数据库
写入或其他逻辑判断
// 如果UserForm传来的参数name的值为默认的lpw,将forward到failed,
// 该名称将到struts-config.xml的中寻找映射的url地址
// (可以是绝对路径,也可以是相对路径),对于本例,是转到failed.cool,
// 还记得吗?后缀为cool的请求全部到action-mappings中寻找
// 对应的action处理,最终目录是wuwu.jsp*/
if
(
"lpw"
.equals(userform.getName()) )
return
(mapping.findForward(
"failed"
));
else
return
(mapping.findForward(
"regist"
));
}
}
五、以下所有新增或修改的页面相当于struts的View部分,把首页index.jsp改成:
站点导航
用户:
密码:
六、增加hello.jsp,用于站点导航:
site mapThe following is content filling by reader
七、增加wuwu.jsp,当没有新用户登陆时,将转到这个页面:
现有用户:
密码:
没有得到新的用户!55555~~~~~
八、增加regist.jsp,当有新用户登陆时,将转到这个页面:
新用户帐号:
密码:
九、启动tomcat4,浏览器中键入http://localhost:8080/test/index.jsp,操作一下,就可以看到结果,并初步理解struts的M、V、C各部分的协同工作原理,当然这是作者的良好意愿,如果读者看得一头雾水,欢迎指出错误在哪里 :)
[ 收藏此页到
[url=javascript:d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(vivi=window.open('http://vivi.sina.com.cn/collect/icollect.php?pid=http://www.newsunday.com&title='+escape(d.title)+'&url='+escape(d.location.href)+'&desc='+escape(t),'vivi','scrollbars=no,width=480,height=480,left=75,top=20,status=no,resizable=yes'));vivi.focus();]
VIVI
[/url]
|
[url=javascript:d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(keyit=window.open('http://www.younote.com/Noteit.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit', 'scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes')); keyit.focus();]
Younote
[/url]
|
[url=javascript:d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(keyit=window.open('http://www.365key.com/storeit.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes'));keyit.focus();]
365Key
[/url]
|
[url=javascript:d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(keyit=window.open('http://blogmark.blogchina.com/jsp/key/quickaddkey.jsp?k='+encodeURI(d.title)+'&u='+encodeURI(d.location.href)+'&c='+encodeURI(t),'keyit','scrollbars=no,width=500,height=430,status=no,resizable=yes'));keyit.focus();]
Blogchina
[/url]
|
[url=javascript:d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(keyit=window.open('http://my.poco.cn/fav/storeIt.php?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes'));keyit.focus();]
POCO
[/url]
|
[url=javascript:d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(rrwz=window.open('http://rrwz.qihoo.com/user/AddWebSnip.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,width=475,height=450,left=280,top=50,status=no,resizable=yes'));keyit.focus();]
人人
[/url]
|
[url=javascript:d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(websnip=window.open('http://x.yeeyoo.com/MouseAdd.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'yeeyoo','scrollbars=no,width=475,height=450,left=280,top=50,status=no,resizable=yes'));websnip.focus();;]
亿友
[/url]
|
[url=javascript:d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(wozhai=window.open('http://www.wozhai.com/wozhai/Cento.asp#t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'wozhai','scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes'));wozhai.focus();]
我摘
[/url]
|
[url=javascript:t=document.title;u=location.href;e=document.selection?(document.selection.type!='None'?document.selection.createRange().text:''):(document.getSelection?document.getSelection():'');void(open('http://bookmark.hexun.com/post.aspx?title='+escape(t)+'&url='+escape(u)+'&excerpt='+escape(e),'HexunBookmark','scrollbars=no,width=600,height=450,left=80,top=80,status=no,resizable=yes'));wozhai.focus();]
和讯
[/url]
|
[url=javascript:d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(keyit=window.open('http://www.ok4545.com/club/myweb_add.asp?title='+escape(d.title)+'&url='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,width=800,height=575,left=75,top=20,status=no,resizable=yes'));keyit.focus();]
拇指
[/url]
|
[url=javascript:d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(yesky=window.open('http://hot.yesky.com/dp.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t)+'&st=2','yesky','scrollbars=no,width=400,height=480,left=75,top=20,status=no,resizable=yes'));yesky.focus();]
天极
[/url]
]
首页
|
投资与合作
|
服务条款
|
隐私政策
|
[url=javascript:window.external.AddFavorite('http://www.chinaitpower.com/',%20'“中国IT动力,提供最新最全的电脑技术教程”"/www.chinaitpower.com')]
收藏本站
[/url]
|
设为首页
|
新用户注册
|
免责声明
|
使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有
本文来自ChinaUnix博客,如果查看原文请点:
http://blog.chinaunix.net/u/910/showart_73204.html
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2