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

This article is taken from: http://kenter1643.javaeye.com/blog/109874

 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  
DROP DATABASE IF EXISTS `test`;
CREATE DATABASE `test`
USE `test`;

CREATE TABLE `user` (
`id` int(11) NOT NULL auto_increment,
`username` varchar(30) NOT NULL default '',
`password` varchar(30) default '',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gb2312;
  Open myeclipse the database explore perspective  
 bedd7dfaf428726f007e52849e34aba8 
  Right-click an empty area on the left, click the new button  
 941adab370d439bc73c6fc2b76d7e290 
  Click the "configure database driver" to configure the database driver, you must have mysql's jdbc driver not to mysql's official website.  
 d74dfcf64878d6efc775512ed54edbfc 
  Test database configuration was successful.  

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

  Right Project Name  
 5ed6fbd62fe1e9a7c3a8c9bc97caac3c 
 6500a163876c51bb2338e1488db22600 
  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  
 eb867deb5187360f01da3f7122f00239 
  System will detect the project has added a Spring package, choose to use the Spring configuration file  
 33b596afe36b9f6f2fe148afc8a2c367 
  Click next  
 c1880208934ea8059c400b235dffeca1 
  Click next  
 479de86b9d95293eb552aabc0bd11122 
  Click next  
 d83197adedacb67220e62b975301cb3d 
  Finally click the Finish button.  

4  Switch to the database explore view, right click on user table "Hibrnate Reverse Engineering"  
 b83e22d90ac2a5092f31b60f804735a8 
  The following pop-up window  
 7d6957d10900c4427262374b0adb7d62 
  Click "next"  
 febca31d9f5c395ffbe09dac11c5f4c0 
  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  
 fb9c674c2655f9a508352ed5c7f29201 
  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  
 1ba301a403669b075c44011053b2933a 
  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;
else
return false;
}
}
  At the same time in the applicationContext  .xml  Add the following configuration  
<bean singleton="false">
<property name="userDao">
<ref bean="userDao" />
</property>
</bean>
  
6  . UI Layer Configuration  
  Add struts package, add the spring with the same method  
 0bd885bc7b1c04d152d0f622cddd60c4 
  Configuring struts  -config.xml  File, right-click a blank area of a new action  ,actionForm ,jsp
 33a4b1181e7492e1a6bce19ead8f13d9 
 47dad78c047ebaa7ffb4b0b2e78fa1f1 
 cc5229b3bee7d4812edf665d3cbc8293 
  Next  

  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  
<action
attribute="loginForm"
input="/login.jsp"
name="loginForm"
path="/login"
scope="request"
type="org.springframework.web.struts.DelegatingActionProxy">
<forward name="fail" path="/login.jsp" />
<forward name="succeed" path="/welcome.jsp" />
</action>

  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" />
</plug-in>>
6.2  Modify applicationContext, add the following
<bean name="/login"
singleton="false">
<property name="service">
<ref bean="service" />
</property>
</bean>
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();
user.setPassword(loginForm.getPassword());
user.setUsername(loginForm.getUserName());
if (service.isValid(user))
return mapping.findForward("succeed");
else
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 人气:1131
本文关键词:
分享到:
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 :http://hi.baidu.com/eako/blog/item/2b33a686d36acf3e66096e1d.html =================== 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 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

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