【发布时间】:2014-02-04 02:19:59
【问题描述】:
假设以下动作类可以处理文件上传。
@Namespace("/admin_side")
@ResultPath("/WEB-INF/content")
@ParentPackage(value="struts-default")
public final class CategoryAction extends ActionSupport implements Serializable, ValidationAware, ModelDriven<Category>
{
private static final long serialVersionUID = 1L;
private File fileUpload;
private String fileUploadContentType;
private String fileUploadFileName;
private Long editId;
//Getters and setters.
@Validations(
requiredFields={
@RequiredFieldValidator(fieldName="fileUpload", type= ValidatorType.FIELD, message="Uploading an image is mandatory.")})
@Action(value = "AddCategory",
results = {
@Result(name=ActionSupport.SUCCESS, type="redirectAction", params={"namespace", "/admin_side", "actionName", "Category", "currentPage", "${currentPage}", "message", "${message}", "editId", "${editId}", "status", "${status}"}),
@Result(name = ActionSupport.INPUT, location = "Category.jsp")},
interceptorRefs={
@InterceptorRef(value="defaultStack", params={"params.acceptParamNames", "editId, fileUpload, fileUploadContentType, fileUploadFileName, catId, catName, visible, latest, currentPage, rowCount, totalPages, status", "validation.validateAnnotatedMethodOnly", "true"})
})
public String insertOrUpdate(){
//Do either insert or update based on editId.
return ActionSupport.SUCCESS;
}
@Action(value = "Category",
results = {
@Result(name=ActionSupport.SUCCESS, location="Category.jsp"),
@Result(name = ActionSupport.INPUT, location = "Category.jsp")},
interceptorRefs={
@InterceptorRef(value="defaultStack", params={"params.acceptParamNames", "id, currentPage, rowCount, totalPages, message, status", "validation.validateAnnotatedMethodOnly", "true", "validation.excludeMethods", "load"})})
public String load() throws Exception{
//This method is just required to return an initial view on page load.
return ActionSupport.SUCCESS;
}
}
我想仅在 editId 为 null 时验证 fileUpload(强制)。如果将其初始化为 Long 值,则不应执行和排除此验证。 editId 在编辑 HTML 表中的行时作为查询字符串参数提供。
有没有办法做到这一点?
【问题讨论】:
标签: java validation struts2