Myeclipse integration using struts, hibernate, spring, a sample program

This article is taken from:

 Enterprise applications in java, ejb ejb powerful configuration with the complex and difficult to learn as well known. But in the past, what we do? Only the unpleasant task to learn ejb. Really little, "are aware of the angels fear to tread line of" spirit. With the positive changes in the form now, java continued popularity of open source, allowing developers to java ejb enterprise applications are no longer limited to the field. Here I describe how to use the main source of Spring  ,Hibernate  And the Struts framework to build a lightweight.  
  First, the overall structure description  
  In software engineering, in order to reduce module coupling, enhance the reusability of modules, hierarchical has been a widely adopted method. In fact, stratification also allows developers to focus on a particular level for development, enabling software developers finer division of labor, increase production efficiency (comparable to this point, Ford invented the assembly line production of automotive practice, responsible for all workers in specific parts of the production, final assembly).  
  An enterprise-class java application, is usually divided into the following layers: UI layer, business logic and data persistence layer, the domain object layer. The following briefly describes these layers are:  
1  . UI layer: responsible for user interaction, including: receiving a user's request, the processing results are returned to the user. Here we use struts to UI layer, although these events than Tapestry and JSF presentation layer technology, the driver, Struts design looks a little old fashioned, but it is still the de facto standard performance level, so we still use it.  

2  . Business Logic Layer: The main charge of specific business processes. Through Spring's IOC and AOP to realize various functions connected to the coupling  
3  . Data persistence layer: responsible for dealing with the underlying database. Here we use Spring Hibernate package to simplify the actual encoding operation. The major operation for the CRUD (create  ,read,update,delete  )  
4  . Domain object layer: a specific entity type, such as teachers, students, etc. These specific categories (Note: the need for the database in a Correspondence).
  These are just a brief introduction of each layer and the layers corresponding to the division of open-source framework, to learn more about the layers of a layer of information, need access to struts  ,hibernate,spring  The official website.  
  Second, case study  
  Examples: a website login example of simple, but can explain how the coordination of all levels.  
  Tools: jdk1  .5,eclipse3.2,myeclipse5.0GA,mysql5.0,tomcat5.5  As these tools in the end how to install here I do not say we can not clear search the web search. Here are the steps:  
  Preparatory work: create a test with the mysql database, then create a user table, create two fields username and password. Script as follows  
USE `test`;

`id` int(11) NOT NULL auto_increment,
`username` varchar(30) NOT NULL default '',
`password` varchar(30) default '',
  Open myeclipse the database explore perspective  
  Right-click an empty area on the left, click the new button  
  Click the "configure database driver" to configure the database driver, you must have mysql's jdbc driver not to mysql's official website.  
  Test database configuration was successful.  

1  . Create a new web project in myeclipse  
2  . The introduction of Spring Package  

  Right Project Name  
  Note: This can then add the spring in order to save the relevant package, so a one-time check all the packages. Select the Copy option, it will copy the package to  /WebRoot/WEB-INF/lib  Directory to facilitate the actual deployment of the future.  

3  . Add the hibernate package  
  System will detect the project has added a Spring package, choose to use the Spring configuration file  
  Click next  
  Click next  
  Click next  
  Finally click the Finish button.  

4  Switch to the database explore view, right click on user table "Hibrnate Reverse Engineering"  
  The following pop-up window  
  Click "next"  
  Then click finish  

  View can be seen in the package under the src one more vo of the package name and the package of the four documents  
  In order to more directly display layers, I also built a package called the dao of  ,  And to UserDAO  .java  Moved to the dao of  
  Also note the applicationContext  .xml  In the reference section of the UserDAO also be amended accordingly.  

5  . The establishment of business logic code  

  Create a new package called the service and then create a Service class in which the code is as follows  
