Struts Tutorial Passo A Passo 阳飞文档格式.docx
- 文档编号:453356
- 上传时间:2023-04-28
- 格式:DOCX
- 页数:52
- 大小:233.96KB
Struts Tutorial Passo A Passo 阳飞文档格式.docx
《Struts Tutorial Passo A Passo 阳飞文档格式.docx》由会员分享,可在线阅读,更多相关《Struts Tutorial Passo A Passo 阳飞文档格式.docx(52页珍藏版)》请在冰点文库上搜索。
IntroducingtheActionForm
VII.AbetterwaytoseparateBookandBookForm
VIII.HandlingInvalidEntries
IX.Recapitulation:
Whereweare
X.UsingLogic
XI.Templates
XII.FAQ
XIII.BeanstoXML
XIV.ReplacingJSPwithXSLT
XV.IntroducingAspectJ
XVI.AntforCompilationandDistribution
XVII.Conclusions
XVIII.Epilogue:
TechnicalBackgroundaboutthisDocument
XIX.AbouttheAuthor
1.Introduction
WhyIwrotethetutorial,whoisthetargetgroupandanoverviewofthecontents.
Goals:
Togetanideawhatthistutorialisaboutandwhetheryoushouldbeabletolearnsomethingnew.
DuringthistutorialIwilldevelopalittleapplication,stepbystep,thatimplementsApacheStruts.
YoushouldhavesomeexperiencewithJavaServerpages,JSPandXML.Furtheryoushouldhaveaserverimplementedthatcanrunthosethings.Seemyhomepageforatutorialaboutthat,ifnot.
ThetutorialwillstartwiththeinstallationoftheStrutsframeworkitself,willimplementsomeminimalisticfeaturesinpureHTMLandwillthenaddsomeStrutsfuntionality,givinghintsandthoughtsabouthowtoorganizeyourcode.FinallyI'
llevenshowyousometrickswithAspectJtomakeyourlifeasaprogrammersomewhateasier.
Thistutorialisunderconstantdevelopment,socheckbackfromtimetotime.Alsotakealookatthechapterabouttechnicalbackgrounds.Itmightinterestyou.
2.InstallationofStruts
Thisoneisquiteeasy.Itakeit,youhavearunningTomcat3.2(orcomparableserver)andknowhowtohandleit?
Illustration1:
Thefolderstructure
1.Generateadirectorystructurelikeinthescreenshot(adaptifyoudon'
tuseTomcat)
2.Downloadthenewest(binary)versionofStruts(IUse1.0.2)[I].
3.Extractthedownload.
4.CopytheStruts.jar
5.Pasteittothelibdirectoryofyourapplication(seeabove).DON'
T,IrepeatDON'
TjustaddittoyourCLASSPATH!
Ididthatanditworkedfine.UntilIrestartettheTomcat,thatis.Tookmequiteawhiletofiguretheconnectionout.Atleastwithmysystemtherewasabigproblem,soIrecommendtoadditjusttothisapplication.
6.Thatisall,forthemoment.Don'
tdeletetherestoftheStrutsdownload,though.Wewillneedmorelateron.
3.TheFirstTry:
WewillstartwithasimpleJSPpage,justtoensurethatwedideverythingrightuntilnow.CreateafilecalledBookView.jspinthe'
strutsShop'
-directory.
FirstJSP
<
%@pagelanguage="
java"
%>
html>
head>
title>
StrutsTutorial:
BookView<
/title>
/head>
bodybgcolor="
white"
>
h2>
BookView<
/h2>
/body>
/html:
Illustration2:
OurFirstPage
Notveryimpressive,Iadmit,butwewillstartimprovingitrightaway.
4.SecondTry:
StrutsForInternationalisation
WewilluseStrutstodisplaypredefinedtextsinthedefaultlanguageoftheuser.Well,notforeveryuser,Isuppose...ThiswillrequiresomecloserlookatthefunctionalityofStruts.
Displaythetitleofthepageindifferentlanguages.
WestartswithsomebasicsaboutStruts:
GotothedirectoryyouextractedtheStrutsdownloadtoandcopysomefilestoourWEB-INFdirectorylikeinthescreenshot(youshouldbrowsethroughafewoftheexamplesthatcomewithStruts,whilesearchingforthem).
Illustration3:
NeededLTDfiles
Takethetimetolookintothestruts-html.tldfile.Don'
tpanic,though,wewillnotadaptthosefiles!
TheyaretheretohelpusanddefinethefunctionalityofStruts.YoucanfindtheparametersyouneedforusingaStruttaginthere.Youcouldlookintotheofficialdocuandwouldprobablybebetteroff,though.
Ourweb.xmlmustdefineanactionServletandthelinkstotheltd-files.Justcopyoneweb.xmlfromtheStrutssourcesandremoveeverythingelse.
EmptyStruts-config.xml
?
xmlversion="
1.0"
encoding="
ISO-8859-1"
?
!
DOCTYPEstruts-configPUBLIC
"
-//ApacheSoftwareFoundation//DTDStrutsConfiguration1.0//EN"
http:
//jakarta.apache.org/struts/dtds/struts-config_1_0.dtd"
web.xml
DOCTYPEweb-app
PUBLIC"
-//SunMicrosystems,Inc.//DTDWebApplication2.2//EN"
web-app>
servlet>
servlet-name>
action<
/servlet-name>
servlet-class>
org.apache.struts.action.ActionServlet<
/servlet-class>
init-param>
param-name>
application<
/param-name>
param-value>
ApplicationResources<
/param-value>
/init-param>
config<
/WEB-INF/struts-config.xml<
debug<
2<
detail<
validate<
true<
load-on-startup>
/load-on-startup>
/servlet>
--StandardActionServletMapping-->
servlet-mapping>
url-pattern>
*.do<
/url-pattern>
/servlet-mapping>
--TheWelcomeFileList-->
welcome-file-list>
welcome-file>
index.jsp<
/welcome-file>
index.html<
/welcome-file-list>
--StrutsTagLibraryDescriptor-->
taglib>
taglib-uri>
/WEB-INF/struts-bean.tld
/taglib-uri>
taglib-location>
/taglib-location>
/taglib>
/WEB-INF/struts-html.tld
/WEB-INF/struts-logic.tld
/WEB-INF/struts-template.tld
/web-app>
Okay,letsstartwithsomethingreallyeasy.Wewilldisplaythetitleofthepageindifferentlanguages,dependingonthepreferenceoftheuser.
Createafilecalled'
ApplicationResources.properties'
intheclassesdirctory.
Openitandentertheline:
'
index.title=StrutsTutorial'
intoit
Createanotherfilecalled'
ApplicationResources_de.properties'
(deforDeutschland(germany))andenter'
index.title=StrutsEinfü
hrung'
EdittheBookView.jspandchangetheheadto:
BookView.jsp:
IntroducingInternationalization
%@tagliburi="
/WEB-INF/struts-bean.tld"
prefix="
bean"
/WEB-INF/struts-html.tld"
html"
/WEB-INF/struts-logic.tld"
logic"
html:
htmllocale="
true"
base/>
bean:
messagekey="
index.title"
/>
Illustration4:
Displayingthegermantitleelement
NowyouhavetorestartyourTomcatandafterreloadingthepageyoushouldseeatitle.Whatexactlyyouseedependsonthedefaultlanguageonyourcoumputer.Iseethegermantitle'
StrutsEinfü
.Changeyourdefaultlanguageandrelaodthepage.Setitfirsttoenglish,thentogermanandthetitleshouldadaptaccordingly.
Note:
Asyoujustsawitisveryeasytodisplaytextsindifferentlanguages.Whatyouwillrealizeveryquickly,though,ishowtediousitcangettoenter/check/changeeverythingindifferentlanguages,butthathasnothingtodowiththewayitisimplementedinStruts.IhavemadetheexperiencethatitisamusttoimplementeveryTextusingthevariablesandthatitisbesttousejustonefileforthedevelopment.then,beforemakingarelease,Iadaptthefilesfortheotherlanguages.ThiswayIsavealotoftime.Don'
tthinkyoucanwriteplaintextandexchangeitforvariablesatreleasetimethough!
5.StrutsforForms
Thischapterwillfinallyallowustodosomerealprogramming(abouttime,Iknow).WewillcreateasimpleBean(Book.java)andtwoJSPpages.Onetocreateanewbookandasecondtodisplayit.ForthatwewilluseStruts.Further,wewilltakeafirstlookatthestruts-config.xmltoconfigureourapplication.
TounderstandhowStrutscanhelpuswithstandardbehaviourconcerningforms.
Let'
sstartwiththeBean.CreateafileBook.java[II]inyourclassesdirectoryandenterthefollowing:
Book.java
importjava.util.Vector;
/*
Asimplebook.
@authorstephan@stephanwiesner.de
*/
publicclassBook
{
/**Thetitle*/
privateStringtitle="
"
;
/**Wecanhavemorethanoneauthor*/
privateVectorauthors=newVector();
/**Thenumberofpagesthebookhas*/
privateintpages=0;
/**Standardconstructor.*/
publicBook()
{}
/**@paramtitleThenewTitle*/
publicvoidsetTitle(Stringtitle)
{this.title=t
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Struts Tutorial Passo 阳飞