我在jsp中有一些字段,包括文件字段。当我提交表单,然后如果一个字段的验证失败,在ActionErrors
中设置后,当响应返回到JSP时,所有字段都保留其值除了文件字段。
我的代码:
<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>
我的行动类:
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;
}
上面的代码,carrier
对象包含其所有及其字段的getter setter。没问题。
除文件外,所有其他字段name="carrier.xxx"
都保留。
答案 0 :(得分:1)
由于浏览器限制规则,您无法保留文件字段。但是你可以通过Struts2 jQuery插件使用Ajax
<%@ 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>