Home Java javaTutorial How to implement the function of uploading single file and multiple files in spring webflow in java development

How to implement the function of uploading single file and multiple files in spring webflow in java development

May 22, 2023 pm 03:25 PM
java spring webflow

Upload a single file

Preparation

1. If you use spring security in your project, refer to the previous article and use The second method in the previous article, and remove the MultipartFilter (if configured), otherwise you will not get the file

2. Variables in the process (such as variables defined with the var tag) need to implement the Serializable interface .

Implementation process

Add the following dependencies to the pom.xml file:

<!-- 支持文件上传 -->
  <dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.2.1</version>
  </dependency>
  <dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.4</version>
  </dependency>

In spring-servlet.xml (Spring MVC configuration file) Add the file upload parser:

<!-- 文件上传解析器-->
   <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <!-- one of the properties available; the maximum file size in bytes -->
    <property name="maxUploadSize" value="10485760"/>
  </bean>

Entity class, remember to implement the Serializable interface, the attribute type is MultipartFile:

@Component
public class GoodsEntity implements Serializable{
  private static final long serialVersionUID = 1L;
  private MultipartFile images;
  public MultipartFile getImages() {
    return images;
  }
  public void setImages(MultipartFile images) {
    this.images = images;
  }
}

Process definition code, nothing special:

<?xml version="1.0" encoding="UTF-8"?>
<flow xmlns="http://www.springframework.org/schema/webflow"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.springframework.org/schema/webflow
   http://www.springframework.org/schema/webflow/spring-webflow-2.0.xsd">
     <var name="goods" class="com.huanle.model.entity.GoodsEntity"/>
     <view-state id="viewfirst" view="/views/user/releasegoods/release_first.jsp" model="goods">
      <transition on="submit" to="viewsecond"></transition>
     </view-state>
     <view-state id="viewsecond" view="/views/user/releasegoods/second.jsp" model="goods">
      <transition on="submit" to="performReleaseGoodsAction"></transition>
     </view-state>
     <action-state id="performReleaseGoodsAction" >
       <evaluate expression="goodsService.save(goods)"></evaluate>
       <transition to="returntouserindex"></transition>
     </action-state>
     <end-state id="returntouserindex" view="/views/user/seller/index.jsp"></end-state>
     <global-transitions>
      <transition on="cancel" to="returntouserindex"></transition>
     </global-transitions>
</flow>

Upload form code, no special configuration required:

<form:form action="${flowExecutionUrl}&_eventId=submit&${_csrf.parameterName}=${_csrf.token}" method="post" commandName="goods" enctype="multipart/form-data">
  <input type="hidden" name="_flowExecutionKey" value="${flowExecutionKey}"/>
 商品图片:<form:input id="images" path="images" type="file" multiple="multiple" />
 <input type="submit" >
</form:form>

That’s it

Upload multiple files

Upload a single file can be uploaded in front This can be achieved with minor modifications to a single file.

Implementation

First of all, the entity class needs to be modified and List is used to store multiple files:

@Component
public class GoodsEntity implements Serializable{
  private static final long serialVersionUID = 1L;
  private List<MultipartFile> images;
  public List<MultipartFile> getImages() {
    return images;
  }
  public void setImages(List<MultipartFile> images) {
    this.images = images;
  }
}

The upload form also needs to be modified:

<form:form action="${flowExecutionUrl}&_eventId=submit&${_csrf.parameterName}=${_csrf.token}" method="post" commandName="goods" enctype="multipart/form-data">
  <input type="hidden" name="_flowExecutionKey" value="${flowExecutionKey}"/>
商品图片:<form:input path="images" type="file" multiple="multiple"/>
<input type="submit" value="提交">
</form:form>

Just add a multiple="multiple" attribute.

The above is the detailed content of How to implement the function of uploading single file and multiple files in spring webflow in java development. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Beginner's Guide to RimWorld: Odyssey
1 months ago By Jack chen
PHP Variable Scope Explained
4 weeks ago By 百草
Tips for Writing PHP Comments
3 weeks ago By 百草
Commenting Out Code in PHP
3 weeks ago By 百草

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1509
276
edge pdf viewer not working edge pdf viewer not working Aug 07, 2025 pm 04:36 PM

