简体   繁体   中英

Struts with FormFile filed

I am doing enhancement for a application which build on struts 1.1 There i need to have a file upload field which i use,

<html:file  size="40" property="strUploadNoFile" />

Then i add the property to struts configaration file form bean which use the DynaValidatorForm as below,

<form-property name="strUploadNoFile" type="org.apache.struts.upload.FormFile"/>

Also i add the form encript type as multipart/form-data in the jsp too.

The code works fine and i can read the content of uploaded file successfully.

But the application here design in a was that couple of actions map to same form bean. So when i come from a request path which a file upload field is not present i get the exception,

Cannot assign value of type 'java.lang.String' to property 'strUploadNoFile' of type 'org.apache.struts.upload.FormFile'

The reason would be because I am conditionally switch the encript as either default application/x-www-form-urlencoded or multipart/form-data. So when i come from the path where file upload is not present, my encript type would be application/x-www-form-urlencoded. But as i mentioned above the application structure use common form bean for different actions still the bean property strUploadNoFile which is a FormFile remains in the form properties but the encript type in the jsp is multipart/form-data. I assume this is the cause for the exception.

I just need to know whether i can dynamically add from properties which are hard coded in that xml file? So that i can only add the FileForm property from a request path which has the file upload field. Redesign the application architecture for each request has seperate form bean is a high effort in this case.

If your actions use the same form bean then they'll expect the same form elements.

The reason you get the error is because, as you noted, you also mix form encodings.

IMO having one form to serve all needs completely defeats the purpose, and is using the framework counter to how it's designed to be used–hence the problems.

The easiest solution might be to create a custom request processor that does something "useful" when it encounters a non-multipart form, but I'm not exactly sure what to do.

All this said, you wouldn't need to architect a new form for each request, you only need to create one form bean with a file, and one without . IMO doing so is wrong , but only on from the "let's not mislead everybody and make it difficult to think about the application" front, not on the technical side.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM