Struts + Spring + Hibernate Upload Download - Third

xiaoxiao2021-04-09  410

Business Layer 1, Business Layer Interface "Interface Instead of Single Programming" is the programming principle recommended by Spring spare no effort, which has also been accepted for most developers; in addition, JDK's dynamic agent is only valid for the interface, otherwise The subclass of the target class must be generated using CGLIB. We define an interface for the business class in accordance with Spring Advocacy: Code 7 Business Layer Operation Interface

1. Public interface fileservice2. {3. Void Save (fileActionform fileform); // Save the submitted upload file to the data table 4. List getAllFile (); // Get the T_File record 5. Void Write (Outputstream OS, String fileID; // Write the file data of a file to the output stream 6. String getFileName (String fileID); // Get the file name 7.}

Save the Save (FileActionform fileform), save the upload file encapsulated in the fileform into the database, here we use FileActionForm as a method, FileActionForm is a form data object for the web layer, which encapsulates the data submitted. Use the FileActionform as the interface of the business layer, which is equivalent to propagating the web layer into the business layer, so that the business layer is bound to a particular web layer implementation technology, according to the viewpoint of the hierarchical model, this is a kind The anti-mold block design, but in the "general" business system does not need to provide a variety of UI interfaces, the system web layer will switch to another to realize technology, so the author feels that it is not necessary for this business layer. To engage in an additional isolation layer in the adjustment of the oversight of the tuning layer, waste the raw materials, but also to make the system too complicated, "simple" is always the largest principle compared to other principles. GetFile () is responsible for getting all the records of the T_FILE table to display on the web page. GetFileName (String FileID) and Write (OutputStream OS, String FileID) are used to download a specific file. The specific call is to transmit the Web layer to the Write (OutputStream OS, String FileID) interface, and the business layer outputs the file data directly into this response stream. Please refer to the specific implementation! No reference source is found. Section download file section. 2

1. ... 2. Public Class FileServiceImpl3. Implements FileService4. {5. Private TfileDao TfileDao; 6. Public Void Save (FileActionform Fileform) 7. {8. Tfile Tfile = New TFile (); 9. Try10. {11. TFile. SetFileContent (). getFileData ()); 12.} 13. catch (filenotfoundexception ex) 14. {15. Throw new runtimeException (ex); 16.} 17. Catch (ioexception ex) 18. {19. Throw new runtimeException (ex); 20.} 21. TFile.GetFileName (fileform.getfileContent (). getFileName ()); 22. TFile.SetRemark (fileform.getremark ()); 23. TFiledao.save (tfile); 24 In the Save (FileActionform fileform) method, the two steps are completed: First, like the water in the bucket, pour the data in the FileActionForm object into the TFile object; two, call TFiledao saves data. It is necessary to pay special attention to the 11th line of the code. FileActionform's filecontent attribute is org.apache.struts.upload.formfile type, FormFile provides a convenient method getFileData (), you get binary data for files. By interpreting the original code of class DiskFile, we may know that the FormFile itself does not caches the file data, and only the data is obtained from the disk file input stream only when getting getFileData (). Since FormFile uses stream reading mode to obtain data, it does not have all data of the cached file, so there is no problem for uploading a large volume of files; however, due to the data persistent layer TFile uses Byte [] to cache data, so It is not suitable for processing a large volume of files (such as 100m), for large volume files, still need to use java.sql.blob types to process regular streaming operations. In addition, the file name of the upload file can be obtained by the FileForm's getFileName () method, as shown in the 21st line code. The implementation of the Write (OutputStream OS, String FileID) method, such as code 9: Code 9 Business Interface Implementation of Write ()

... 1. ... 2 public class FileServiceImpl3 implements FileService4 {5.6 public void write (OutputStream os, String fileId) 7 {8. Tfile tfile = tfileDAO.findByFildId (fileId);.... 9 try10 {11. os.write (TFile.GetFileContent ()); 12. Os.flush (); 13.} 14. Catch (IOException EX) 15. {16. Throw new runtimeException (ex); 17.} 18.

Write (OutputStream OS, String FileID) is also simply divided into two steps, first, based on the FileID load table record, then write FileContent into the output stream. 3, Spring Transaction Configuration Next, let's see how to configure declarative transactions for FILESERVICE in the Spring profile 1. 2. ... 3. 5. 6. 7. 8. 10. 11. 12. 13. PropAgation_required, readonly 14. PropAgation_required, readonly 15. Prop Analysis_Required 16. propagation_required, readonly 17. 19. 19. 20. 21> 21> 22. 23. 24. 25. 26. 27.

Spring's transaction configuration includes two parts: 1. Define transaction manager TransactionManager, use HibernateTransactionManager to implement transaction management; Second, definition of each business interface, actually TXProxyTemplate and FileService are the relationship between parent-child nodes, which can be defined by TXProxyTemplate The content is merged into the FILEService. Since our system only has a business interface needs to be defined, some of its definitions are not very small in the parent TXProxyTemplate, but for real systems, there are often numerous business interfaces You need to define that the common part of these service interface definitions is extracted into a parent node, and then the association is made through the Parent in the child node, it can greatly simplify the configuration of the service interface. Parent Node TXProxyTemplate Injects Transaction Manager, and also defines a method of business interface transaction management (allowing matching declarations through wildcard, such as the first two interface methods), some interface methods only read data on data, while others The interface method needs to be changed to data. For the former, you can identify the readonly, which is conducive to the improvement of the performance performance, it is necessary to note that the bean defined by the parent class node is only the abstraction of the sub-node configuration information, and it is not specifically labeled. For abstract = "true", as shown in Chapter 8. FileService is injected into the transactional agents as a target class, while the FILEService implementation TFiledao instance required to import the TFiledao Bean defined in Section 3.2.

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

New Post(0)