订阅所有JSP/Servlet的日志 订阅 | 这是最新一篇日志 上一篇 | 下一篇日志 下一篇 ]
JSP探讨

Struts入门经验

以下内容是我自己整理的一些Struts实施的入门,希望能对大家有所帮助

Struts安装:
首先请到http://jakarta.apache.org/Struts下载Struts,建议使用release版,现在最高版本为1.1,下载后得到的是一个ZIP文件。
将ZIP包解开,可以看到这个目录:lib和webapps,webapps下有一些WAR文件。假设你的Tomcat装在c:\Tomcat下,则将那些WAR文件拷贝到C:\Tomcat\webapps,重新启动Tomcat即可。打开浏览器,在地址栏中输入:http://localhost:8080/Struts-example/index.jsp,若能见到“powered by Struts”的深蓝色图标,即说明成功了。这是Struts自带的一个例子,附有详细的说明文档,可以做为初学者的入门教程。另外,Struts还提供了一系统实用对象:XML处理、通过Java reflection APIs自动处理JavaBeans属性、国际化的提示和消息等

一个实例:
一个用户注册系统,用户通过网页输入相关信息:注册ID号,密码,EMAIL,若注册成功,则返回成功提示信息,反之出现注册失败提示信息。
以下是相关文件的部分核心代码。

项目建立:
正式开发前,需要在Tocmat(我的tomcat装在c:\tomcat)中建立此项目。比较快的一种建立方式为:在C:\tomcat\webapps下新建目录test,再将C:\tomcat\webapps\struts-example下的
WEB-INF目录拷贝到test目录下,然后将test\WEB-INF下的src和classes目录清空,以及struts-config.xml文件中内容清空即可。这样,我们需要的Struts类包及相关的配置文件就都齐了。
开发时,将JSP文件放在test目录下,Java原文件放在test\WEB-INF\src下,编译后的类文件放在test\WEB-INF\classes下。

注册页面:reguser.jsp
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<%@ page contentType="text/html;charset=UTF-8" language="java" %> 
<%@ taglib uri="/WEB-INF/Struts-bean.tld" prefix="bean" %> 
<%@ taglib uri="/WEB-INF/Struts-html.tld" prefix="html" %> 
<html:html locale="true"> 
<head> 
<title>RegUser</title> 
<html:base/> 
</head> 
<body bgcolor="white"> 
<html:errors/> 
<html:form action="/regUserAction" focus="logname"> 
<table border="0" width="100%"> 
<tr> 
<th align="right"> 
Logname: 
</th> 
<td align="left"> 
<html:text property="logname" size="20" maxlength="20"/> 
</td> 
</tr> 
<tr> 
<th align="right"> 
Password: 
</th> 
<td align="left"> 
<html:password property="password" size="20" maxlength="20"/> 
</td> 
</tr> 
<tr> 
<th align="right"> 
E-mail: 
</th> 
<td align="left"> 
<html:password property="email" size="30" maxlength="50"/> 
</td> 
</tr> 
<tr> 
<td align="right"> 
<html:submit property="submit" value="Submit"/> 
</td> 
<td align="left"> 
<html:reset/> 
</td> 
</tr> 
</table> 
</html:form> 
</body> 
</html:html> 

此JSP页面不同于普通的JSP页,因为它大量运用了taglib,这些taglib对初学者而言,可能难于掌握,可这却是Struts的精华之一。灵活运用,将大大提高开发效率。

Struts-config.xml:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<Struts-config> 
<form-beans> 
<form-bean name="regUserForm" 
type="org.cjea.Struts.example. RegUserForm "/> 
</form-beans> 
<action-mappings> 
<action path="/regUserAction" 
type=" org.cjea.Struts.example.RegUserAction " 
attribute=" regUserForm " 
scope="request" 
validate="false"> 
<forward name="failure" path="/ messageFailure.jsp"/> 
<forward name="success" path="/ messageSuccess.jsp"/> 
</action> 
</action-mappings> 
</Struts-config> 

Struts的核心是Controller,即ActionServlet,而ActionServlet的核心就是Struts-config.xml,Struts-config.xml集中了所有页面的导航定义。对于大型的WEB项目,通过此配置文件即可迅速把握其脉络,这不管是对于前期的开发,还是后期的维护或升级都是大有裨益的。掌握Struts-config.xml是掌握Struts的关键所在。

