Struts 2 + Spring 3 + Hibernate 3.3 Integrated configuration in the MyEclipse 10 Environment

Source: Internet
Author: User

I want to do a login registration function under the SSH framework. Although there are many similar tutorials on the internet, I read a circle, either a version issue or missing many key steps. After several hours of hard work, we finally completed the so-called simple SSH integration to implement the registration function. Next we will share all the steps and hope to help our friends in need.

The following uses a simple registration module as an example to describe the integration steps and configuration.

1. Create a Web Project with any Project name;

2. add Struts 2 support: Right-click the project folder and choose "MyEclipse"> "Add Struts Capabilities" from the pop-up menu. Select Struts 2.1. In addition, select the Struts action url, the author chooses "\ *" and the style can also be selected. Also, pay attention to the libraries selection. We need to introduce Struts 2 Core Libraries and Struts2 Spring Libraries, and choose the default one for others;

3. add Spring 3 support: Right-click the project folder and choose MyEclipse> Add Spring Capabilities from the shortcut menu. On libraries selection, spring 3.0 AOP Libraries, Spring 3.0 Core Libraries, Spring 3.0 Persistence Core Libraries, Spring 3.0 Persistence JDBC Libraries, and Spring 3.0 Web Libraries need to be added;

4. add support for Hibernate 3.3: Right-click the project folder and choose MyEclipse-> Add Hibernate Capabilities from the shortcut menu. Select applicationContext as the configuration file. xml: select Existing Spring configuration file. The JDBC configuration can be queried Based on the selected database, and the Create SessionFactory class check box is removed;

5. database ing: Open the database in the DB Browser, right-click the database table used, select Hibernate Mapping and Application Generation, and create *. hbm. xml, JDO, and DAO. The default settings for other options are;

6. Modify the web. xml configuration file and add spring configuration information. The Code is as follows:

<listener>       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  </listener>  <context-param>       <param-name>contextConfigLocation</param-name>       <param-value>/WEB-INF/classes/applicationContext.xml</param-value>  </context-param>

 

7. Modify the struts. xml file to allow struts to support spring. Add the following code:

<constant name="struts.objectFactory" value="spring"/>

 

8. create a RegisterAction, inherit to ActionSupport, add two attributes, one for obtaining the registration information filled in, the other is DAO for database operations, where DAO can also be encapsulated as a service according to your preferences, which will not be described here, the Code is as follows:

public class RegisterAction extends ActionSupport{         private User user;         public User getUser() {                   return user;         }         public void setUser(User user) {                   this.user = user;        }         public UserDAO getUserDAO() {                   return userDAO;         }         public void setUserDAO(UserDAO userDAO) {                   this.userDAO = userDAO;         }         private UserDAO userDAO;                 public String execute() throws Exception{                   if(user!=null){                           userDAO.save(user);                           return SUCCESS;                   }else{                            return ERROR;                   }         }}

 

9. modify spring configuration applicationContext. xml: Add a bean to our RegisterAction. Here, note that userDAO is an attribute in the RegisterAction class we just created. It is a reference to bean UserDAO and the code to be added is as follows:

         <bean id="RegisterAction" class="actions.RegisterAction">                   <property name="userDAO">                            <ref bean="UserDAO"/>                   </property>         </bean>

 

10. modify struts configuration. xml: Add a package inherited from struts-default and an action. Note that the action class is not the RegisterAction class, but the id of the RegisterAction bean we declare in spring, the specific code is as follows:

<package name="struts" extends="struts-default">         <action name="Register" class="RegisterAction">                   <result name="error">/error.jsp</result>                   <result name="success">/success.jsp</result>         </action></package>

 

11. add related jsp pages. The key register is provided here. jsp page code. Note the user. name and user. password is the property defined in RegisterAction under the property user. It is generated through hibernate ing, as follows:

<% @ Page language = "java" import = "java. util. * "pageEncoding =" GB2312 "%> <% String path = request. getContextPath (); String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; %> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"> 

Summary: All configurations have been completed here. After deployment, perform a simple test, enter the user name and password, and click OK. A record is successfully added to the database.

(Original article, reproduced please note the author schbook: seekerxu@163.com)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.