TestthePDFinanotherapptodetermineiftheissueiswiththefileorEdge.2.Enablethebuilt-inPDFviewerbyturningoff"AlwaysopenPDFfilesexternally"and"DownloadPDFfiles"inEdgesettings.3.Clearbrowsingdataincludingcookiesandcachedfilestoresolveren

How to implement a simple TCP client in Java? How to implement a simple TCP client in Java? Aug 08, 2025 pm 03:56 PM

Importjava.ioandjava.net.SocketforI/Oandsocketcommunication.2.CreateaSocketobjecttoconnecttotheserverusinghostnameandport.3.UsePrintWritertosenddataviaoutputstreamandBufferedReadertoreadserverresponsesfrominputstream.4.Usetry-with-resourcestoautomati

Deploying a Java Application to Kubernetes with Docker Deploying a Java Application to Kubernetes with Docker Aug 08, 2025 pm 02:45 PM

Containerized Java application: Create a Dockerfile, use a basic image such as eclipse-temurin:17-jre-alpine, copy the JAR file and define the startup command, build the image through dockerbuild and run locally with dockerrun. 2. Push the image to the container registry: Use dockertag to mark the image and push it to DockerHub and other registries. You must first log in to dockerlogin. 3. Deploy to Kubernetes: Write deployment.yaml to define the Deployment, set the number of replicas, container images and resource restrictions, and write service.yaml to create

VS Code shortcut to focus on explorer panel VS Code shortcut to focus on explorer panel Aug 08, 2025 am 04:00 AM

In VSCode, you can quickly switch the panel and editing area through shortcut keys. To jump to the left Explorer panel, use Ctrl Shift E (Windows/Linux) or Cmd Shift E (Mac); return to the editing area to use Ctrl ` or Esc or Ctrl 1~9. Compared to mouse operation, keyboard shortcuts are more efficient and do not interrupt the encoding rhythm. Other tips include: Ctrl KCtrl E Focus Search Box, F2 Rename File, Delete File, Enter Open File, Arrow Key Expand/Collapse Folder.

Fixed: Windows Update Failed to Install Fixed: Windows Update Failed to Install Aug 08, 2025 pm 04:16 PM

RuntheWindowsUpdateTroubleshooterviaSettings>Update&Security>Troubleshoottoautomaticallyfixcommonissues.2.ResetWindowsUpdatecomponentsbystoppingrelatedservices,renamingtheSoftwareDistributionandCatroot2folders,thenrestartingtheservicestocle

What is the process of serialization for a Java object? What is the process of serialization for a Java object? Aug 08, 2025 pm 04:03 PM

Javaserializationconvertsanobject'sstateintoabytestreamforstorageortransmission,anddeserializationreconstructstheobjectfromthatstream.1.Toenableserialization,aclassmustimplementtheSerializableinterface.2.UseObjectOutputStreamtoserializeanobject,savin

How to use a while loop in Java How to use a while loop in Java Aug 08, 2025 pm 04:04 PM

AwhileloopinJavarepeatedlyexecutescodeaslongastheconditionistrue;2.Initializeacontrolvariablebeforetheloop;3.Definetheloopconditionusingabooleanexpression;4.Updatethecontrolvariableinsidethelooptopreventinfinitelooping;5.Useexampleslikeprintingnumber

python numpy linear algebra example python numpy linear algebra example Aug 07, 2025 pm 04:52 PM

NumPy is the core library for scientific computing in Python. It is good at handling linear algebra operations and provides efficient ndarray arrays and functions in the numpy.linalg module. 1. Use np.linalg.solve(A,b) to solve the linear equation system Ax=b to obtain the solution vector x; 2. Matrix transposition is implemented through A.T; 3. Matrix multiplication can be used to np.dot(A,B) or A@B; 4. Matrix inverse is calculated by np.linalg.inv(A), and the matrix needs to be reversible; 5. The determinant is given by np.linalg.det(A); 6. The eigenvalue and eigenvector are obtained through np.linalg.eig(A), and the eigenvector has been normalized;

See all articles