In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
What this article shares to you is about how to integrate WebWork, Spring and Hibernate. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article.
1. Integration of Webwork and Spring
(1) enable the integration of Spring:
First add the jar of the * spring to the classpath, and then create the webwork.properties file in the src directory, which contains only the following content webwork.objectFactory=spring
In this case, all objects will at least try to create using Spring. If they cannot be created by Spring, then WebWork will create its own objects. Next, open Spring's Listener in web.xml
Org.springframework.web.context.ContextLoaderListener listener-class > listener >
Since standard Listener is used for WebWork and Spring integration, it can be configured to support configuration files other than applicationContext.xml. Adding the following lines to web.xml causes Spring's ApplicationContext to be initialized from all files that match the given rule:
ContextConfigLocationparam-name > / WEB-INF/applicationContext-*.xml,classpath*:applicationContext context-param >
Configure the appropriate spring context as needed.
(2) initialize Action in Spring
Normally, classes can be specified for each action in xwork.xml. When you use SpringObjectFactory, WebWork will ask Spring to create action and assemble dependent components according to the default autoassembly behavior. SpringObjectFactory will also set all bean post processors (post processors) to complete proxy things such as Action transactions, security, etc. Spring can be determined automatically without relying on external configuration. For most uses, this is all needed to configure action, set them to get services and dependent components.
A declarative approach is highly recommended to let Spring know what to offer to action. This includes enabling bean to automatically assemble, whether it's naming the dependent attributes in Action to match the name of the Bean that Spring should provide (which allows name-based autoassembly), or using by type autoassembly, that is, only one type is required in the Bean registered with Spring. It can also include using JDK5 standards to declare transaction and security requirements, rather than having to explicitly set proxies in your Spring configuration.
If you can find a way to let Spring know what needs to be done for action without any explicit configuration (in _ applicationContext.xml_), then you don't need to maintain the configuration in two places.
Of course, sometimes you may want Spring to manage bean completely. This makes practical sense, for example, if you want to set up more complex AOP or Spring-related technologies for bean, such as Acegi. To do this, all you have to do is configure bean in Spring's applicationContext.xml, and then change the class properties of your WebWork action in xwork.xml to use the name of bean defined in Spring instead of using the class name.
The xwork.xml file also changes the properties of the action class, leaving something like this
/ pages/registerSuccess.jspresult > action > package > / xwork >
A bean of Spring named "userAction" is defined in applicationContext.xml. Note that cn.com.nawang.Action.UserAction does not need to be changed, as it may be automatically assembled:
Bean >
Note: the id value in bean must be consistent with the corresponding class value in xwork.xml.
2. Native API based on Hibernate3 to implement DAO.
Hibernate 3.0.1 introduces a new feature: "Session with the following environment". This feature gives Hibernate itself the ability to bind the current Session object per transaction. This is roughly the same as synchronizing Session and transactions for each Hibernate in Spring.
(1) create the base class BaseDao for Dao
Public class BaseDao {private SessionFactory sessionFactory; public void setSessionFactory (SessionFactory sessionFactory) {this.sessionFactory = sessionFactory;} public Session getSession () {Session session = this.sessionFactory.getCurrentSession (); return session;}}
(2) implement specific persistence operations in the subclass Dao
Public class UserDao extends BaseDao implements IUserDao {public void saveUser (User user) throws HibernateException {getSession () .save (user);}}
(3), configure in context
Xml code "baseDao" class= "cn.com.nawang.dao.BaseDao" > name= "sessionFactory" ref= "sessionFactory" / > bean > "userDao" class= "cn.com.nawang.dao.impl.UserDao" parent= "baseDao" / > "userService" class= "cn.com.nawang.service.impl.UserService" > name= "userDao" ref= "userDao" / > bean > "userAction" class= "cn.com.nawang.action.UserAction" > name= "userService" ref= "userService" / > bean >
Restart the service and trigger the action of register on the web page. After execution, the following exception is thrown:
Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
After google, I probably understand the error caused by not configuring the transaction. Before configuring the transaction, I looked at a previous project implemented in HibernateDaoSupport, and remember that there was no need to configure the transaction to run normally.
So let UserDao inherit from HibernateDaoSupport, and the modified code is as follows:
Public class UserDao extends BaseDao implements IUserDao {public void saveUser (User user) throws HibernateException {getHibernateTemplate () .save (user);}}
Save the changes, restart the service, an error occurs during restart, check the relevant configuration in spring in action, and find that the configuration of baseTransaction this bean is slightly different.
The above configuration was made with reference to springside. At that time, the project was rushed and used directly, and there was no problem, so we did not seriously consider it. Now it was copied into the existing project, but there was a mistake, so we first made corresponding changes according to the introduction in the book. The revised content is as follows:
Java code public class UserDao extends BaseDao implements IUserDao {public void saveUser (User user) throws HibernateException {getHibernateTemplate () .save (user);}}
Next, modify the context-sensitive configuration during the integration of WebWork and Spring
UserDao "class=" cn.com.nawang.dao.impl.UserDao "> name=" sessionFactory "ref=" sessionFactory "/ > bean >" userService "class=" cn.com.nawang.service.impl.UserService "name=" userDao "ref=" userDao "/ > bean >" userAction "class=" cn.com.nawang.action.UserAction "> name=" userService "ref=" userService "/ > bean >
After saving the modified, restart the service, trigger the action of register again, and the user information is saved successfully.
After removing the dao implementation of HibernateDaoSupport, we switch back to the implementation based on hibernate3.0 native API, and configure userService according to the results of previous google.
Transaction, copy the configuration in the previous project, and modify it accordingly, the modified content is as follows:
PROPAGATION_REQUIRED,readOnlyprop > PROPAGATION_REQUIRED,readOnlyprop > PROPAGATION_REQUIRED ReadOnlyprop > PROPAGATION_REQUIREDprop > props > property > bean > bean >
Save the changes, restart the service, an error occurs during restart, check the relevant configuration in spring in action, and find that the configuration of baseTransaction this bean is slightly different.
The above configuration was made with reference to springside. At that time, the project was rushed and used directly, and there was no problem, so we did not seriously consider it. Now it was copied into the existing project, but there was a mistake, so we first made corresponding changes according to the introduction in the book. The revised content is as follows:
PROPAGATION_REQUIRED,readOnlyprop > PROPAGATION_REQUIREDprop > props > property > bean > removed
Change abstract= "true" to lazy-init= "true", save changes to restart the service, and trigger register's action again, as expected, thus completing the integration of WebWork with Sping and Hibernate.
The above is how to integrate WebWork with Spring and Hibernate. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.