FormBean:RegUserForm
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
32
33
34
35
36
37
38
39
40
41
42
43
44
package org.cjea.Struts.example; 
 
import javax.Servlet.http.HttpServletRequest; 
import org.apache.Struts.action.ActionForm; 
import org.apache.Struts.action.ActionMapping; 
 
public final class RegUserForm extends ActionForm{ 
 
private String logname; 
private String password; 
private String email; 
 
public RegUserForm(){ 
logname = null; 
password = null; 
email = null; 
} 
 
public String getLogName() { 
return this.logname; 
} 
public void setLogName(String logname) { 
this.logname = logname; 
} 
public void setPassWord(String password) { 
this.password = password; 
} 
public String getPassWord() { 
return this.password; 
} 
public void setEmail(String email) { 
this.email = email; 
} 
public String getEmail() { 
return this.email; 
} 
 
public void reset(ActionMapping mapping, HttpServletRequest request) 
{ 
logname = null; 
password = null; 
email = null; 
} 
} 

每一个FormBean 都必须继承ActionForm类,FormBean是对页面请求的封装。即把HTTP request 封装在一个对象中,需要说明的一点就是多个HTTP request可以共用一个FormBean,便于维护和重用。

ActionBean:RegUserAction
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package org.cjea.Struts.example; 
 
import javax.Servlet.http.*; 
import org.apache.Struts.action.*; 
 
public final class RegUserAction extends Action 
{ 
 
public ActionForward perform(ActionMapping mapping, 
ActionForm form, HttpServletRequest req, 
HttpServletResponse res) 
{ 
String title = req.getParameter("title"); 
String password = req.getParameter("password"); 
String email = req.getParameter("email"); 
/* 
取得用户请求,做相应数据库操作,略 
*/ 
} 
} 

FormBean的产生是为了提供数据给ActionBean,在ActionBean中可以取得FormBean中封装的数据,经相应的逻辑处理后,调用业务方法完成相应业务要求。

Servlet的演变:在常规的 JSP,Servlet,JavaBean三层结构中,JSP实现View的功能,Servlet实现Controller的功能,JavaBean实现Model的实现。

在Struts中,将常规情况下的Servlet拆分与ActionServlet、FormBean、ActionBean三个部分。ActionServlet配合Struts-config.xml,专职完成页面导航,而不再负责具体的数据获取与相应逻辑,这两部分功能由FormBean和ActionBean来完成。

Struts优缺点
优点:
Struts跟Tomcat、Turbine等诸多Apache项目一样,是开源软件,这是它的一大优点。使开发者能更深入的了解其内部实现机制。
除此之外,Struts的优点主要集中体现在两个方面:Taglib和页面导航。Taglib是Struts的标记库,灵活动用,能大大提高开发效率。另外,就目前国内的JSP开发者而言,除了使用JSP自带的常用标记外,很少开发自己的标记,或许Struts是一个很好的起点。
关于页面导航,我认为那将是今后的一个发展方向,事实上,这样做,使系统的脉络更加清晰。通过一个配置文件,即可把握整个系统各部分之间的联系,这对于后期的维护有着莫大的好处。尤其是当另一批开发者接手这个项目时,这种优势体现得更加明显。
缺点:
Taglib是Struts的一大优势,但对于初学者而言,却需要一个持续学习的过程,甚至还会打乱你网页编写的习惯,但是,当你习惯了它时,你会觉得它真的很棒。
Struts将MVC的Controller一分为三,在获得结构更加清晰的同时,也增加了系统的复杂度。
Struts从产生到现在还不到半年,但已逐步越来越多运用于商业软件。虽然它现在还有不少缺点,但它是一种非常优秀的J2EE MVC实现方式,如果你的系统准备采用J2EE MVC架构,那么,不妨考虑一下Struts。

Struts实施经验:
1、基于Struts架构的项目开发,首先需要有一个很好的整体规划,整个系统中包括哪几个模块,每个模块各需要多少FormBean和ActionBean等,而且最好有专人负责Struts-config.xml的管理。开发基于Struts的项目的难点在于配置管理,尤其是对Struts-config.xml的管理

