Struts + Spring + Hibernate Upload Download - one

xiaoxiao2021-04-09  365

Upload and download of files

J2EE

Programming is already a very ancient topic, maybe you can take a few famous big elements right away: such as Smartupload,

Apache

FileUpload. But if your project is built in Struts Spring

Hibernate

(Hereinafter referred to as the SSH) framework, these large elements are cumbersome and vicissitudes. SSH provides a simple and convenient document to upload the download program, we only need to configure and supplemented with a small amount of code to solve this problem. . This article will revolve the SSH file upload the subject, tell you how to develop SSH-based Web programs. The SSH frames are currently the latest version: · Struts 1.2 · Spring 1.2.5 · Hibernate 3.0 The database selected by this article is

Oracle

9i, of course, you can transplant it to any database with the BLOB field type without changing the code.

Mysql

SQL Server, etc. Overall implementation of the upload file is saved to the T_FILE table, the T_FILE table structure is as follows:

Figure 1 T_FILE table structure

Where: · file_id: file ID, 32 characters, generated with Hibernate's UUID.HEX algorithm. File_name: File Name. · File_content: file content, corresponding to Oracle's BLOB type. · Remark: File Remarks. The file data is stored on the file_content table field of the blob type, and ORACLELOBHANDLER is used in Spring to handle the LOB field (including Clob and BLOB). Due to the need to reference the specific class of the Oracle data driver in the program, different database processing LOB The difference in the field method, so that the programs are granted on the multi-database transplantation. 1. First, the BLOB field in the data table is declared in the Java domain object as a Byte [] type, not the java.sql.blob type. 2. Data Sheet BLOB fields in the Hibernate persistence map file is org.springframework.ob.hibernate3.support.blobbyteArRayType, which is the user-defined type provided by Spring, not Java.sql.blob. 3. Use org.springframework.jdbc.support.lob.oraclobhandler to handle the BLOB type field of the Oracle database in Spring. With this setting and configuration, we can process the blob field like the general field type of persistence tables. The above is Spring Hibernate persisted the file binary data to the database, and Struts gets the file data submitted by the form by mapping the component of the File type in the form into an org.apache.struts.upload. Formfile property. In summary, we can depict the SSH processing files uploaded by Figure 2:

Figure 2 SSH processing file uploading technical solution

The page uploaded page is shown in Figure 3:

Figure 3 File upload page

File downloaded pages are shown in Figure 4:

Figure 4 File Download Page

The resource structure of the project is shown in Figure 5:

Figure 5 Engineering resource structure

The engineering class is divided into data persistence layers, business layers, and web layers; ApplicationContext.xml under Web-INF for Spring profile, struts-config.xml is the configuration file of Struts, file-upload.jsp Upload the page, file-list.jsp is the file list page. The sequence of this article will be from the data persistence layer -> business layer -> Web layer, explain the development process of the document upload download process. add comment

9:59 |

Fixed link

|

Reference Notice (0)

|

Record it

| Project development based on open source technology

Fixed link

Http://lifejava.spaces.msn.com/blog/cns !a666c33543221640 !117.entry

Building multi-layer application system based on struts Spring Hibernate

This article is a introductory article developed based on Spring-based web applications, with Struts M

VC

The frame, the intermediate layer adopts Spring, and Hibernate is used in the background. This article contains the following: • Configuring Hibernate and Transactions • APPLICATIONCONTEXT.XML with Spring

file

· Establish dependencies between business stratum and DAO · Apply Spring to Struts

Introduction This example is a simple web application, called MyUsers, complete user management operations, including simple

database

Increase, delete, check, this is CRUD (new, access, update, delete) operation. This is a three-layer web application that accesses the business layer through Action (Struts), and the business layer accesses DAO. A brief description of the overall structure of the application. The number on the figure illustrates the process sequence - from the web (useerAction) to the intermediate layer (UserManager), then goes to the Data Access Layer (UserDao) and then returns the result. The true power of the Spring layer is that its declarative transaction processing, help, and persistent layer support (such as hiberate and ibatis) below the steps of completing this example: 1. Install the Eclipse plugin 2.

database

Built in tomorrow. Configure Hibernate and Spring 4. Establish an implementation class 5 for the Hibernate DAO interface. Run the test class, test the DAO's Crud operation 6. Create a processing class, declare transaction 7. Create an Action and Model 8 of the Web layer. Test Class Test CRUD Operation 9 running an action. Create JSP

file

