I have some fields in jsp including file field. When I submit my form, and then if validation failed for one fields, after setting it in ActionErrors
, when response comes back to JSP, all fields retain their values except File field.
My Code:
<s:form autocomplete="off" action="carrier-profile" id="id_form_carrier" namespace="/usermgmt"
enctype="multipart/form-data" method="POST">
<s:text name="txt.carrier.url"></s:text><span class="astrcs">*</span></label> <span>
<s:textfield type="text" name="carrier.carrierUrl" class="form-control" maxlength="255" tabindex="12"></s:textfield>
<s:file name="carrier.file" class="upload-input" id="imgInp" accept="image/*" tabindex="12"></s:file>
<s:submit/>
</s:form>
My Action Class:
public class CarrierProfileAction extends BaseSecureAction {
private static final Logger LOG = LogManager.getLogger(CarrierProfileAction.class);
/**
*
*/
private static final long serialVersionUID = -7296331027656555878L;
private static final String RESULT_CARRIER_SEARCH = "carrier-search";
// private static final String RESULT_LOGO_PREVIEW = "logo-preview";
// private static final String RESULT_LOGO_UPDATE = "logo-update";
public static final String CARRIER_ID = "carrierId";
public static final String CARRIER_ID_NBR = "carrierIdNbr";
public static final String RECORD_MODE = "mode";
private static final String IMAGE = "Image";
private CarrierVO carrier = new CarrierVO();
// private File file;
private Long carrierId;
private Long carrierIdNbr;
private Long selectedCarrier;
private boolean imagePreviewToBeShown;
private String mode;
private byte[] imagePreview;
private String userImageFileName = "";
private boolean validImage;
InputStream imageInputStream;
}
Above code, carrier
object has all its and its fields getter setter. No issue in that.
all the other fields name="carrier.xxx"
are retaining except file.
You can't retain a file field due browser restriction rules. But you can use Ajax via Struts2 jQuery plugin
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="sj" uri="/struts-jquery-tags"%>
<html>
<head>
<sj:head/>
</head>
<body>
<s:form autocomplete="off" action="carrier-profile" id="id_form_carrier" namespace="/usermgmt"
enctype="multipart/form-data" method="POST">
<s:text name="txt.carrier.url"></s:text><span class="astrcs">*</span></label> <span>
<s:textfield type="text" name="carrier.carrierUrl" cssClass="form-control" maxlength="255" tabindex="12"></s:textfield>
<s:file name="carrier.file" cssClass="upload-input" id="imgInp" accept="image/*" tabindex="12"></s:file>
<sj:submit value="Submit" />
</s:form>
</body>
</html>