package service;
import java.util.List;
import vo.User;
import dao.UserDAO;
public class Service {
private UserDAO userDao;
public UserDAO getUserDao() {
return userDao;
public void setUserDao(UserDAO userDao) {
this.userDao = userDao;
public boolean isValid(User user) {//  Determine whether the user is legitimate
List result = userDao.findByExample(user);
if (result.size() > 0)
return true;
return false;
  At the same time in the applicationContext  .xml  Add the following configuration  
<bean singleton="false">
<property name="userDao">
<ref bean="userDao" />
6  . UI Layer Configuration  
  Add struts package, add the spring with the same method  
  Configuring struts  -config.xml  File, right-click a blank area of a new action  ,actionForm ,jsp

  As set on the forward  ,  Increased succeed to welcome  .jsp,fail  Go to login  .jsp

  WebRoot directory in a welcome  .jsp  File, display the user login information after the success of Welcome.  

6  . To connect the struts and spring  

6  . 1 modified struts  -config.xml  Note that some of the red font  
<forward name="fail" path="/login.jsp" />
<forward name="succeed" path="/welcome.jsp" />

  Longer strut  -config.xml  Add the following code  

<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/classes/applicationContext.xml" />
6.2  Modify applicationContext, add the following
<bean name="/login"
<property name="service">
<ref bean="service" />
6.3  Changes LoginAction  .java  Part of the code, add a service variable, and its get  /set  Method (mainly for Spring IOC). Code:  
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
package web.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import web.form.LoginForm;
import service.Service;
import vo.User;
* MyEclipse Struts Creation date: 09-27-2006
* XDoclet definition:
* @struts.action path="/login" name="loginForm" input="/login.jsp"
* scope="request" validate="true"
* @struts.action-forward name="fail" path="/login.jsp"
* @struts.action-forward name="succeed" path="/welcome.jsp"
public class LoginAction extends Action {
* Generated Methods
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
private Service service;
public void setService(Service service) {
this.service = service;
public Service getService() {
return service;
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
LoginForm loginForm = (LoginForm) form;// TODO Auto-generated method
// stub
User user = new User();
if (service.isValid(user))
return mapping.findForward("succeed");
return mapping.findForward("fail");
  This point has been made a simple login. The remaining job is to package and distribute your app.  

分类:Java 时间:2009-05-17 人气:1129
blog comments powered by Disqus


  • Struts + hibernate + Spring integration method 2010-03-29

    Struts + hibernate + Spring integration method 1, in the project to import spring support, import Jar package are: Spring Core Libraries Spring ORM/DAO/Hibernate3 Libraries Spring AOP Libraries Spring Web Libraries 2, in the Spring configuration file

  • Summary of Struts, Hibernate, Spring Framework 2010-09-29

    Summary of Struts, Hibernate, Spring Framework Hibernate Works and why to use ? Principle : 1. Read and parse the configuration file 2. Read and parse the mapping information, create SessionFactory 3. Open Sesssion 4. Create Service Transation 5. Per

  • Integration of Struts and Spring 2010-11-21

    Some time learning Struts and Spring, the teacher let the integration of Struts and Spring, so try to do a landing today, simulations using SQLSERVER authentication login, the following talk about their own experiences it! ¹¤óûéææäêâ its , to integra

  • Struts, Hibernate, Spring collection of classic interview questions 2010-11-28

    Struts, Hibernate, Spring collection of classic interview questions from time to time just in case. Keywords: struts, hibernate, spring Hibernate works and why to use? Principle: 1. To read and parse the configuration file 2. To read and parse the ma

  • struts + hibernate + spring svn version control of the URL address [transfer] 2010-12-29

    struts+hibernate+spring Version control with svn URL Address [ Go ] 2007-08-09 16:04 struts+hibernate+spring Version control with svn URL Address Transfers from : =================== Str

  • About Struts + hibernate + Spring integration 2009-03-30

    Struts, Spring and hibernate integration: 1, in the spring to support the project to import, import Jar package has: * Spring 2.0 Core Libraries * Spring 2.0 ORM/DAO/Hibernate3 Libraries * Spring 2.0 AOP Libraries * Spring 2.0 Web Libraries 2, in the

  • Graphic MyEclipse configuration struts + hibernate + spring + FreeMarker 2010-07-30

    The past few days to hibernate and spring looked good, the re-think some things struts, decided to look at three integrated together with, the presentation layer using struts + freemarker, business layer using spring, hibernate persistence layer proc

  • struts + hibernate + spring integration fool, to share with you 2010-11-01

    Struts + spring + hibernater First added plus spring struts hibernate add Change the web. Xml file <! - To set the context position applicationContext.xml -> <context-param> <param-name> contextConfigLocation </ param-name> <par

  • Virtual data integration layer Struts2.Hibernate.Spring generic DAO Version 2010.9.27 2010-09-24

    Struts2, Hibernate, Spring integration of generic DAO (my rating: code code development efficiency by 30% error rate reduced by 70%) For most developers, the system is almost the same for each DAO code written so far has become a habit. While all thi

iOS 开发

Android 开发

Python 开发



PHP 开发

Ruby 开发






Javascript 开发

.NET 开发



Copyright (C), All Rights Reserved. 版权所有 黔ICP备15002463号-1

processed in 0.093 (s). 12 q(s)