如何在 Struts 2 中验证文件上传

2022-01-16 00:00:00 validation file-upload java jsp struts2

上传videoAction.java

private File id;
private String title;
private String url;
private String name="";
private String message="";
private String idContentType;  
private String idFileName;

public String getIdContentType() {  
    return idContentType;  
}  

public void setIdContentType(String idContentType) {  
    this.idContentType = idContentType;  
}  
public String getIdFileName() {  
    return idFileName;  
}  
public void setIdFileName(String idFileName) {  
    this.idFileName = idFileName;  
}  
public void setServletRequest(HttpServletRequest servletRequest) {  
    this.servletRequest = servletRequest;  

}
public String getMessage() {
    return message;
}
public void setMessage(String message1) {
    this.message = message;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public File getId() {
    return id;
}
public void setId(File id) {
    this.id = id;
}
public String getTitle() {
    return title;
}
public void setTitle(String title) {
    this.title = title;
}
public String getUrl() {
    return url;
}
public void setUrl(String url) {
    this.url = url;
}

struts.xml:

<action name="uploadvideo" class="com.myapp.ysrcptv.UploadvideoAction">
    <interceptor-ref name="fileUpload">   
        <param name="allowedTypes">video/mp4,video/ogg,video/webm</param>  
    </interceptor-ref>  
    <interceptor-ref name="defaultStack"></interceptor-ref>  

    <result>${url}</result>
    <result name="login">adminlogin.jsp</result>
    <result name="input">${url}</result>
</action>

上传视频.jsp:

<s:form cssClass="form" action="uploadvideo" method="post" validate="false" enctype="multipart/form-data">
<s:file cssClass="input" name="id" value="" placeholder="Video"></s:file>
<s:textfield cssClass="input" name="title" value="" placeholder="Video Title"></s:textfield>
<input type="hidden" name="name" value="gellery pic"/>
<input type="hidden" name="url" value="uploadvideos.jsp"/>
<s:submit cssClass="btn" value="Upload"></s:submit>
<div class="formdiv"><s:property value="message"/></div>
</s:form>

上传videoAction-validation.xml:

<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.3//EN"
        "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">
<validators>

   <field name="id">
        <field-validator type="requiredstring">            
            <message>File is required.</message>
        </field-validator>
    </field>


问题:只有服务器端文件 id 验证不起作用.即使我选择了一个文件,它也会显示验证消息 File is required.其余验证工作正常.这里我放一些东西.在它工作之前.重新启动我的服务器后,此验证不起作用.


Problem: Only server side file id validation is not working. Even if I selects a file its also showing validation message File is required. Remaining validations are working perfectly. Here I'm placing some stuff. Before its worked. After restarting my server this validation not working.

推荐答案

你使用了错误的验证器."requiredstring" 验证器用于验证文本字段.您可以使用 "required" 验证器来验证字段是否为 null.

You have used wrong validator. "requiredstring" validator is used to validate textfields. You can use "required" validator that validates the field to be not null.

<field name="id">
    <field-validator type="required">            
        <message>File is required.</message>
    </field-validator>
</field>

相关文章