Struts2+spring+hibernate implementation of employee management and deletion check function (i) SSH Framework integration

Source: Internet
Author: User

Preface reprint Please indicate 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 Introduction: This project mainly has the front desk administrator through the login to enter the Employee Management System page, then can the employee list carries on the general deletion and modification investigation. And the list of departments and additions and deletions to check. The IDE uses eclipse, the personal feeling is more useful, but recently I was studying idea, the database is MySQL, the front desk mainly to bootstrap-based.

This is a direct excerpt.

For Struts control

Hibernate operations database.

Spring uses a decoupled

The role of Struts, spring and Hibernate in each layer

1) Struts is responsible for the Web tier.

Actionformbean receives the data submitted by the form in the Web page, then processes it through the Action and Forward it to the corresponding page.

The <ACTION-MAPPING> is defined in Struts-config.xml, and Actionservlet is loaded.

2) Spring is responsible for business layer management, i.e. service (or manager).

1. The service provides a statistical calling interface for the action, encapsulating the DAO for the persistence layer.

2. Can write some of their own business methods.

3. A unified approach to JavaBean management

4. Declarative transaction Management

5. Integrated Hiberante

3) Hiberante, responsible for persistence layer, complete the database crud operation

Hibernate is a persistent layer that provides or/mapping.

It has a set of. hbm.xml files and POJO that correspond to the tables in the database. Then you define DAO, which is the class that deals with the database, and they use the PO.

In the Struts+spring+hibernate system,

The calling process for an object is: Jsp-> Action-> Service->dao->hibernate.

The flow of data is Actionformbean accepts the user's data, the Action takes the data out of the Actionfrombean, encapsulates it into VO or PO,

Then call the business layer of the Bean class, complete a variety of business processing and then forward. When the business layer Bean receives the PO object, it invokes the DAO interface method for persistence.

SPRING:AOP Management Transaction Control, IOC manages the coupling of various components, daotemplate as a rapid development template for regular persistence layer!

Struts: Control layer Action, page label and model data, calling the business layer

Hibernate: Responsible for database and object mapping, the DAO layer (data Access object)

Spring integrates hibernate and struts, as long as the applicationcontext.xml is well-equipped and is called directly in the struts action. Hibernate access to the database is implemented in spring, and spring calls are implemented in Stuts's action. This SSH framework is connected together ...

Note

Here's the MySQL table I won't post it, this can be created according to the entity class, I created the EMP and the Dept table, they are a one-to-many relationship.

In addition, about *.js in JSP pages, you can create them based on the path in the JSP

Because of too much content, today I'll say the integration of SSH framework

Now let's look at the directory structure

The first step is to import the jar package

Step two: Let's 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" >
<meta charset= "Utf-8" >
<meta http-equiv= "Content-type" content= "Ie=edge" >
<title> Login </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"/>
<body>
<div class= "Div_from_aoto" style= "width:500px;" >
<form action= "${pagecontext.request.contextpath}/user_login.action" method= "POST" >
<div class= "Control-group" >
<label class= "Laber_from" > Username </label>
<div class= "Controls" ><input class= "Input_from" Type=text name= "username" placeholder= "Please enter user name" ></ Input><p class= "Help-block" ></p></div>
</div>
<div class= "Control-group" >
<label class= "Laber_from" > Password </label>
<div class= "Controls" ><input class= "Input_from" Type=password name= "password" placeholder= "Please enter your password" ></ Input><p class= "Help-block" ></p></div>
</div>

<div class= "Control-group" >
<label class= "Laber_from" ></label>
<div class= "Controls" >
<button class= "btn btn-success" style= "width:120px" > Confirmation </button>
</div>
</div>
</form>
</div>
</body>

The main use of the bootstrap framework. Of course, the login page I do is relatively simple, you can also according to their own feelings to write the effect

The third step is the configuration file, first we configure the Web. xml

<?xml version= "1.0" encoding= "UTF-8"?>
<web-app xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://java.sun.com/xml/ns/javaee" xmlns : web= "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee Http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd "id=" webapp_id "version=" 3.0 ">
<display-name>ssh-day02</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!--load Spring listener--
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--load Spring configuration file Applicationcontext.xml--
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--solve no session problem--
<filter>
<filter-name>OpenSessionInviewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionInviewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--Configure Struts---
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--set session effective time--
<!--<session-config>
<session-timeout>1</session-timeout>
</session-config>
</web-app>

Are basic configuration, no session configuration is the back of the two table to be associated with the session early shutdown problem

Fourth step: Create the corresponding package class, so that we could configure the Bean instance in Applicationcontext.xml

Next we start configuring Applicationcontext.xml

The main is to create a database connection pool, create sessionfactory, configure hibernate properties and declarative transactions, AOP (which I don't have, no configuration), and the 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 "><!--integrated Hibernate--
<!--1. Configure 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>
<!--configuration Sessionfactory--
<bean id= "Sessionfactory" class= "Org.springframework.orm.hibernate5.LocalSessionFactoryBean" >
<!--data Source-
<property name= "DataSource" ref= "DataSource" ></property>
<!--Configure Hibernate basic Properties-
<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>
<!--configuring Hibernate mapping Files--
<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 Hibernate transactions--
<bean id= "TransactionManager" class= "Org.springframework.orm.hibernate5.HibernateTransactionManager" >
<property name= "Sessionfactory" ref= "Sessionfactory" ></property>
</bean>
<!--open transactions--
<tx:annotation-driven transaction-manager= "TransactionManager"/>

<!--configuring AOP--

</beans>

Then in the introduction of 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 that the hibernate configuration file is integrated with spring, and all configuration files that do not create hibernate are created separately.

Here, SSH configuration is basically complete. Running successfully is a landing page

Struts2+spring+hibernate implementation of employee management and deletion check function (i) SSH Framework integration

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.