Struts2文件上传不起作用

时间:2015-08-09 12:40:14

标签: java validation jsp file-upload struts2

UploadvideoAction.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>

uploadvideos.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>

UploadvideoAction-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。剩余的验证工作正常。我在这里放一些东西。在它工作之前。重新启动服务器后,此验证无效。

1 个答案:

答案 0 :(得分:0)

您使用了错误的验证器。 "requiredstring"验证程序用于验证文本字段。您可以使用验证字段的"required"验证程序不是null

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