tapestry教程_tapestry5_tapestry5 教程

电脑杂谈  发布时间:2017-01-22 21:08:57  来源:网络整理
tapestry5tapestry5

Tutoriel: Tapestry5 + Spring + HibernateIntroductionThis tutorial is a translation of a french tutorial written by Baptiste Meurant :This tutorial proposes a blanck application for a Tapestry5-Spring-Hibernate project. It consists on a main page of login that seach for the existence of a user inside a DB.This tutorial is in two parts : a blank application with Spring-Hibernate and in the second part we integrate Tapestry 5. With this separation you'll be able to reuse the first part with another front framework.The source of this tutorial could be found here or Http MirrorThe pdf version of this tutorial is available hereBefore you startInstallationInstall Eclipse and WTP here or try the official eclipse siteInstall Tomcat 5.5, download hereInstall mySQL, download herePrepare the DBCreate a table userCREATE TABLE `user` `id_user` int 10 unsigned NOT NULL auto_increment, `login_user` varchar 25 NOT NULL default '', `password_user` varchar 25 NOT NULL default '', PRIMARY KEY `id_user` ENGINE InnoDB;Note : The tables must be in InnoDB format in order to have the automatic generation of the model class working properly in Eclipse.Create a user nowinsert into user login_user, password_user values 'test', 'test' ;Creation of the projectNote : If you already have an existing project you can skip this part.Create a new project Dynamic Web : go to File - new Project and choose Web - Dynamic Web Project. Click on nextFill the project name and choose a Target Runtime. If nothing is defined click on newChoose the runtime here tomcat 5.5 . Caution : you must have installed the server of course.Fill the required informations and click on finishBack on the previous screen, click on Next and Next againAt the end of the process, the package explorer should look like this image : a Dynamic Web project has been created and a server configration integrated.Software architectureThe main insterest of setting your software architecture this way is having a rigourous management of your project, it garantees the maintainability, the evolutivity and the exploitation of the application. The image below shows the architecture that we're going to set up in this tutorial. This type of architecture is widely regarded as efficient and could be applied to any web project.The main goal of this kind of architecture is to separate the concerns buisness vs web using a strict separation of layers. Indeed one can see on the previous image the 3 layers of this application :DAO Layer : this layer manages the access to the database through the Hibernate framework.Service Layer : The layer represents all the buisness code, it organises the access to the DAO layer and manages all the transactionnal aspects. All of them are organized and managed by the Spring framework.MVC Layer : This layer is the IHM, it's the client part of the application. It interacts with the Service layer depending of the client action and retrieve the data to show them in a well formed way. This layer is undertaken by the Tapestry5 framework.Those layers must be decoupled, which means that there should be no dependencies between each other. Pratically this is possible by having each layer collborating to the other layer through intece. For instance the Service layer only know the inteces of the DAO layer and the MVC layer only know the inteces of the Service Layer. This way, each layer publish, through its inteces, all the treatement that it can offer to the upper layer. And the link between the intece and the implementation, which introduces a technologic dependency for instance the implementation of the DAO layer uses hibernate is managed by Spring.Indeed it's Spring when the application starts who establish the link between the intece and the implementation with its Inversion Of Control and dependencies Injection engine. The developper works only on method call from intece and not directly from the implementation, which enhances the evolutivity and the loose coupling of the application.We notice also a special layer : the model layer. This layer traverses the other three layers because it reflects the buisness entity stored in the DB model. Each layer naturally could manipulate those entities.The link between the buisness entities and the DB model is called Object Relational Mapping ORM , and the role of hibernate is to build this mapping. Every DB relations is represented by an object. Then persisting an object is actually persiting data in the DB but it's transparent for the developper. When the state of an object is modified in a transactional context the change will be propagated to the DB without any action from the developper. It's what we call object persistance.Let's focus for a little while on the manipulation of the DB. The previous explainations show two aspects :The DAO layer publishs its access methods to the DB, theses methods can search, create or delete records. Actually they can retreive objects from DB record, create new objects by creating new records in the DB, or delete object by deleting record in the DB.The object relational mapping and the persistance of datas in a transactionnal context allow any change in the state of an object to be propagated in the DB through UPDATE actions.Thus the DAO layer won't features any methods to update the objects. Thoses aspects will be covered in greater details later on.Nevertless, the creation of object corresponding to INSERT are still needed, because even if you are in a transactionnal context the service layer can't decide on your behalf if a new object of the model layer must be persited. After all it could be a temporary variable.Setting HibernateNote : the version of Hibernate is 3.2 or greater.In this part we're going to generate the object model from the relationnal model and then set the persitence through Hibernate.Download Hibernate Core, Hibernate Annotations and Hibernate Tools or get them in the archive of the tutorial.Install Hibernate toolHibernate Tools is an eclipse plugin that make possible to generate classes from the DB model :Unzip the archive.You'll find 2 directories : plugins and features, put what they contains respectively in the directory plugins and features of your eclipse installation.Restart eclipse.New elements show up in the intece, proving that the plugins is well setup :Add the librariesGet the driver of your DB for MySQL, see here and put it in the WEB-INF/lib directoryRefresh the project, the jar should show up.Add also in the WEB-INF/lib directory all the jar that come with hibernateConfigure the connexionCreate the directory config and add it as a source folderSelect the directory config and click on File/New/Other. Then choose Hibernate/Hibernate configuration file.Configure the connexion to the DB depending of your own settings. Don't forget to thick the checkbox 'Create Console configuration'We'll take the opportunity to configure the Hibernate Tools console in order to have a tool of mapping generation. Configure the console like shown below :Click on Classpath/Add JAR and add the DB Driver then click on finish.The Hibernate configuration look like this : xml version "1.0" encoding "UTF-8" !DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" ""hibernate-configuration session-factory property name "hibernate.connection.driver_class" com.mysql.jdbc.Driver property name "hibernate.connection.password" root property name "hibernate.connection.url" jdbc:mysql://localhost:3306/experiments property name "hibernate.connection.username" root property name "hibernate.dialect" org.hibernate.dialect.MySQLDialect /session-factory/hibernate-configurationOpen the console view and browse the DB. The result should be about the same than the image bellow. If no, check your connection settings.Generating the model layer code and the mappingLaunch the automatic generation of code :Configure the code generation this wayClick on export and only check the generation of Domain code. Then Click on run.The user code class has been generated with all the necessary annotationspackage tuto.webssh.domain.model;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.Id;import javax.persistence.Table;@Entity@Table name "user", catalog "experiments"public class User implements java.io.Serializable private int idUser; private String loginUser; private String passwordUser; public User public User int idUser, String loginUser, String passwordUser this.idUser idUser; this.loginUser loginUser; this.passwordUser passwordUser; @Id @Column name "id_user", unique true, nullable false public int getIdUser return this.idUser; public void setIdUser int idUser this.idUser idUser; @Column name "login_user", nullable false, length 25 public String getLoginUser return this.loginUser; public void setLoginUser String loginUser this.loginUser loginUser; @Column name "password_user", nullable false, length 25 public String getPasswordUser return this.passwordUser; public void setPasswordUser String passwordUser this.passwordUser passwordUser; we notice the annotations :@Entity : declares to hibernate that this class is persistant.@Table : to which table in the DB this entity must be mapped@Id : declares the property of the entity that is used as a primary key@Column : map a a property in the entity to a column of the table. This annotation brings extra constraint informations like for instance non nullity or non mutabillity. These hints on the column let hibernate validate the constraints before acting in the DB.Adding the mapped class to the Hibernate configurationModify hibernate.cfg.xml to add the mapped class.hibernate-configuration session-factory property name "hibernate.connection.driver_class" com.mysql.jdbc.Driver property name "hibernate.connection.password" root property name "hibernate.connection.url" jdbc:mysql://localhost:3306/experiments property name "hibernate.connection.username" root property name "hibernate.dialect" org.hibernate.dialect.MySQLDialect mapping class "tuto.webssh.domain.model.User"/ /session-factory/hibernate-configurationUser object is now persistent, every instance of will correspond to a record in the DB.Installing SpringNote : the version of Spring here should be 2.0 or greater.Download Spring hereUnzip and paste spring.jar in WEB-INF/libThe Spring framework is on top of an inversion of control and injections dependencies engine. To have layers of the application properly decoupled, each class of a layer collabore with the intece of the other layer. Then the Spring framework inject at runtime the implementation code of the intece in the different classes. The developper make this possible by creating the getters and the setters of this inteces, then Spring through xml configurations files use this setters to inject implementations of the inteces.Using this mechanism we're going to create two distincts layers: The DAO layer Data Access Object and the service layer buisness layer , the two layers are going to collabore with each other through inteces.The DAO LayerCreate DAO layer in the dao packageCreate the implementation of this intece : UserDaoImpl in the package dao.hibernate3. In order to benefit from the good integration of Spring with Hibernate, extends this class from HibernateDaoSupport.http://baptiste-meurant.developpez.com/tutoriaux/tapestry5-spring-hibernate/images/fig23.jpgHere is all the classes and inteces we've created:Intece UserDAOpackage tuto.webssh.domain.dao;import org.springframework.dao.DataAccessException;import tuto.webssh.domain.model.User;/*** Allows performing complex actions on persistent data * @author bmeurant*/public intece UserDao /** * Check if the login exists and if the password is correct in datasource. * @param login : user login * @param password : user password * @return true if the login exists and if the password is correct. * Otherwise, return false. * @throws DataAccessException in case of Data access errors * database unreachable, etc. */ public boolean checkLogin String login, String password ; /** * Return a User object from a given login. * @param login : user login * @return the corresponding user object. * @throws DataAccessException in case of Data access errors * database unreachable, etc. */ public User getUser String login ; UserDAOImplpackage tuto.webssh.domain.dao.hibernate3;import org.hibernate.Criteria;import org.hibernate.Session;import org.hibernate.criterion.Expression;import org.springframework.dao.DataAccessException;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import tuto.webssh.domain.dao.UserDao;import tuto.webssh.domain.model.User;/*** Implements a strategy to perform complex actions on persistent data.* @author bmeurant*/public class UserDaoImpl extends HibernateDaoSupport implements UserDao /** * @inheritDoc */ public boolean checkLogin String login, String password if null login || null password throw new IllegalArgumentException "Login and password are mandatory. Null values are forbidden." ; try logger.info "Check user with login: "+login+" and password : [PROTECTED]" ; Session session getHibernateTemplate .getSessionFactory .getCurrentSession ; // create a new criteria Criteria crit session.createCriteria User.class ; crit.add Expression.ilike "loginUser", login ; crit.add Expression.eq "passwordUser", password ; User user User crit.uniqueResult ; return user ! null ; catch DataAccessException e // Critical errors : database unreachable, etc. logger.error "Exception - DataAccessException occurs : "+e.getMessage +" on complete checkLogin ." ; return false; /** * @inheritDoc */ public User getUser String login if null login throw new IllegalArgumentException "Login is mandatory. Null value is forbidden." ; try logger.info "get User with login: "+login ; Session session getHibernateTemplate .getSessionFactory .getCurrentSession ; // create a new criteria Criteria crit session.createCriteria User.class ; crit.add Expression.eq "loginUser", login ; User user User crit.uniqueResult ; return user; catch DataAccessException e // Critical errors : database unreachable, etc. logger.error "Exception - DataAccessException occurs : "+e.getMessage +" on complete getUser ." ; return null; We notice :The use of the logger API commons-logging already integrated with HibernateDaoSupport - commons-logging let you choose your log tool, in this example we choose the powerful Log4J.The use of the Hibernate Criteria API that let you make request based on Object. We could do the same thing with HQL but it's not as elegant as the Criteria API is.The use of differents Spring inteces for accessing the Hibernate for instance getHibernateTemplate , etc. .Create the xml file ApplicationContextDao.xml in WEB-INF and declare the couple intece/implementation using Spring's bean. By Default a Spring's bean is a singleton and thus are ThreadSafe. This means that this code could be concurently called by two different processes safly. This mechanism in internally managed by Spring.This file defines a bean userDao corresponding to the previous intece and we choose the implementation we want to use. This bean contains a property in the JavaBeans sens sessionFactory. The sessionFactory bean represents actually the Hibernate SessionFactory and has to be configured as well. The two mains configuration aspects of this beans is where to find the hibernate.cfg.xml and which strategy must be used for mapping entity to the DB tables, here it's the annotation strategy. xml version "1.0" encoding "UTF-8" !DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" ""!-- Application context DAO layer -- !-- General -- bean id "userDao" class "tuto.webssh.domain.dao.hibernate3.UserDaoImpl" property name "sessionFactory" ref bean "sessionFactory" / !-- sessionFactory -- bean id "sessionFactory" class "org.springframework.orm.hibernate3.LocalSessionFactoryBean" property name "configLocation" classpath:hibernate.cfg.xml property name "configurationClass" org.hibernate.cfg.AnnotationConfiguration The services layerThe DAO layer will be used by the upper layer : the services layerCreate two packages service and service.impl, create the intece UserManager and its implementation UserManagerImpl.Intece UserManagerpackage tuto.webssh.service;import tuto.webssh.domain.model.User;/*** This intece publishes business features to handler users* @author bmeurant*/public intece UserManager /** * Check if the login exists and if the password is correct. * @param login : user login * @param password : user password * @return true if the login exists and if the password is correct. * Otherwise, return false. */ public boolean checkLogin String login, String password ; /** * Return a User object from a given login. * @param login : user login * @return the corresponding user object. */ public User getUser String login ; /** * Change the password to 'password' for the given login * @param login : user login * @param password : user new password * @return the new User object */ public User changePassword String login, String password ; Implementation UserManagerImplpackage tuto.webssh.service.impl;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import tuto.webssh.domain.dao.UserDao;import tuto.webssh.domain.model.User;import tuto.webssh.service.UserManager;/*** Implements business features to handler users* @author bmeurant*/public class UserManagerImpl implements UserManager private final Log logger LogFactory.getLog UserManagerImpl.class ; /** * @inheritDoc */ public boolean checkLogin String login, String password return userDao.checkLogin login, password ; /** * @inheritDoc */ public User changePassword String login, String password User user userDao.getUser login ; if user ! null user.setPasswordUser password ; return user; /** * @inheritDoc */ @SuppressWarnings "finally" public User getUser String login return userDao.getUser login ; The services layer need to collaborate with the dao layer. But following the principle of loose coupling, the service layer is going to collaborate with the intece of the dao not with its implementation. Spring has the duty to inject the implementation in place of the intece at runtime. Two things need to be done to make this possible : provide in the service class a setter method for the dao and write the needed configuration in xml files.Edit UserManagerImpl to create the dao injector actually its a setter . We also add two extras methods that we be needed for the next steps.UserManagerImpl modifiedpackage tuto.webssh.service.impl;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import tuto.webssh.domain.dao.UserDao;import tuto.webssh.domain.model.User;import tuto.webssh.service.UserManager;/*** Implements business features to handler users* @author bmeurant*/public class UserManagerImpl implements UserManager private final Log logger LogFactory.getLog UserManagerImpl.class ; private UserDao userDao null; /** * setter to allows spring to inject userDao implementation * @param userDao : object implementation of UserDao intece to inject. */ public void setUserDao UserDao userDao this.userDao userDao; /** * @inheritDoc */ public boolean checkLogin String login, String password return userDao.checkLogin login, password ; /** * @inheritDoc */ public User changePassword String login, String password User user userDao.getUser login ; if user ! null user.setPasswordUser password ; return user; /** * @inheritDoc */ @SuppressWarnings "finally" public User getUser String login return userDao.getUser login ; Notice that there's no reference to the implementation : only the intece is known and used via the locale variable userDao. At startup, Spring uses the setter to inject the implementation you defined in ApplicationContextDao.xml. Thanks to the configuration of ApplicationContextDao.xml, all the method of UserDao become available Caution : actually only the method defined in the intece are available, anyway trying to invoke a non intece method would end up in compilation error .To make this injection effective, userManager need to be declared to Spring and must add the internal property userDao.Create the file ApplicationContext.xml in WEB-INF:ApplicationContext.xml xml version "1.0" encoding "UTF-8" !DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "" bean id "userManager" class " tuto.webssh.service.impl.UserManagerImpl" property name "userDao" ref bean "userDao" / Once again we deal with the paradigm intece/implementation with this time the use of injection through the definition of userDao which reference the bean defined before. : the name of the bean must be exactly the name of the property defined in userManager. Spring will use camelise mechanism to invoke the setter : ie set + "U"serDao userDAO ;Transaction managementBeside inversion of control, dependencies injection and layering structuration of your code, Spring features powerful mechanism to manage the transactions. Transaction rely on proxy and AOP Aspect Oriented Programmation , which is the core of the Spring Framework. You configure transaction in three steps :First define a general abstract proxy that will be used by all the manager that need to be transactional : Add this code to applicationContextDao.xmlapplicationContextDao.xml bean id "transactionManager" class "org.springframework.orm.hibernate3.HibernateTransactionManager" property name "sessionFactory" ref "sessionFactory"/ bean id "transactionProxy" abstract "true" class "org.springframework.transaction.interceptor.TransactionProxyFactoryBean" property name "transactionManager" ref bean "transactionManager"/ property name "transactionAttributes" prop key "insert*" PROPAGATION_REQUIRED prop key "update*" PROPAGATION_REQUIRED prop key "save*" PROPAGATION_REQUIRED prop key "*" PROPAGATION_REQUIRED, readOnly Then for every bean that need to be transactionnal you feature a proxy that inherit from the general TransactionProxyapplicationContext.xml bean id "userManagerTarget" class "tuto.webssh.service.impl.UserManagerImpl" property name "userDao" ref bean "userDao" / bean id "userManager" parent "transactionProxy" property name "target" ref bean "userManagerTarget"/ property name "transactionAttributeSource" bean class "org.springframework.transaction.annotation.AnnotationTransactionAttributeSource"/ We put a transactionnal proxy in front of the buisness bean. The transactionManager is passed to the transactionnal proxy and we declare to Spring that the transaction configuration is done with annotation thanks to the property transactionAttributeSource.Now we need to add the necessary annotation to the intece UserManager to configure its transactionnal behavior. Change UserManager :UserManagerpackage tuto.webssh.service;import org.springframework.transaction.annotation.Propagation;import org.springframework.transaction.annotation.Transactional;import tuto.webssh.domain.model.User;/*** This intece publishes business features to handler users* @author bmeurant*/@Transactional readOnly true, propagation Propagation.REQUIREDpublic intece UserManager /** * Check if the login exists and if the password is correct. * @param login : user login * @param password : user password * @return true if the login exists and if the password is correct. * Otherwise, return false. */ public boolean checkLogin String login, String password ; /** * Return a User object from a given login. * @param login : user login * @return the corresponding user object. */ public User getUser String login ; /** * Change the password to 'password' for the given login * @param login : user login * @param password : user new password * @return the new User object */ @Transactional readOnly false public User changePassword String login, String password ; With this configuration Spring will be able to manage on its own all the transaction aspects: commit, rollback and persitence of the attached object as long as the flow of execution is occuring in a transactionnal context.Let's watch the implementation of the changePassword method and notice the persitent nature of the User object : in this method the user is retreived with the login. Then the password is changed and no access to the DAO layer is needed anymore. Only because this method is in a transactionnal context and also not read-only , any changed to the object will be propagated to the DB.Integrate Spring when the project startThe last step consists in integrating this 2 xml beans definition files to the Spring listener in web.xml to make sure Spring build those object at startup :add this in web.xmlcontext-param param-name contextConfigLocation /param-name param-value /WEB-INF/applicationContext.xml /WEB-INF/applicationContextDao.xml /param-value/context-param listener-class org.springframework.web.context.ContextLoaderListener /listener-class Then add the Spring dependenciesSpring will load the whole configuration at startup and eventually throw errors.To get more information, add the jar log4j to your classpath and the file log4j.properties in the config directories.Lo4j.properties# Set root logger level to DEBUG and its only appender to CONSOLE.log4j.rootLogger DEBUG,CONSOLE_APP# le appender CONSOL_APP est associé à la consolelog4j.appender.CONSOLE_APP org.apache.log4j.ConsoleAppender# CONSOLE_APP utilise un PatternLayout qui affiche : le nom du thread, la priorité,# le nom du logger et le messagelog4j.appender.CONSOLE_APP.layout org.apache.log4j.PatternLayoutlog4j.appender.CONSOLE_APP.layout.ConversionPattern %d dd-MM-yyyy HH:mm:ss:SSS %-4r %-5p %c %x - %m%n# Change the level of messages for various packages.log4j.logger.org.apache DEBUGlog4j.logger.org.springframework DEBUGlog4j.logger.org.hibernate.cache DEBUGlog4j.logger.org.hibernate.cfg DEBUGlog4j.logger.org.hibernate DEBUGAt startup Spring will log its actionsINFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFact[emailprotected]:defining beans [userManagerTarget,userManager,userDao,sessionFactory,transactionManager,transactionProxy];root of factory hierarchySetting up tapestryNote : in this tutorial we use tapestry 5.Simple loginThe first step is to add the jars to the projectThen create Login.html in the WEB-INF directoryLogin.htmlhtml xmlns:t "" Login div id "login_box" t:form t:errors / label t:type "Label" for "login" class "login_label"/ input t:type "TextField" t:id "login" t:value "login" t:label "login " class "login_input" / label t:type "Label" for "password" class "login_label"/ input t:type "PasswordField" t:id "password" t:value "password" t:label "password " class "login_input" / input t:id "submitform" t:type "Submit" t:value "submit" class "login_submit"/ /t:form Notice the attributes t:value that binds the textefield value to a page property. The atrribute for binds the html input to the label.Create a package pages and a class Login.java which mirrors the html form :Login.javapackage tuto.webssh.pages;import org.apache.tapestry.annotations.ApplicationState;import org.apache.tapestry.beaneditor.Validate;public class Login @ApplicationState private String login; private String password; public String getLogin return login; @Validate "required" public void setLogin String login this.login login; public String getPassword return password; @Validate "required" public void setPassword String password this.password password; String onSuccess //The buisness code String ret "Home"; return ret; We note :The annotation @ApplicationState which persist a variable in the session. Every time the login page reload or any page with this variable name coming along with the @ApplicationState annotation , the value of the variable will be obtained from the session.The annotation @Validate "required" let tapestry knows this input is mandatory. When the form submit, if the value is not defined an error message shows up.The method onSuccess is invoked when the form is succesfully submitted, it returns the next page here Home .Create a page Home.html within WEB-INF, the user will be directed to this page after it submits Loginhtml xmlns:t "" Congratulation Congratulations, you are logged with login: $ login !! span id "LogoutLink" span class "moduleTitle" t:actionlink t:id "logout" Deconnexion /t:actionlink $ login uses the login variable in the session if defined. Tapestry can retrieve this value if the context ApplicationState has been defined for both classes : Login.java and Home.java must bear the login property with the ApplicationState context.The element t:actionLink define another link to a tapestry page.In the package pages create Home.javapackage tuto.webssh.web.pages;import javax.servlet.http.HttpSession;import org.apache.tapestry.ComponentResources;import org.apache.tapestry.Link;import org.apache.tapestry.annotations.ApplicationState;import org.apache.tapestry.annotations.Inject;import org.apache.tapestry.annotations.OnEvent;import org.apache.tapestry.services.RequestGlobals;public class Home @Inject private ComponentResources resources; @Inject private RequestGlobals requestGlobals; @ApplicationState private String login; public String getLogin return login; public void setLogin String login this.login login; @OnEvent component "logout" public Link onLogout HttpSession session requestGlobals.getHTTPServletRequest .getSession ; session.invalidate ; return resources.createPageLink "login", false ; Once again, the annotation @ApplicationState will retrieve the variable login from the session.The annotation @Inject let tapestry inject dependencies from third parties source here J2EE core .The method onLogout which thanks to the annotation @onEvent component "logout" will be invoked every time the component bubble an event. Here, the component is a link, so every time the link is clicked the session is invalidated and the user comebacks to the login page.Edit web.xml to configure the Tapestry filter and the base package from which tapestry knows where to find the pages package that contains page beans. xml version "1.0" encoding "UTF-8" web-app id "WebApp_ID" version "2.4" xmlns "" xmlns:xsi "" xsi:schemaLocation " http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" display-name BlankApplication /display-name context-param param-name contextConfigLocation /param-name param-value /WEB-INF/applicationContext.xml /WEB-INF/applicationContextDao.xml /param-value /context-param context-param param-name tapestry.app-package /param-name param-value tuto.webssh.web /param-value /context-param filter-name app /filter-name filter-class org.apache.tapestry.TapestryFilter /filter-class filter-mapping filter-name app /filter-name url-pattern /* /url-pattern /filter-mapping listener-class org.springframework.web.context.ContextLoaderListener /listener-class welcome-file-list welcome-file Login /welcome-file /welcome-file-list/web-appStart the server and type the url : :8080/BlankApplicationTapestry/login. you should get the following pageNotice that if you click Submit before filling up the fields login and password, errors are displayed. this is the expected tapestry behaviour when you annotate a property with @Validate.Otherwise the Home page shows up. Notice that the login is now in the session and is displayed on the Home page though this value was provided in the Login page.Clicking on deconnexion lead you to the Login page again.Integration with SpringIf the result is already interesting, it's still not enough. We must now search the user in the DB, check its existence, if his password is valid, etc. To achieve this we're going to traverse the different application layers, from tapestry to dao passing by the service.As explained before, the communicationship between the different layers - as some advanced functions transactions, etc. - are managed by Spring. We must integrate Spring and Tapestry. Tapestry5 propose natively a support to Spring.The first thing to do is to change the tapestry filter in web.xml : replace the class org.apache.tapestry.TapestryFilter by org.apache.tapestry.spring.TapestrySpringFilter. filter-name app /filter-name !-- Special filter that adds in a T5 IoC module derived from the Spring WebApplicationContext. -- filter-class org.apache.tapestry.spring.TapestrySpringFilter /filter-class Once Tapestry is configured to work with Spring, we must inject the Spring beans in the Tapestry pages. change Login.java :package tuto.webssh.pages;import org.apache.tapestry.annotations.ApplicationState;import org.apache.tapestry.annotations.Inject;import org.apache.tapestry.annotations.Persist;import org.apache.tapestry.annotations.Service;import org.apache.tapestry.beaneditor.Validate;import tuto.webssh.service.UserManager;public class Login private static final String BAD_CREDENTIALS "Bad login and/or password. Please retry."; @Persist private boolean error false; @ApplicationState private String login; @Inject @Service "userManager" private UserManager userManager; private String password; public String getLogin return login; @Validate "required" public void setLogin String login this.login login; public String getPassword return password; public String getErrorMessage String ret null; if error ret BAD_CREDENTIALS; return ret; @Validate "required" public void setPassword String password this.password password; String onSuccess String ret "Login"; error true; if userManager.checkLogin login, password error false; ret "Home"; return ret; We notice :the annotations @Inject and @Service inject the bean Manager from Spring into Tapestry. We can now use all of the methods defined in the intece here we modify the methode onSucces, when the form is submitted the application called the Spring userManager service to execute the checkLogin method and check that login and password really exist in DB. If yes the user is redirected to the Home page. In the opposite, we initialize an error message variable to be diplayed to the user.The annotation @Persist on error persist the value of error only for this page : instead of the annotation @ApplicationState, @Persist define persistence only for the page not for the whole application.The getErrorMessage make the property errorMessage available in the Login page. change Login.html :html xmlns:t "" Login div id "login_box" t:form t:errors / label style "color:red; font-weight:bold;" $ errorMessage label t:type "Label" for "login" class "login_label"/ input t:type "TextField" t:id "login" t:value "login" t:label "login " class "login_input" / label t:type "Label" for "password" class "login_label"/ input t:type "PasswordField" t:id "password" t:value "password" t:label "password " class "login_input" / input t:id "submitform" t:type "Submit" t:value "submit" class "login_submit"/ /t:form Start the server and navigate to :8080/BlankApplicationTapestry/login. Choose an incorrect login and/or password, you are redirected to the page login and the expected message is displayed :Now type the good login/password 'test', 'test' and validate. You are redirected to the page Home :Setting the lazy loading for hibernateAs we explained it at the beginning of this tutorial dealing with hibernate, every model object is related to a table in the DB. We therefore get a complete object graph : an entity having a 1,n relation with entity E2 in object realm is going to be object O1 of type T1 hold an object O2 of type collection of T2 ?And so on.We quickly realized that the object graph could be extremely heavy if all collections and children of children collections must be loaded. It's why by default, Hibernate uses "Lazy loading". Lazy loading means that when the object is loaded in memory, only the object is loaded not all its collections; collections will be loaded when they will first be used. The opposite of lazy loading is eager loading : when the object is loaded all the graph is loaded. You must explicitly annotate your classes to have eager loading 1,n ; n,1 ou n,n : @OneToMany, @ManyToMany ou @ManyToOne cascade , fetch FetchType.EAGER .One very important thing to understand about lazy loading is that you can access to the lazy collections only if the transaction in wich you got this object is not commited yet. Indeed, the lazy loading is possible because the session Hibernate is still active and opened to be able to complete the graph from the DB when needed. And with the transaction manager we used in this tutorial, the session is closed at the end of the execution of the service method. Thus if we try to access a collection in the view we're going to have a LazyInitializationException.But there's a workaround to keep the session Hibernate "alive" until the page renders, with this workaround you only load what's really needed. It doesn't mean that you can do now whatever you want with the session. Indeed the session is alive until the request completes ; it means that we are going to be able to access a lazy collection in the view, but if you decide to put the object you got from a transactionnal context in the http session you won't be able to retreive a lazy collection in the next request. The Hibernate session would have died.Extending the life of the httpSession is easy with Spring thanks to a dedicated filter : OpenSessionInViewFilter. Just change your web.xml : filter-name Hibernate Session In View Filter /filter-name filter-class org.springframework.orm.hibernate3.support.OpenSessionInViewFilter /filter-class filter-mapping filter-name Hibernate Session In View Filter /filter-name url-pattern /* /url-pattern/filter-mappingMake sure this filter is declared before the the tapestry filter.To test this new filter we're going to create a new table 'rights' that will defines the rights of a given user :CREATE TABLE `experiments`.`rights` `id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,`label` VARCHAR 45 NOT NULL,`id_user` INTEGER UNSIGNED NOT NULL,PRIMARY KEY `id` ,CONSTRAINT `FK_user` FOREIGN KEY `FK_user` `id_user` REFERENCES `user` `id_user` ON DELETE CASCADE Create some data testINSERT INTO rights label, id_user VALUES 'USER', 1 ;INSERT INTO rights label, id_user VALUES 'ADMIN', 1 ;We must redo the mapping now with the entity Right and UserUser.ja


本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/jisuanjixue/article-28134-1.html

    相关阅读
      发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

      热点图片
      拼命载入中...