Java framework --- struts development, java framework --- struts

Source: Internet
Author: User

Java framework --- struts development, java framework --- struts
1. File Upload

  • Master the basic operation principles of Struts file upload;
  • You can use Struts to upload files.
File Upload is an essential operation in a WEB project. We have introduced the use of the two Upload components: SmartUpload and FileUpload, compared with the two components, SmartUpload is easier, while FileUpload is more complex. Fortunately, it also supports file upload in Struts, and the component used for uploading is FileUpload, however, it is much easier to use FileUpload. If you want to use the FileUpload component correctly to complete the operation, you still need to use the

No.

Method

Type

Description

1

Public byte [] getFileData () throws FileNotFoundException, IOException

Normal

Get the size of the uploaded file

2

Public InputStream getInputStream () throws FileNotFoundException, IOException

Normal

Get the input stream of the uploaded file

3

Public int getFileSize ()

Normal

Get the size of the uploaded file

4

Public String getFileName ()

Normal

Get the name of the uploaded file

5

Public String getContentType ()

Normal

Obtain the type of the uploaded file

Define form -- upload. jsp
<% @ Page language = "java" pageEncoding = "GBK" %> <% @ taglib uri = "http://www.mldn.cn/struts/bean" prefix = "bean" %> <% @ taglib uri = "http://www.mldn.cn/struts/html "prefix =" html "%> <% @ taglib uri =" http://www.mldn.cn/struts/logic "prefix =" logic "%> Define ActionForm and receive the uploaded file-UploadForm. java
Package org. lxh. struts. form; import javax. servlet. http. httpServletRequest; import org. apache. struts. action. actionErrors; import org. apache. struts. action. actionForm; import org. apache. struts. action. actionMapping; import org. apache. struts. upload. formFile; public class UploadForm extends ActionForm {private FormFile photo; // receives the Upload File public ActionErrors validate (ActionMapping mapping, HttpServletRequest request) {// return null is not verified for the moment ;} public void reset (ActionMapping mapping, HttpServletRequest request) {} public FormFile getPhoto () {return photo;} public void setPhoto (FormFile photo) {this. photo = photo ;}}
Define Action -- UploadAction. java
Public class UploadAction extends Action {public ActionForward execute (ActionMapping mapping, ActionForm form, HttpServletRequest request, response) {UploadForm uploadForm = (UploadForm) form; IPTimeStamp its = new IPTimeStamp (request. getRemoteAddr (); // automatically generate the file name String fileName = its. getIPTimeRand () + ". "+ uploadForm. getPhoto (). getFileName (). split ("\\. ") [uploadForm. getPhoto (). GetFileName (). split ("\\. "). length-1]; // generate the File name File outFile = new File (super. getServlet (). getServletContext (). getRealPath ("/") + "upload" + File. separator + uploadForm. getPhoto (). getFileName (). split ("\\. "); // output file path try {OutputStream output = new FileOutputStream (outFile); // file output byte data [] = new byte [1024]; // received file int temp = 0; // end judgment while (temp = uploadForm. getPhoto (). getInputStre Am (). read (data, 0, 1024 ))! =-1) {// Save the output. write (data); // save the file} output. close (); // close the output} catch (Exception e) {e. printStackTrace (); // error output} return null ;}}
Configure struts-config.xml files
<struts-config>    <form-beans>        <form-bean name="uploadForm" type="org.lxh.struts.form.UploadForm" />    </form-beans>    <action-mappings>            <action attribute="uploadForm" input="/upload.jsp"            name="uploadForm" path="/upload" scope="request"            type="org.lxh.struts.action.UploadAction" />    </action-mappings></struts-config>

In this way, you can easily upload files.

2. How to configure dynamic ActionForm? In normal operations, each Action must correspond to an ActionForm, but this will cause too many actionforms. To solve this problem, Struts provides a dynamic ActionForm, to solve the problem of too many actionforms. The operation class corresponding to the dynamic ActionForm is org. apache. struts. action. DynaActionForm. When using this class, you do not need to define a specific class, just need to configure it in the struts-config.xml file. Configure a dynamic ActionForm with two properties -- struts-config.xml
<struts-config>    <form-beans>        <form-bean name="newsForm"             type="org.apache.struts.action.DynaActionForm">            <form-property name="title" type="java.lang.String">            </form-property>            <form-property name="content" type="java.lang.String">            </form-property>        </form-bean>    </form-beans>    <action-mappings>            <action attribute="newsForm" input="/ch17/news.jsp"            name="newsForm" path="/ch17/news" scope="request"            type="org.lxh.struts.action.NewsAction" />    </action-mappings></struts-config>
Define input form -- news. jsp
<% @ Page language = "java" pageEncoding = "GBK" %> <% @ taglib uri = "http://www.mldn.cn/struts/bean" prefix = "bean" %> <% @ taglib uri = "http://www.mldn.cn/struts/html "prefix =" html "%> <% @ taglib uri =" http://www.mldn.cn/struts/logic "prefix =" logic "%> Receiving parameter -- NewsAction. java
Package org. lxh. struts. 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 org. apache. struts. action. dynaActionForm; public class NewsAction extends Action {public ActionForward execute (ActionMapping mapping, ActionForm form, HttpServletRequest request, response) {DynaActionForm dynaForm = (DynaActionForm) form; String title = dynaForm. getString ("title"); // obtain the title input String content = dynaForm. getString ("content"); // get the content input content System. out. println ("title -->" + title); // output the title content System. out. println ("content -->" + content); // output content return null ;}}
Dynamic ActionForm relies on configuration to solve the problem of too many actionforms, but ActionForm is still the biggest weakness of Struts.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.