2、如果你的项目非常紧,并且项目组中又没有富有经验的Struts开发人员,建议不要冒然采用Struts。Struts的掌握需要一个过程,对于一个熟练的JSP程序员,自学大概需要半个月左右的时间。如果结合titls,则需要更长的时间

3、如果你在网页中大量运用taglib,那么你的美工将做出部分牺牲。当你结合Tiles,功能增强的同时,这种牺牲尤为明显。当然,你对功能和美观的取舍由你自己决定

4、Taglib是一个好东西,但灵活运用它却需要一个过程,如果你不想在Taglib上花太多的时间,那么只需理解与FORM有关的几个标记,其它的标记就放着吧,以后再看,先去研究ActionServlet和Struts-config.xml,你会觉得很有成就感

5、Struts是否只适合于大型项目呢?No!Struts适合于各种大小的项目,当然,对于大型项目,它所体现出来的优势更加明显。

平均得分
(1 次评分)





文章来自: ITepub.net
标签: Struts 入门 
评论: 11 | 查看次数: 812
  • 共有 11 条评论
游客 [2008-12-29 17:15:27]
游客 [2008-12-05 13:29:25]
游客 [2008-11-05 15:17:44]
cheap wow power leveling Beyond knowing your limitations. HDRO gold it's important to be able to size up opponents lecteur mp3 at a glance. level wow Knowing their class is the first step, understanding. lord of rings online gold your places in a rock-paper-scissors scenario. lotro gold More important, however, is the estimation .lotro gold of gear. mp3 As much as skill and class
balance . mp3 mp4 player plays a part in the outcome . mp3 mp4 player of combat, gear is a major differentiator that makes up for shortcomings in other areas. mp3 mp4 player In fact, with . mp3 player kaufen the introduction of Resilience, gear more than mp4 ever plays a more substantial part in PvP. power level In an Arena match, the very first thing we . power level scope out is gear. wow level Through quick tab-selection viewing .wow leveling of character portraits, we generally wow lvl have a good idea of the classes we're up against if they keep their helm graphic on. wow lvl If they are wearing Season 3 shoulders, then we know . wow lvl 60 their relative experience. wow lvl 70 This is why the visual i.wow power leveling mpact of Arena shoulders is so important.wow powerlevel It immediately gives you a general idea of how tough the match will be. wow powerlevel Players in full S3 will likely have over 10k hp and over. wow powerleveling 400 Resilience, depending on the class and spec. A full S3 SL/SL Warlock, for example, will easily have. about 12-13k hp and over 400 Resilience. Identifying weapons is slightly more . difficult but will also give a general idea of an enemy's strength. Season 1 and 2 weapons share the same graphics, so it's harder to identify. Season 3 weapons, on the other hand, are distinctive and share models with Black Temple and Mount Hyjal weapons. which 最新免费网络游戏 have relatively the same power. A review of Brutal Gladiator weapons will come in handy because these will be the most common way to identify opponents of relative skill. With the new mechanics. in place for Arenas, Season 4 will more or less weed out the chaff from the grain.
游客 [2008-11-05 15:12:34]
4GB MP3 PLAYER My wife is a shrewd little fox. Bluetooth Headset She knows just how much I love the fact. Bluetooth Headsets that she plays the game with me so sometimes, when we have our little domestic arguments, she makes sure. cell phone accessories to cancel her WoW account just to drive home a point. des po wow Of course, it doesn't mean much since we're both paid up for the next few months, but the message is clear -- "we make up (or you see things my way). digital camcorder or I'm quitting the game!" Of course, we don't reconcile merely because I'll be losing my . digital camcorders favorite playing partner, but I have to confess that it doesn't make . dvd players me happy one bit. free online games For parents, World of Warcraft can be a . free online war games useful bargaining chip for their kids with the parental . gold wow controls feature. gold wow It's easy enough to control WoW time if kids aren't doing their homework, floundering in school, or simply not. mp3 mp4 player doing their chores. mp3 player Conversely, a friend . mp3 player accessories of mine gave his son a WoW subscription when . mp3 player accessory he did well in school. mp3 player kaufen World of Warcraft can be so. online games much fun and addicting that it's . play war games often used as a social tool, and it's often upsetting when our. po wow friends quit playing the game. portable dvd player How many of us have . wow europe had friends whose significant others have "allowed" them to. wow geld play the game after, say, a wonderful date. wow gold verkaufen I'm not sure if it only applies to me, but . wow level service because I play the game with many of my RL friends . wow leveling service and my family, I use the lure of WoW . to full effect. I once had my brother do a specific task for . the promise of an upgrade to The Burning Crusade. A little before he finished what I asked him to do, I secretly upgraded his account. so he could免费网络游戏 finally make his Blood Elf Priest. Kind of manipulative, I know, but we did end up having . a lot of fun leveling our alts together. How about you. How much a part of your life is WoW and has it .最新网游 ever been used as a bargaining . chip in your social life.
游客 [2008-11-05 15:12:22]
mp3 8GB MP3 PLAYER A couple of people . apple ipod have posted about the Shattered Sun Peacekeepers slacking off on their jobs lately. buy cheap wow gold quite possibly thanks . canon digital camera to something Blizzard fed them . cheap world of warcraft gold in PatchIn their reports, they complain about Peacekeepers. digital camera attacking them for no reason, sometimes not even in retaliation for attacking (or defending yourself against). digital cameras a member of the opposing faction. dvd player I can actually empathize with this as. eve isk I encountered the ill-placed wrath of the Peacekeepers myself when I rezzed my wife's toon in front of . ipod the Staging Area. ipod nano Without having done anything other. ipod shuffle than rezz, the Peacekeepers promptly charged . ipod touch and made short work of me. ipods In my experience, I have found 网游 that the Peacekeepers around . mp3 the Shattered Sun Staging Area have . mp3 player been slacking off. mp3 players In fact, my wife's toon was ganked right in front . mp4 of the building and the so-called Peacekeepers . portable dvd players did absolutely nothing. world of warcraft buy gold Sensing a bug, my wife . wow wrote a ticket and got a somewhat rude e-mail response saying that -- you guessed it -- it was working as intended. wow gold An Alliance . wow gold guild on my server seemed to be aware of the fact and exploited it to full effect, killing solo players who could seek no refuge under the apathetic -- or even hostile -- Peacekeepers. wow leveling According to reports, it's a known bug -- one player even . [url=http://www.gamesavo
游客 [2008-11-05 15:07:04]
2GB MP3 PLAYER Remember the WoW account hacking. 4GB MP3 PLAYER If you're a WoW player. buy wow gold you couldn't have missed . buying gold world of warcraft any of the articles posting this issue. cell phones Well, as it turns out, an article posted by . phones cell grimwell says that someone might have found the problem behind all the . cheap cell phones account hacking, stealing and selling even 5 year's worth items. cheap wow gold A piece of the BBC news article says:"Analysis [. cheap wow gold cheap wow gold cheapest wow gold ] showed that. eve isk it lay dormant on a victims . mp3 players machine until . portable mp3 player they ran World of Warcraft (WoW) at which point it captured login data and sent it to. portable mp3 players the hacking group. sell wow gold The group's enthusiastic use of the cursor flaw suggests it is trying to . world of warcraft gold do the same again. wow The online fantasy game . wow gold now has more than eight million active players around the world. wow gold Research by security firm Symantec . wow gold suggests that the raw value of a WoW account is now higher. wow gold than a credit card and its associated verification data. wow gold "Normally, as a WoW player, you'd know about the risks involved when creating an online account, yet some were . wow gold lazy enough not to check on their accounts every once in a while, thus resulting in their items . wow gold kaufen and WoW currency being stolen. wow gold kaufen Everyone knows that . stuff like that happen, some. even managed to hack the Superbowl website and use it . to host code for spyware, so monitor the hell out of your computer!In an article I wrote yesterday. about hackers seeing 游戏 console users as the new target, there is a comment 魔兽 coming from Stefana Muller.
游客 [2008-10-06 11:07:07]
游客 [2008-09-03 08:14:31]
游客 [2008-07-08 09:49:09]
Hey whats up guys New to the board.wow gold I'm in a bit of a dilema.wow gold welcome to my blog. mining and skinning as professions.world of warcraft gold I've made tons of with this guy.wow geld Enough money that I was able to buy.wow powerlevel my mount at level 40 with no problem at all.buy wow gold So now Im about to create an ALT toon and was wondering.wow which professions would compliment my.wow gold level 44 warrior and make me money at the same time.cheap wow gold One of my friends sayd I should go with disenchanting.serveur wow and blacksmithing but I don't know if there.wow europe is any money to be made in those.cheapest wow gold What do you guys think.wow power leveling Blacksmithing doesn't really pay off that well.wow powerleveling And I think that your warrior is geared quite well.gold wow so that the blacksmithing skill would have.mp3 players to be levelled quitea lot before you.mp3 player can make some nice and useful things.One crafting skill that compliments a warrior is alchemy.wow gold You can brew potions that he can consume.mp3 player and which are quite helpful in battle.zubehoer mp3 player There are potions to replenish lost health or rage.wow gold kaufen and there are battle and guardian elixirs to give you a boost.mp3 You could try enchanting to boost your warrior's gear.mp4 but then your other skill would have to be. mp4one where you can create green items (such as tailoring or leatherworking) to be disenchanted.
You can also send any useless greens you find with your warriorwow gold to your alt.but it's easier if you can also make your own.
游客 [2008-07-08 09:48:06]
Best place to start is in Bloodhoof Village in Mulgore.wow geld Go speak to Harn Longcast and buy Brilliant Smallfish him.mp3 players Now start fishing in Stonebull Lake.mp3 player Equip your rod and then apply the shiny bauble lure to it.wow level service as this will make catching fish easier.You'll want to catch about 60 Brilliant minutes.mp3 players You'll find that by the time you have Smallfish.mp3 player you'll also have about 30 Longjaw Mud Snappers.wow Once you have 60.mp3 players cook them.mp3 player You can own fire or jog back.wow gold to Harn Longcast and use the fire in front of him.wow schnell gold Cook the Brilliant Smallfish.gold für wow then at level 50 start cooking Longja.wow gold paypal Mud Snappers (after learning the recipe you bought earlier).You'll need to go and catch Longjaw.gold in wow Mud Snappers now and the place for these is.wow gold 1000 the pond in Orgrimmar by Lumark the Fishing trainer.wow power leveling (I know you've been catching quite a few where you are.wowgold but the "drop rate" is a lot better in Orgrimmar).Between levels 50 and 75 go and cooking.wowgold You'll now need to catch about 30 Longjaw 100.world of warcraft power leveling Now go to Mill in Hillsbrad and buy Bristle Whisker.wow golds Catfish recipe off of Derak Nightfall.cheapest wow gold Go to Mill and fish for Bristle Whisker Catfish.wow lvl Depending on your cooking level.world of warcraft power leveling Between levels 125 expert cooking from trainer.buy mp3 players You'll need and buy a book for your Fishing level.
Speak tobuy the book Expert Fishing: The Bass and You.Once your up to 175 cooking, go to Shadowprey Village in Desolace.
游客 [2008-07-08 09:47:07]
At this stage, you've got some unzipped folders sitting in front of you.level wow it's time to find them a home.buy mp3 player You'll need to locate your World of Warcraft folder.best mp3 player I'll detail how this is done for a default installation.wow lvl using the default file structure.1BG MP3 PLAYER Hopefully if you know enough to change the defaults.2BG MP3 PLAYER you won't need this guide.wow powerlevel For Mac users.mp3 player kaufen you will need to open a new Finder window, open up your Applications folder.baladeurs MP3 then open your World of Warcraft folder.buying gold world of warcraft For PC users, you'll need to open the My Computer folder from your Start Menu.mp3 mp4 From there.mp3 mp4 player open the hard disk that you have WoW installed on.wow level (Default is C:) Next, open the Program Files folder.mp3 player kaufen then locate your World of Warcraft folder.Now that you have opened the World of Warcraft folder.mp3 player kaufen you should see a folder named Interface.po wow Open up this folder and you will find the Addons folder.mp3 player kaufen This is the new home of your addons.wow lvl 60 This step is where some addons require some extra attention.wow lvl 70 Some addons.wow powerleveling like Atlas and Cartographer.wow gold come with multiple modules that work together.lotro gold but are located in different folders within the unzipped directory.lotro po Open up your unzipped addon folder and look for files with the .lua .toc file extension.wow levelExample: Atlas.lua Atlas.toc) Once you locate a file with the .toc extension. you can take the folder containing that file and move it into the Addons folder.
  • 共有 11 条评论
发表评论
昵 称:  登录
内 容:
选 项:
字数限制 1000 字 | UBB代码 开启 | [img]标签 开启