CRUD operation 10 is performed via the browser. Install the Eclipse plugin 1 via the browser checking JSP 1. Hibernate plugin http://www.binamics.com/hibernateSync 2. Spring plugin http://springframework.sourceforge.net/spring-ide/eclipse/UpdateSite/ 3. MyECLIPSE Plugin (Crack Edition) 4. Tomcat plugin. TANGHAN 5. Other plugins include XML, JSP,

Database built table

Create Table App_User (ID Number Nour Null Primary, FirstName Vchar (32), LastName Vchar (32)); New Project New Web Project, newly created directory structure contains new construction

file

Clip Page for Presting JSP

file

, And source

file

Test Test is used to put a JUnit test

file

. At the same time, the package, including struts, hibernate, and Spring into the lib directory. Create a persistence layer O / R mapping 1. Using the Hibernate plugin under src / com.jandar.model

database

Export App_User's.hbm.xml

file

Rename User.hbm.xml

2. Generate User via Hibernate Synchronizer-> Synchronizer File.

Java

file

User object corresponds to

database

APP_USER Table Note: Automatically generated objects under Eclipse

file

Not exactly the same, the same is every object

file

The serializable interface must be implemented, and the TOSTRING and HashCode methods must be implemented.

import java.io.Serializable; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder; import org.apache.commons.lang.builder.ToStringBuilder; import org.apache .commons.lang.builder.ToStringStyle; public class BaseObject implements Serializable {public String toString () {return ToStringBuilder.reflectionToString (this, ToStringStyle.MULTI_LINE_STYLE);} public boolean equals (Object o) {return EqualsBuilder.reflectionEquals (this, o );} public int hashCode () {return HashCodeBuilder.reflectionHashCode (this);}} public class User extends BaseObject {private Long id; private String firstName; private String lastName; / ** * @return Returns the id * / public. Long getId () {return;} / ** * @Param ID the ID to set. * / Public void setid (long id) {this.id = ID;} / ** * @return returns the firstname. * / Public string getfirstname () {return firstname;} / ** * @Param firstname the firstname to set. * / public void setfirstname (STRIN g firstName) {this.firstName = firstName;}. / ** * @return Returns the lastName * / public String getLastName () {return lastName;}. / ** * @param lastName The lastName to set * / public void setLastName Create a DAO Access Object 1 (String lastname) {this.lastname = LastName;}} Create a DAO Access Object 1. New iDao in src / com.jandar.service.dao.

Java

Interface, all DAOs inherit the interface

Package com.jandar.services.dao; public interface idao {}

2. Create new iUserdao under src / com.jandar.service.dao.

Java

interface

Public Interface iUserdao Extends Dao {list getUsers (); user getuser; void saveuse; void removeuser (Integer ID);

This interface provides a method of accessing an object, 3. Enjoy Userdaohiberante under src / com.jandar.service.dao.hibernate.

Java

import java.util.List; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.orm.hibernate.support.HibernateDaoSupport; import com.jandar.model.User ; import com.jandar.service.dao.IUserDAO; public class UserDaoHibernate extends HibernateDaoSupport implements IUserDAO {private log log = LogFactory.getLog (UserDaoHibernate.class); / * (non-Javadoc) * @see com.jandar.dao.IUserDAO # GetUsers () * / public list getusers () {return gethibernateTemplate (). Find ("from user");} / * (non-javadoc) * @see com.jandar.dao.iUserdao # getuser (java.lang.long) * / Public user getuser (Integer User GetUser) Automatic Method Store Return (User) gethateTemplate (). Get (user.class, id);} / * (non-javadoc) * @see com.jandar.dao. Iuserdao # saveuser (com.jandar.model.user) * / public void saveuser (user user) {log.debug ("xxxxxxx"); system.out.println ("YYYY"); gethibernateTemplate (). SaveorUpdate (user) ; If (log.Indebugenabled ()) {log.debug ("userid set to" user.getId ());}} / * (Non-javadoc) * @see com.jandar.dao.iuserdao # Removeuser (java.lang.long) * / public void removeuser (Ibject user = gethibernateTemplate (); load (user.class, id); GethibernateTemplate (). delete (user); if (log.isDebugeload ()) {log.debug ("DEL User" ID);}} The iUserdao interface is implemented in this class and inherits the HibernatedAosupport class. This class is to access, operate objects, and then implement them through hibernate.

database

Operation.

转载请注明原文地址:https://www.9cbs.com/read-132974.html

New Post(0)