Struts2 + Spring + Hibernate implement the add, delete, modify, and query functions of employee management (1) Integrate the ssh framework and struts2ssh

Source: Internet
Author: User

Struts2 + Spring + Hibernate implement the add, delete, modify, and query functions of employee management (1) Integrate the ssh framework and struts2ssh

Preface reprinted please indicate the source: http://www.cnblogs.com/smfx1314/p/7795837.html

This project is an exercise I wrote to review the integration and use of the ssh framework. Project Description: a front-end administrator can log on to the employee management system page, and then perform regular addition, deletion, modification, and query on the employee list. And add, delete, modify, and query the Department list. IDE uses eclipse, which is easy to use. However, I am studying idea recently. The database is mysql and the frontend is mainly bootstrap.

This is directly excerpted.

Struts-controlled

Hibernate

Spring decoupled

Role of Struts, spring, and Hibernate at various layers

1) struts is responsible for the web layer.

ActionFormBean receives the data submitted by the form on the webpage, processes the data through the Action, and forwards the data to the corresponding webpage.

Define <action-mapping> In the struts-config.xml and the ActionServlet loads.

2) spring is responsible for business layer management, that is, Service (or Manager ).

1. The service provides a statistical call interface for action to encapsulate the DAO of the persistent layer.

2. You can write your own business methods.

3. unified management of javabean

4. Declarative Transaction Management

5. Integrate Hiberante

3) Hiberante, responsible for the persistence layer and performing database crud operations

Hibernate provides OR/Mapping for the persistence layer.

It has a set of. hbm. xml files and POJO, which correspond to tables in the database. Then define DAO. These are classes used to deal with databases, and they use PO.

In the struts + spring + hibernate system,

The object calling process is: jsp-> Action-> Service-> DAO-> Hibernate.

The data flow direction is that ActionFormBean accepts user data. Action extracts the data from ActionFromBean and encapsulates the data into VO or PO,

Then, call the Bean class at the business layer to complete various business processing and then forward. After receiving this PO object, the business layer Bean will call the DAO interface method to perform persistence operations.

 

Spring: Manages transaction control using Aop, manages coupling of various components using IoC, and uses DaoTemplate as a quick development template for the regular persistent layer!

Struts: control layer Action, page tag and Model data, call business layer

Hibernate: maps databases and objects, and is responsible for the DAO layer (Data Access Object: Data Access)

 

Spring integrates hibernate and struts. You only need to configure applicationContext. xml and directly call it in struts action. All hibernate database access operations are implemented in spring, and spring calls are implemented in stuts action. This ssh framework is even together ......

 

Remarks

I will not post the mysql table here. You can create it based on the object class. I created emp and dept tables, which have one-to-multiple relationships.

In addition, for details about *. js on the jsp page, you can create it by yourself based on the path in jsp.

Due to the large amount of content, let's talk about the integration of the ssh framework.

Next let's look at the directory structure

 

Step 1: import the jar package

 

Step 2: Let's take a look at the index. jsp page.

<% @ Page language = "java" contentType = "text/html; charset = UTF-8"
PageEncoding = "UTF-8" %>
<% @ Taglib uri = "/struts-tags" prefix = "s" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
<Html>
<Head>
<Meta charset = "UTF-8">
<Meta http-equiv = "Content-Type" content = "IE = edge">
<Title> logon </title>
<Script type = "text/javascript" src = "$ {pageContext. request. contextPath}/js/jquery. min. js"> </script>
<Link rel = "stylesheet" href = "$ {pageContext. request. contextPath}/utilLib/bootstrap.min.css" type = "text/css" media = "screen"/>
</Head>
<Body>
<Div class = "div_from_aoto">

Next, configure applicationContext. xml.

It mainly creates a database connection pool, creates sessionFactory, configures hibernate attributes and declarative transactions, aop (I didn't use it, so there is no configuration), and bean instantiation configuration.

<? Xml version = "1.0" encoding = "UTF-8"?>
<Beans xmlns = "http://www.springframework.org/schema/beans"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xmlns: aop = "http://www.springframework.org/schema/aop"
Xmlns: tx = "http://www.springframework.org/schema/tx"
Xmlns: context = "http://www.springframework.org/schema/context"
Xsi: schemaLocation = "http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans.xsd
Http://www.springframework.org/schema/tx
Http://www.springframework.org/schema/tx/spring-tx.xsd
Http://www.springframework.org/schema/aop
Http://www.springframework.org/schema/aop/spring-aop.xsd
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context.xsd "> <! -- Integrate hibernate -->
<! -- 1. Configure the database -->
<Bean id = "dataSource" class = "com. mchange. v2.c3p0. ComboPooledDataSource">
<Property name = "driverClass" value = "com. mysql. jdbc. Driver"> </property>
<Property name = "jdbcUrl" value = "jdbc: mysql: // localhost: 3306/ssh2"> </property>
<Property name = "user" value = "root"> </property>
<Property name = "password" value = "1234"> </property>
</Bean>
<! -- Configure sessionFactory -->
<Bean id = "sessionFactory" class = "org. springframework. orm. hibernate5.LocalSessionFactoryBean">
<! -- Data source -->
<Property name = "dataSource" ref = "dataSource"> </property>
<! -- Configure basic hibernate Attributes -->
<Property name = "hibernateProperties">
<Props>
<Prop key = "hibernate. dialect"> org. hibernate. dialect. MySQLDialect </prop>
<Prop key = "hibernate. show_ SQL"> true </prop>
<Prop key = "hibernate. hbm2ddl. auto"> update </prop>
</Props>
</Property>
<! -- Configure the hibernate ing file -->
<Property name = "mappingResources">
<List>
<Value> com/ssh/entity/User. hbm. xml </value>
<Value> com/ssh/entity/Emp. hbm. xml </value>
<Value> com/ssh/entity/Dept. hbm. xml </value>
</List>
</Property>
</Bean>
<! -- Configure a hibernate transaction -->
<Bean id = "transactionManager" class = "org. springframework. orm. hibernate5.HibernateTransactionManager">
<Property name = "sessionFactory" ref = "sessionFactory"> </property>
</Bean>
<! -- Enable transaction -->
<Tx: annotation-driven transaction-manager = "transactionManager"/>

<! -- Configure aop -->

</Beans>

Then introduce the Struts2 configuration file

<? Xml version = "1.0" encoding = "UTF-8"?>
<! DOCTYPE struts PUBLIC
"-// Apache Software Foundation // DTD Struts Configuration 2.3 // EN"
Http://struts.apache.org/dtds/struts-2.3.dtd>
<Struts>
<Package name = "ssh-day02" namespace = "/" extends = "struts-default">

 

</Package>
</Struts>

Note: The above Section integrates the hibernate configuration file and spring, and does not create any hibernate configuration files separately.

At this point, ssh configuration is basically complete. Running successfully is a login page

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.