Web layer implementation 1. The component and interactive flow of the web layer include the main three functions: • Upload files. · List all lists of files that have been uploaded for clicking download. ·download file. Web layer implementation components include 2 JSP pages, 1 ActionForm, and an Action: · file-upload.jsp: page of upload files. File-list.jsp: The list page that has been uploaded. · FileActionform: file-upload.jsp page form corresponding to ActionForm. · FileAction: Inherits the action of org.apache.struts.Actions.dispatchaction, so this action can be in response to different requests via a URL parameter. The interactive flow of these components of the Web layer is shown in Figure 6
Figure 6 Struts flowchart of web layer
Among them, when executing the request uploaded request, FileAction is uploaded after executing the file, forward to the loadAllFile exit, the loadAllFile loads all the uploaded records in the database, then forward to the exit called FileListPage, call file-list.jsp page Displays records that have been uploaded. 2, FileAction Features Struts 1.0 Action has a weak term: an action can only process a request, and a Dispatchction is introduced in Struts 1.1, allowing a method in calling an action through the URL parameter, such as http: // YourWebsite / FileAction .do? method = UPLOAD is called the UPLOAD method in the fileAction. In this way, we can focus some relevant requests to an action, and there is no need to write an Action class for a request operation. But the parameter name is configured in struts-config.xml:
1.
The parameter = "method" of Chain 6 specifies the parameters of the bearer method name, in line 9, we also configure an Action exit that calls FileAction different methods. FileAction has three ways to request a response, which are: • Upload (...): Processing the request for uploading the file. ListAllFile (...): Processes the request for all records in the database table. Download (...): Processes the request for download files. Here we explain these three request processing methods respectively. 2.1 Uploading file Upload request processing method is very simple, in addition, is to obtain a business layer processing class FileService from the Spring container, call its Save (FileActionform Form) method upload file, as shown below: 1. Public class fileAction2. . extends DispatchAction3 {4. // save the upload to the database 5. public ActionForward upload (ActionMapping mapping, ActionForm form, 6 HttpServletRequest request, 7 HttpServletResponse response..) 8 {9. FileActionForm fileForm = (FileActionForm) form.; 10. FILESERVICE FILESERVICE = GetFileService (); 11. FileService.save (fileForm); 12. Return mapping.forward ("loadAllFile"); 13.} 14. Obtain FileService object from the Spring container 15. Private FileService getFileService .) 16 {17. ApplicationContext appContext = WebApplicationContextUtils.18 getWebApplicationContext (this.getServlet () getServletContext ());... 19 return (FileService) appContext.getBean ( "fileService");. 20} 21 ... 22}..
Since FileAction other two request processing methods also need to get a FILESERVICE instance from the Spring container, we specially provide a getFileService () method (line 15-12). A principle of reconstruction is: "There are repetitive expressions in the discovery code, extract it as a variable; found that there are repetitive code segments in the class, extract it as a method; found that there is the same method in different classes, will It is extracted as a class. " In a real system, there are often multiple Actions and multiple Service classes. At this time, a better settings is to provide a tool class that gets all service implementations, so you can block the Spring Service configuration information. In the class, otherwise the configuration name of the service is scattered in the procedures, and maintaining maintaining is very poor. 2.2 Lists all uploaded file listAllFile methods Call the Servie layer method to load all records in the T_FILE table, and save it in the Request domain, then Forward to the list page:
..... 1. public class FileAction2 extends DispatchAction3 {4. ... 5 public ActionForward listAllFile (.. ActionMapping mapping, ActionForm form, 6 HttpServletRequest request, 7 HttpServletResponse response) 8 throws ModuleException9 {10. FileService fileService = getFileService (); 11. List fileList = fileservice.getAllFile (); 12. Request.setttribute ("filelist", filelist); 13. Return mapping.forward ("filelistpage"); 14.} 15.} File-list.jsp page Using Struts Label showcases that are saved in the Request domain:
1. <% @ Page ContentType = "Text / HTML; Charset = GBK"%> 2. <% @ Taglib Uri = "/ Web-INF / STRUTS-Logic.TLD" prefix = "logic"%> 3. <% @Taglib URI = "/ Web-inf / struts-bean.tld" prefix = "bean"%> 4. 5.
6.