`
- 浏览:
43402 次
- 性别:
- 来自:
上海
-
- Chapter14.Integratingwithotherwebframeworks
- 14.1.Introduction
-
SpringcanbeeasilyintegratedintoanyJava-basedwebframework.AllyouneedtodoistodeclaretheContextLoaderListenerinyourweb.xmlanduseacontextConfigLocation<context-param>tosetwhichcontextfilestoload.
- The<context-param>:
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/applicationContext*.xml</param-value>
- </context-param>
- The<listener>:
- <listener>
-
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
-
NOTE:ListenerswereaddedtotheServletAPIinversion2.3.IfyouhaveaServlet2.2container,youcanusetheContextLoaderServlettoachievethissamefunctionality.
-
Ifyoudon'tspecifythecontextConfigLocationcontextparameter,theContextLoaderListenerwilllookfora/WEB-INF/applicationContext.xmlfiletoload.Oncethecontextfilesareloaded,SpringcreatesaWebApplicationContextobjectbasedonthebeandefinitionsandputsitintotheServletContext.
-
AllJavawebframeworksarebuiltontopoftheServletAPI,soyoucanusethefollowingcodetogettheApplicationContextthatSpringcreated.
- WebApplicationContextctx=WebApplicationContextUtils.getWebApplicationContext(servletContext);
-
TheWebApplicationContextUtilsclassisforconvenience,soyoudon'thavetorememberthenameoftheServletContextattribute.ItsgetWebApplicationContext()methodwillreturnnullifanobjectdoesn'texistundertheWebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTEkey.RatherthanriskgettingNullPointerExceptionsinyourapplication,it'sbettertousethegetRequiredWebApplicationContext()method.ThismethodthrowsanExceptionwhentheApplicationContextismissing.
- OnceyouhaveareferencetotheWebApplicationContext,youcanretrievebeansbytheirnameortype.Mostdevelopersretrievebeansbyname,thencastthemtooneoftheirimplementedinterfaces.
-
Fortunately,mostoftheframeworksinthissectionhavesimplerwaysoflookingupbeans.NotonlydotheymakeiteasytogetbeansfromtheBeanFactory,buttheyalsoallowyoutousedependencyinjectionontheircontrollers.Eachframeworksectionhasmoredetailonitsspecificintegrationstrategies.
- 14.2.JavaServerFaces
-
JavaServerFaces(JSF)isacomponent-based,event-drivenwebframework.AccordingtoSunMicrosystem'sJSFOverview,JSFtechnologyincludes:
-
AsetofAPIsforrepresentingUIcomponentsandmanagingtheirstate,handlingeventsandinputvalidation,definingpagenavigation,andsupportinginternationalizationandaccessibility.
-
AJavaServerPages(JSP)customtaglibraryforexpressingaJavaServerFacesinterfacewithinaJSPpage.
- 14.2.1.DelegatingVariableResolver
-
TheeasiestwaytointegrateyourSpringmiddle-tierwithyourJSFweblayeristousetheDelegatingVariableResolverclass.Toconfigurethisvariableresolverinyourapplication,you'llneedtoedityourfaces-context.xml.Aftertheopening<faces-config>element,addan<application>elementanda<variable-resolver>elementwithinit.ThevalueofthevariableresolvershouldreferenceSpring'sDelegatingVariableResolver:
- <faces-config>
- <application>
- <variable-resolver>org.springframework.web.jsf.DelegatingVariableResolver</variable-resolver>
- <locale-config>
-
<default-locale>en</default-locale>
- <supported-locale>en</supported-locale>
- <supported-locale>es</supported-locale>
- </locale-config>
- <message-bundle>messages</message-bundle>
- </application>
-
ByspecifyingSpring'svariableresolver,youcanconfigureSpringbeansasmanagedpropertiesofyourmanagedbeans.TheDelegatingVariableResolverwillfirstdelegatevaluelookupstothedefaultresolveroftheunderlyingJSFimplementation,andthentoSpring'srootWebApplicationContext.ThisallowsyoutoeasilyinjectdependenciesintoyourJSF-managedbeans.
-
Managedbeansaredefinedinyourfaces-config.xmlfile.Belowisanexamplewhere#{userManager}isabeanthat'sretrievedfromSpring'sBeanFactory.
- <managed-bean>
- <managed-bean-name>userList</managed-bean-name>
-
<managed-bean-class>com.whatever.jsf.UserList</managed-bean-class>
- <managed-bean-scope>request</managed-bean-scope>
- <managed-property>
- <property-name>userManager</property-name>
- <value>#{userManager}</value>
- </managed-property>
- </managed-bean>
-
TheDelegatingVariableResolveristherecommendedstrategyforintegratingJSFandSpring.Ifyou'relookingformorerobustintegrationfeatures,youmighttakealookattheJSF-Springproject.
- 14.2.2.FacesContextUtils
-
AcustomVariableResolverworkswellwhenmappingyourpropertiestobeansinfaces-config.xml,butattimesyoumayneedtogrababeanexplicitly.TheFacesContextUtilsclassmakesthiseasy.It'ssimilartoWebApplicationContextUtils,exceptthatittakesaFacesContextparameterratherthanaServletContextparameter.
- ApplicationContextctx=FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
- 14.3.Struts
-
StrutsisthedefactowebframeworkforJavaapplications,mainlybecauseitwasoneofthefirsttobereleased(June2001).InventedbyCraigMcClanahan,StrutsisanopensourceprojecthostedbytheApacheSoftwareFoundation.Atthetime,itgreatlysimplifiedtheJSP/Servletprogrammingparadigmandwonovermanydeveloperswhowereusingproprietaryframeworks.Itsimplifiedtheprogrammingmodel,itwasopensource,andithadalargecommunity,whichallowedtheprojecttogrowandbecomepopularamongJavawebdevelopers.
- TointegrateyourStrutsapplicationwithSpring,youhavetwooptions:
-
ConfigureSpringtomanageyourActionsasbeans,usingtheContextLoaderPlugin,andsettheirdependenciesinaSpringcontextfile.
-
SubclassSpring'sActionSupportclassesandgrabyourSpring-managedbeansexplicitlyusingagetWebApplicationContext()method.
- 14.3.1.ContextLoaderPlugin
-
TheContextLoaderPluginisaStruts1.1+plug-inthatloadsaSpringcontextfilefortheStrutsActionServlet.ThiscontextreferstotherootWebApplicationContext(loadedbytheContextLoaderListener)asitsparent.Thedefaultnameofthecontextfileisthenameofthemappedservlet,plus-servlet.xml.IfActionServletisdefinedinweb.xmlas<servlet-name>action</servlet-name>,thedefaultis/WEB-INF/action-servlet.xml.
-
Toconfigurethisplug-in,addthefollowingXMLtotheplug-inssectionnearthebottomofyourstruts-config.xmlfile:
-
<plug-inclassName="org.springframework.web.struts.ContextLoaderPlugIn"/>
-
Thelocationofthecontextconfigurationfilescanbecustomizedusingthe"contextConfigLocation"property.
-
<plug-inclassName="org.springframework.web.struts.ContextLoaderPlugIn">
-
<set-propertyproperty="contextConfigLocation"
-
value="/WEB-INF/action-servlet.xml.xml,/WEB-INF/applicationContext.xml"/>
-
</plug-in>
-
Itispossibletousethisplugintoloadallyourcontextfiles,whichcanbeusefulwhenusingtestingtoolslikeStrutsTestCase.StrutsTestCase'sMockStrutsTestCasewon'tinitializeListenersonstartupsoputtingallyourcontextfilesinthepluginisaworkaround.Abughasbeenfiledforthisissue.
-
Afterconfiguringthisplug-ininstruts-config.xml,youcanconfigureyourActiontobemanagedbySpring.Spring1.1.3providestwowaystodothis:
-
OverrideStruts'defaultRequestProcessorwithSpring'sDelegatingRequestProcessor.
-
UsetheDelegatingActionProxyclassinthetypeattributeofyour<action-mapping>.
-
BothofthesemethodsallowyoutomanageyourActionsandtheirdependenciesintheaction-context.xmlfile.ThebridgebetweentheActioninstruts-config.xmlandaction-servlet.xmlisbuiltwiththeaction-mapping's"path"andthebean's"name".Ifyouhavethefollowinginyourstruts-config.xmlfile:
-
<actionpath="/users".../>
-
YoumustdefinethatAction'sbeanwiththe"/users"nameinaction-servlet.xml:
-
<beanname="/users".../>
- 14.3.1.1.DelegatingRequestProcessor
-
ToconfiguretheDelegatingRequestProcessorinyourstruts-config.xmlfile,overridethe"processorClass"propertyinthe<controller>element.Theselinesfollowthe<action-mapping>element.
- <controller>
-
<set-propertyproperty="processorClass"
-
value="org.springframework.web.struts.DelegatingRequestProcessor"/>
- </controller>
-
Afteraddingthissetting,yourActionwillautomaticallybelookedupinSpring'scontextfile,nomatterwhatthetype.Infact,youdon'tevenneedtospecifyatype.Bothofthefollowingsnippetswillwork:
-
<actionpath="/user"type="com.whatever.struts.UserAction"/>
-
<actionpath="/user"/>
-
Ifyou'reusingStruts'modulesfeature,yourbeannamesmustcontainthemoduleprefix.Forexample,anactiondefinedas<actionpath="/user"/>withmoduleprefix"admin"requiresabeannamewith<beanname="/admin/user"/>.
-
NOTE:Ifyou'reusingTilesinyourStrutsapplication,youmustconfigureyour<controller>withtheDelegatingTilesRequestProcessor.
- 14.3.1.2.DelegatingActionProxy
-
IfyouhaveacustomRequestProcessorandcan'tusetheDelegatingTilesRequestProcessor,youcanusetheDelegatingActionProxyasthetypeinyouraction-mapping.
-
<actionpath="/user"type="org.springframework.web.struts.DelegatingActionProxy"
-
name="userForm"scope="request"validate="false"parameter="method">
-
<forwardname="list"path="/userList.jsp"/>
-
<forwardname="edit"path="/userForm.jsp"/>
- </action>
-
Thebeandefinitioninaction-servlet.xmlremainsthesame,whetheryouuseacustomRequestProcessorortheDelegatingActionProxy.
-
IfyoudefineyourActioninacontextfile,thefullfeaturesetofSpring'sbeancontainerwillbeavailableforit:dependencyinjectionaswellastheoptiontoinstantiateanewActioninstanceforeachrequest.Toactivatethelatter,addsingleton="false"toyourAction'sbeandefinition.
-
<beanname="/user"singleton="false"autowire="byName"
-
class="org.example.web.UserAction"/>
- 14.3.2.ActionSupportClasses
-
Aspreviouslymentioned,youcanretrievetheWebApplicationContextfromtheServletContextusingtheWebApplicationContextUtilsclass.AneasierwayistoextendSpring'sActionclassesforStruts.Forexample,insteadofsubclassingStruts'Actionclass,youcansubclassSpring'sActionSupportclass.
-
TheActionSupportclassprovidesadditionalconveniencemethods,likegetWebApplicationContext().BelowisanexampleofhowyoumightusethisinanAction:
-
publicclassUserActionextendsDispatchActionSupport{
-
publicActionForwardexecute(ActionMappingmapping,
- ActionFormform,
- HttpServletRequestrequest,
- HttpServletResponseresponse)
- throwsException{
-
if(log.isDebugEnabled()){
-
log.debug("entering'delete'method...");
- }
- WebApplicationContextctx=getWebApplicationContext();
-
UserManagermgr=(UserManager)ctx.getBean("userManager");
-
-
returnmapping.findForward("success");
- }
- }
-
SpringincludessubclassesforallofthestandardStrutsActions-theSpringversionsmerelyhaveSupportappendedtothename:
- ActionSupport,
- DispatchActionSupport,
- LookupDispatchActionSupportand
- MappingDispatchActionSupport.
-
Therecommendedstrategyistousetheapproachthatbestsuitsyourproject.Subclassingmakesyourcodemorereadable,andyouknowexactlyhowyourdependenciesareresolved.However,usingtheContextLoaderPluginallowyoutoeasilyaddnewdependenciesinyourcontextXMLfile.Eitherway,Springprovidessomeniceoptionsforintegratingthetwoframeworks.
- 14.4.Tapestry
-
Tapestryisapowerful,component-orientedwebapplicationframeworkfromApache'sJakartaproject(http:
- 14.4.1.Architecture
-
AtypicallayeredJ2EEapplicationbuiltwithTapestryandSpringwillconsistofatopUIlayerbuiltwithTapestry,andanumberoflowerlayers,hostedoutofoneormoreSpringApplicationContexts.
- UserInterfaceLayer:
-
-concernedwiththeuserinterface
- -containssomeapplicationlogic
- -providedbyTapestry
-
-asidefromprovidingUIviaTapestry,codeinthislayerdoesitsworkviaobjectswhichimplementinterfacesfromtheServiceLayer.TheactualobjectswhichimplementtheseservicelayerinterfacesareobtainedfromaSpringApplicationContext.
- ServiceLayer:
-
-applicationspecific'service'code
-
-workswithdomainobjects,andusestheMapperAPItogetthosedomainobjectsintoandoutofsomesortofrepository(database)
-
-hostedinoneormoreSpringcontexts
-
-codeinthislayermanipulatesobjectsinthedomainmodel,inanapplicationspecificfashion.Itdoesitsworkviaothercodeinthislayer,andviatheMapperAPI.Anobjectinthislayerisgiventhespecificmapperimplementationsitneedstoworkwith,viatheSpringcontext.
-
-sincecodeinthislayerishostedintheSpringcontext,itmaybetransactionallywrappedbytheSpringcontext,asopposedtomanagingitsowntransactions
- DomainModel:
-
-domainspecificobjecthierarchy,whichdealswithdataandlogicspecifictothisdomain
-
-althoughthedomainobjecthierarchyisbuiltwiththeideathatitispersistedsomehowandmakessomegeneralconcessionstothis(forexample,bidirectionalrelationships),itgenerallyhasnoknowledgeofotherlayers.Assuch,itmaybetestedinisolation,andusedwithdifferentmappingimplementationsforproductionvs.testing.
-
-theseobjectsmaybestandalone,orusedinconjunctionwithaSpringapplicationcontexttotakeadvantageofsomeofthebenefitsofthecontext,e.g.,isolation,inversionofcontrol,differentstrategyimplementations,etc.
- DataSourceLayer:
- -MapperAPI(alsocalledDataAccessObjects):anAPIusedtopersistthedomainmodeltoarepositoryofsomesort(generallyaDB,butcouldbethefilesystem,memory,etc.)
-
-MapperAPIimplementations:oneormorespecificimplementationsoftheMapperAPI,forexample,aHibernate-specificmapper,aJDO-specificmapper,JDBC-specificmapper,oramemorymapper.
-
-mapperimplementationsliveinoneormoreSpringApplicationContexts.Aservicelayerobjectisgiventhemapperobjectsitneedstoworkwithviathecontext.
- Database,filesystem,orotherrepositories:
-
-objectsinthedomainmodelarestoredintooneormorerepositoriesviaoneormoremapperimplementations
-
-arepositorymaybeverysimple(e.g.filesystem),ormayhaveitsownrepresentationofthedatafromthedomainmodel(i.e.aschemainadb).Itdoesnotknowaboutotherlayershowerver.
- 14.4.2.Implementation
-
Theonlyrealquestion(whichneedstobeaddressedbythisdocument),ishowTapestrypagesgetaccesstoserviceimplementations,whicharesimplybeansdefinedinaninstanceoftheSpringApplicationContext.
- 14.4.2.1.Sampleapplicationcontext
-
AssumewehavethefollowingsimpleApplicationContextdefinition,inxmlform:
-
<?xmlversion="1.0"encoding="UTF-8"?>
-
<!DOCTYPEbeansPUBLIC"-//SPRING//DTDBEAN//EN"
-
"http://www.springframework.org/dtd/spring-beans.dtd">
- <beans>
- <!--=========================GENERALDEFINITIONS=========================-->
- <!--=========================PERSISTENCEDEFINITIONS=========================-->
- <!--theDataSource-->
-
<beanid="dataSource"class="org.springframework.jndi.JndiObjectFactoryBean">
-
<propertyname="jndiName"><value>java:DefaultDS</value></property>
-
<propertyname="resourceRef"><value>false</value></property>
- </bean>
- <!--defineaHibernateSessionfactoryviaaSpringLocalSessionFactoryBean-->
-
<beanid="hibSessionFactory"
-
class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
-
<propertyname="dataSource"><refbean="dataSource"/></property>
- </bean>
- <!--
-
-Definesatransactionmanagerforusageinbusinessordataaccessobjects.
-
-Nospecialtreatmentbythecontext,justabeaninstanceavailableasreference
-
-forbusinessobjectsthatwanttohandletransactions,e.g.viaTransactionTemplate.
- -->
-
<beanid="transactionManager"
-
class="org.springframework.transaction.jta.JtaTransactionManager">
- </bean>
-
<beanid="mapper"
-
class="com.whatever.dataaccess.mapper.hibernate.MapperImpl">
-
<propertyname="sessionFactory"><refbean="hibSessionFactory"/></property>
- </bean>
- <!--=========================BUSINESSDEFINITIONS=========================-->
- <!--AuthenticationService,includingtxinterceptor-->
-
<beanid="authenticationServiceTarget"
-
class="com.whatever.services.service.user.AuthenticationServiceImpl">
-
<propertyname="mapper"><refbean="mapper"/></property>
- </bean>
-
<beanid="authenticationService"
-
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
-
<propertyname="transactionManager"><refbean="transactionManager"/></property>
-
<propertyname="target"><refbean="authenticationServiceTarget"/></property>
-
<propertyname="proxyInterfacesOnly"><value>true</value></property>
-
<propertyname="transactionAttributes">
- <props>
-
<propkey="*">PROPAGATION_REQUIRED</prop>
- </props>
- </property>
- </bean>
- <!--UserService,includingtxinterceptor-->
-
<beanid="userServiceTarget"
-
class="com.whatever.services.service.user.UserServiceImpl">
-
<propertyname="mapper"><refbean="mapper"/></property>
- </bean>
-
<beanid="userService"
-
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
-
<propertyname="transactionManager"><refbean="transactionManager"/></property>
-
<propertyname="target"><refbean="userServiceTarget"/></property>
-
<propertyname="proxyInterfacesOnly"><value>true</value></property>
-
<propertyname="transactionAttributes">
- <props>
-
<propkey="*">PROPAGATION_REQUIRED</prop>
- </props>
- </property>
- </bean>
- </beans>
-
InsidetheTapestryapplication,weneedtoloadthisapplicationcontext,andallowTapestrypagestogettheauthenticationServiceanduserServicebeans,whichimplementtheAuthenticationServiceandUserServiceinterfaces,respectively.
-
14.4.2.2.ObtainingbeansinTapestrypages
-
Atthispoint,theapplicationcontextisavailabletoawebapplicationbycallingSpring'sstaticutilityfunctionWebApplicationContextUtils.getApplicationContext(servletContext),whereservletContextisthestandardServletContextfromtheJ2EEServletspecification.Assuch,onesimplemechanismforapagetogetaninstanceoftheUserService,forexample,wouldbewithcodesuchas:
- WebApplicationContextappContext=WebApplicationContextUtils.getApplicationContext(
- getRequestCycle().getRequestContext().getServlet().getServletContext());
-
UserServiceuserService=(UserService)appContext.getBean("userService");
- ...somecodewhichusesUserService
-
Thismechanismdoeswork.Itcanbemadealotlessverbosebyencapsulatingmostofthefunctionalityinamethodinthebaseclassforthepageorcomponent.However,insomerespectsitgoesagainsttheInversionofControlapproachwhichSpringencourages,whichisbeingusedinotherlayersofthisapp,inthatideallyyouwouldlikethepagetonothavetoaskthecontextforaspecificbeanbyname,andinfact,thepagewouldideallynotknowaboutthecontextatall.
-
Luckily,thereisamechanismtoallowthis.WerelyuponthefactthatTapestryalreadyhasamechanismtodeclarativelyaddpropertiestoapage,anditisinfactthepreferredapproachtomanageallpropertiesonapageinthisdeclarativefashion,sothatTapestrycanproperlymanagetheirlifecycleaspartofthepageandcomponentlifecycle.
- 14.4.2.3.ExposingtheapplicationcontexttoTapestry
-
FirstweneedtomaketheApplicationContextavailabletotheTapestrypageorComponentwithouthavingtohavetheServletContext;thisisbecauseatthestageinthepage's/component'slifecyclewhenweneedtoaccesstheApplicationContext,theServletContextwon'tbeeasilyavailabletothepage,sowecan'tuseWebApplicationContextUtils.getApplicationContext(servletContext)directly.OnewayisbydefiningacustomversionoftheTapestryIEnginewhichexposesthisforus:
- packagecom.whatever.web.xportal;
- ...
- import...
- ...
-
publicclassMyEngineextendsorg.apache.tapestry.engine.BaseEngine{
-
publicstaticfinalStringAPPLICATION_CONTEXT_KEY="appContext";
-
-
-
protectedvoidsetupForRequest(RequestContextcontext){
- super.setupForRequest(context);
-
- Mapglobal=(Map)getGlobal();
-
ApplicationContextac=(ApplicationContext)global.get(APPLICATION_CONTEXT_KEY);
-
if(ac==null){
- ac=WebApplicationContextUtils.getWebApplicationContext(
- context.getServlet().getServletContext()
- );
- global.put(APPLICATION_CONTEXT_KEY,ac);
- }
- }
- }
-
ThisengineclassplacestheSpringApplicationContextasanattributecalled"appContext"inthisTapestryapp's'Global'object.MakesuretoregisterthefactthatthisspecialIEngineinstanceshouldbeusedforthisTapestryapplication,withanentryintheTapestryapplicationdefinitionfile.Forexample:
- file:xportal.application:
-
<?xmlversion="1.0"encoding="UTF-8"?>
- <!DOCTYPEapplicationPUBLIC
-
"-//ApacheSoftwareFoundation//TapestrySpecification3.0//EN"
-
"http://jakarta.apache.org/tapestry/dtd/Tapestry_3_0.dtd">
- <application
-
name="WhateverxPortal"
-
engine-class="com.whatever.web.xportal.MyEngine">
- </application>
- 14.4.2.4.Componentdefinitionfiles
-
Nowinourpageorcomponentdefinitionfile(*.pageor*.jwc),wesimplyaddproperty-specificationelementstograbthebeansweneedoutoftheApplicationContext,andcreatepageorcomponentpropertiesforthem.Forexample:
-
<property-specificationname="userService"
-
type="com.whatever.services.service.user.UserService">
-
global.appContext.getBean("userService")
- </property-specification>
-
<property-specificationname="authenticationService"
-
type="com.whatever.services.service.user.AuthenticationService">
-
global.appContext.getBean("authenticationService")
- </property-specification>
-
TheOGNLexpressioninsidetheproperty-specificationspecifiestheinitialvaluefortheproperty,asabeanobtainedfromthecontext.Theentirepagedefinitionmightlooklikethis:
-
<?xmlversion="1.0"encoding="UTF-8"?>
- <!DOCTYPEpage-specificationPUBLIC
-
"-//ApacheSoftwareFoundation//TapestrySpecification3.0//EN"
-
"http://jakarta.apache.org/tapestry/dtd/Tapestry_3_0.dtd">
-
<page-specificationclass="com.whatever.web.xportal.pages.Login">
-
<property-specificationname="username"type="java.lang.String"/>
-
<property-specificationname="password"type="java.lang.String"/>
-
<property-specificationname="error"type="java.lang.String"/>
-
<property-specificationname="callback"type="org.apache.tapestry.callback.ICallback"persistent="yes"/>
-
<property-specificationname="userService"
-
type="com.whatever.services.service.user.UserService">
-
global.appContext.getBean("userService")
- </property-specification>
-
<property-specificationname="authenticationService"
-
type="com.whatever.services.service.user.AuthenticationService">
-
global.appContext.getBean("authenticationService")
- </property-specification>
-
<beanname="delegate"class="com.whatever.web.xportal.PortalValidationDelegate"/>
-
<beanname="validator"class="org.apache.tapestry.valid.StringValidator"lifecycle="page">
-
<set-propertyname="required"expression="true"/>
-
<set-propertyname="clientScriptingEnabled"expression="true"/>
- </bean>
-
<componentid="inputUsername"type="ValidField">
-
<static-bindingname="displayName"value="Username"/>
-
<bindingname="value"expression="username"/>
-
<bindingname="validator"expression="beans.validator"/>
- </component>
-
<componentid="inputPassword"type="ValidField">
-
<bindingname="value"expression="password"/>
-
<bindingname="validator"expression="beans.validator"/>
-
<static-bindingname="displayName"value="Password"/>
-
<bindingname="hidden"expression="true"/>
- </component>
- </page-specification>
-
14.4.2.5.Addingabstractaccessors
-
NowintheJavaclassdefinitionforthepageorcomponentitself,allweneedtodoisaddanabstractgettermethodforthepropertieswehavedefined,toaccessthem.WhenthepageorcomponentisactuallyloadedbyTapestry,itperformsruntimecodeinstrumentationontheclassfiletoaddthepropertieswhichhavebeendefined,andhookuptheabstractgettermethodstothenewlycreatedfields.Forexample:
-
-
publicabstractUserServicegetUserService();
-
-
publicabstractAuthenticationServicegetAuthenticationService();
-
Forcompleteness,theentireJavaclass,foraloginpageinthisexample,mightlooklikethis:
- packagecom.whatever.web.xportal.pages;
-
-
-
publicabstractclassLoginextendsBasePageimplementsErrorProperty,PageRenderListener{
-
-
publicstaticfinalStringUSER_KEY="user";
-
-
-
privatestaticfinalStringCOOKIE_NAME=Login.class.getName()+".username";
-
privatefinalstaticintONE_WEEK=7*24*60*60;
-
-
publicabstractStringgetUsername();
-
publicabstractvoidsetUsername(Stringusername);
-
publicabstractStringgetPassword();
-
publicabstractvoidsetPassword(Stringpassword);
-
publicabstractICallbackgetCallback();
-
publicabstractvoidsetCallback(ICallbackvalue);
-
publicabstractUserServicegetUserService();
-
publicabstractAuthenticationServicegetAuthenticationService();
-
-
protectedIValidationDelegategetValidationDelegate(){
-
return(IValidationDelegate)getBeans().getBean("delegate");
- }
-
protectedvoidsetErrorField(StringcomponentId,Stringmessage){
- IFormComponentfield=(IFormComponent)getComponent(componentId);
-
IValidationDelegatedelegate=getValidationDelegate();
-
delegate.setFormComponent(field);
-
delegate.record(newValidatorException(message));
- }
-
-
-
publicvoidattemptLogin(IRequestCyclecycle){
- Stringpassword=getPassword();
-
-
setPassword(null);
-
IValidationDelegatedelegate=getValidationDelegate();
-
delegate.setFormComponent((IFormComponent)getComponent("inputPassword"));
-
delegate.recordFieldInputValue(null);
-
-
if(delegate.getHasErrors())
-
return;
-
try{
- Useruser=getAuthenticationService().login(getUsername(),getPassword());
- loginUser(user,cycle);
- }
-
catch(FailedLoginExceptionex){
-
this.setError("Loginfailed:"+ex.getMessage());
-
return;
- }
- }
-
-
-
publicvoidloginUser(Useruser,IRequestCyclecycle){
- Stringusername=user.getUsername();
-
-
- Mapvisit=(Map)getVisit();
- visit.put(USER_KEY,user);
-
-
- ICallbackcallback=getCallback();
-
if(callback==null)
-
cycle.activate("Home");
-
else
- callback.performCallback(cycle);
-
-
- IEngineengine=getEngine();
-
Cookiecookie=newCookie(COOKIE_NAME,username);
- cookie.setPath(engine.getServletPath());
- cookie.setMaxAge(ONE_WEEK);
-
- cycle.getRequestContext().addCookie(cookie);
- engine.forgetPage(getPageName());
- }
-
publicvoidpageBeginRender(PageEventevent){
-
if(getUsername()==null)
- setUsername(getRequestCycle().getRequestContext().getCookieValue(COOKIE_NAME));
- }
- }
- 14.4.3.Summary
-
Inthisexample,we'vemanagedtoallowservicebeansdefinedintheSpringApplicationContexttobeprovidedtothepageinadeclarativefashion.Thepageclassdoesnotknowwheretheserviceimplementationsarecomingfrom,andinfactitiseasytoslipinanotherimplementation,forexample,duringtesting.ThisinversionofcontrolisoneoftheprimegoalsandbenefitsoftheSpringFramework,andwehavemanagedtoextenditallthewayuptheJ2EEstackinthisTapestryapplication.
- 14.5.WebWork
-
WebWorkisawebframeworkdesignedwithsimplicityinmind.It'sbuiltontopofXWork,whichisagenericcommandframework.XWorkalsohasanIoCcontainer,butitisn'tasfull-featuredasSpringandwon'tbecoveredinthissection.WebWorkcontrollersarecalledActions,mainlybecausetheymustimplementtheActioninterface.TheActionSupportclassimplementsthisinterface,anditismostcommonparentclassforWebWorkactions.
-
WebWorkmaintainsitsownSpringintegrationproject,locatedonjava.netinthexwork-optionalproject.Currently,threeoptionsareavailableforintegratingWebWorkwithSpring:
-
SpringObjectFactory:overrideXWork'sdefaultObjectFactorysoXWorkwilllookforSpringbeansintherootWebApplicationContext.
-
ActionAutowiringInterceptor:useaninterceptortoautomaticallywireanAction'sdependenciesasthey'recreated.
-
SpringExternalReferenceResolver:lookupSpringbeansbasedonthenamedefinedinan<external-ref>elementofan<action>element.
-
AllofthesestrategiesareexplainedinfurtherdetailinWebWork'sDocumentation.
- --------------------------------------------------------------------------------
- Prev
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
spring与webwork框架集成详细描述
整理网上大牛的资料 spring集成mybatis logback redis quartz 常用框架
struts+spring+ibatis框架集成.pdf
struts spring hibernate ssh 三大框架集成详细步骤 手把手教你搭框架 步骤清晰;搭配MyEclipse 5.5 M2截图;让你不需要任何其他资料;跟着步骤;轻松搭建;
struts+spring+hibernate框架集成
手把手教你集成spring cloud + shiro微服务...用最少的工作量,改造基于shiro安全框架的微服务项目,实现spring cloud + shiro 框架集成。博客地址:https://blog.csdn.net/weixin_42686388/article/details/103084289
spring框架集成socket服务,监听器检测到容器启动完成,通过线程启动socket服务
ssm框架整合,实现spring mvc,spring,mybaits框架的集成,
SpringMVC+Spring+mybatis框架集成,直接解压导入myeclipse或eclipse,修改com.xinxinsoft.util下的GeneratorConfig数据库配置信息,再运行com.xinxinsoft.util下的AutoGenerator下的main方法即可根据数据库信息自动...
spring.net和nhiberate框架的集成,如何通过spring.net集成Nhibernate框架
spring boot 的集成框架事例,让你高效的开发。
hibernate+struts+spring三大框架集成代码,个人学习时写的,希望对大家有用。
完整spring springmvc集成框架
但正如Spring A Developer's Notebook作者Bruce Tate 和Justin Gehtland所说的那样,与其他的OR框架相比,iBATIS独辟蹊径“iBATIS不是试图从根本上掩盖SQL,实际上,它是在拥抱SQL。” <br>但别犯愁:SQL本身具备...
spring-boot 2.0.0.RELEASE +MyBatis 框架集成。数据库使用mysql
核心容器。提供Spring框架的基本功能,其主要组件是BeanFactory,是工厂模式的实现。...Spring AOP 模块直接将面向方面编程的功能集成到Spring框架中。它为基于Spring 应用程序的对象提供了事务管理服务。
SpringMVC+Spring+mybatis框架集成,采用UTF-8编码,直接解压导入myeclipse或eclipse,修改com.xinxinsoft.util下的GeneratorConfig数据库配置信息,再运行com.xinxinsoft.util下的AutoGenerator下的main方法即可...
Spring Boot框架集成Redis教程
此文档只说明简单的框架集成,各个框架的高级特性未涉及,此文档适合有框架使用基础的开发人员查看,新手可能需要参考其他资料。