java - Struts2 Bootstrap 插件客户端使用文件验证

标签 java validation jsp struts2 struts2-bootstrap-plugin

我使用 Struts2-Bootstrap-Plugin 版本 2.0.4 进行客户端验证。我使用 bootstrapValidation 作为验证函数,并将 jsonValidationWorkflowStack 添加到我的 struts 操作中。对于大多数事情,这按预期工作,将我的表单数据发送到服务器并执行我的服务器验证,而无需实际执行完整的发布。但是,它不适用于文件。如果页面上有 s:file,它似乎会被忽略。当验证方法执行时它始终为 null。如果我绕过插件的客户端验证,文件将正确提交并经过验证。我缺少什么文件才能正常工作?谢谢。

我的 Struts.xml

    <action name="testValidateSave" class="webapp.action.TestValidationAction" method="save" >
        <interceptor-ref name="jsonValidationWorkflowStack"/>

        <result name="success">/WEB-INF/pages/testValidate.jsp</result>
        <result name="error">/WEB-INF/pages/testValidate.jsp</result>
        <result name="input">/WEB-INF/pages/testValidate.jsp</result>
    </action>

我的JSP页面

<s:form id="testValidationForm" theme="bootstrap" method="post" labelCssClass="col-sm-2 text-semibold" elementCssClass="col-sm-10"
    cssClass="form-horizontal" action="testValidateSave" enctype="multipart/form-data">
<div class="panel panel-flat">
    <div class="panel-heading">
        <h5 class="panel-title text-bold">Testing Client Validation</h5>
        <div class="heading-elements">

            <sj:submit button="true"
                       name = "Save"
                       type="button"
                       id="saveButton"
                       formIds="testValidationForm"
                       targets="bodySection"
                       iframe="true"
                       dataType="html"
                       validate="true"
                       validateFunction="bootstrapValidation"
                       cssClass="btn bg-cobalt-400 mr-5 pull-right"
                       buttonIcon="icon-floppy-disk"
                       label="Save"
                       value="Save"
                       />
        </div>
    </div>
    <div id="bodySection" class="panel-body">

        <s:textfield
            id="testText"
            name="testText"
            label="Test Text"
            />

        <s:file
            id="testDoc"
            label="Test Doc"
            name="testDoc"/>

    </div>
</div>
</s:form>

还有我的 Action 类:

public class TestValidationAction extends ActionSupport {
private static final long serialVersionUID = 1L;

private String testText;

private File testDoc;
private String testDocContentType;
private String testDocFileName;

/*****************************************************************
 * Action Methods
 *****************************************************************/

@SkipValidation
public String execute() {
    return SUCCESS;
}

public String save() {
    return SUCCESS;
}

public void validate() {
    super.validate();

    System.out.println("    testText = " + this.testText);
    System.out.println("    testDoc = " + this.testDoc );

    if (this.testText == null || this.testText.isEmpty()) {
        addFieldError("testText", "Required");
    }

    if (this.testDoc == null) {
        addFieldError("testDoc", "Required");
    }
}

/*****************************************************************
 * Getters and Setters
 *****************************************************************/

public String getTestText() {
    return this.testText;
}
public void setTestText(String testText) {
    this.testText = testText;
}

public File getTestDoc() {
    return this.testDoc;
}
public void setTestDoc(File testDoc) {
    this.testDoc= testDoc;
}

public String getTestDocFileName() {
    return this.testDocFileName;
}
public void setTestDocFileName(String testDocFileName) {
    this.testDocFileName= testDocFileName;
}

public String getTestDocContentType() {
    return this.testDocContentType;
}
public void setTestDocContentType(String testDocContentType) {
    this.testDocContentType= testDocContentType;
}
}

最佳答案

明白了。我缺少 fileUpload 拦截器。当然,这还不够,因为只使用了 Action 中的第一个拦截器,而我一开始并没有意识到这一点。必须将所有内容组合到包中的新拦截器堆栈中,然后将其分配给操作。

对 Struts.xml 进行的更改解决了问题:

<package name="testPackage" extends="default,json-default" namespace="/testPackage">
    <interceptors>
        <interceptor-stack name="completeValidation">
            <interceptor-ref name="fileUpload"/>
            <interceptor-ref name="jsonValidationWorkflowStack"/>
        </interceptor-stack>
    </interceptors>

    <action name="testValidate" class="webapp.action.TestValidationAction">
        <result name="success">/WEB-INF/pages/testValidate.jsp</result>
    </action>
    <action name="testValidateSave" class="webapp.action.TestValidationAction" method="save" >
        <interceptor-ref name="completeValidation"/>

        <result name="success">/WEB-INF/pages/testValidate.jsp</result>
        <result name="error">/WEB-INF/pages/testValidate.jsp</result>
        <result name="input">/WEB-INF/pages/testValidate.jsp</result>
    </action>
</package

现在一切正常。

关于java - Struts2 Bootstrap 插件客户端使用文件验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36230595/

相关文章:

java - 在 Struts 2 OGNL 中使用动态键访问 Map 属性

java - 在 Theme.res CodenameOne 中设置加载屏幕

java - 使用 Guice 在对象图中构建特定类

java - 从 servlet 获取值并将其显示在 JSP 的表中

java - 操作栏布局看起来不正确

JavaScript 表单验证未提交,页面只是重新加载

javascript - 同时验证表单和一些输入

asp.net - 一个按钮可以验证更多验证组吗?

sql - 如何使用jsp将日期插入数据库?

jsp - 在 JSP 中使用 EL 时出错