大家好,我正在尝试在表单上使用验证,并且我做了示例中的所有工作,并且多次搜索以寻找解决方案,但不幸的是我无法解决问题。

我的问题是:当我单击带有空字段的提交按钮时,它向数据库注册为空并且验证不起作用!!!!!!!!!!!!

我正在使用struts2 tomcat7 jdk7 spring4 hibernate3和sitemesh 2.4.2

有人可以帮我吗

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

    <constant name="struts.custom.i18n.resources" value="ApplicationResources"/>
    <constant name="struts.ui.theme" value="xhtml"/>
    <constant name="struts.devMode" value="true"/>
    <!--<constant name="struts.objectFactory" value="spring" />-->


    <package name="json" namespace="/" extends="json-default">
        <action name="state2CityAction" class="state2CityAction">
            <result name="SUCCESS" type="json" />
        </action>
    </package>


    <package name="default" namespace="/" extends="struts-default">

        <interceptors>
            <interceptor-stack name="myStack">
                <interceptor-ref name="defaultStack">
                    <param name="validation.excludeMethods">prepareNewInterView,prepare,list,previous,next,load,delete,prepareNewPersonnel,loadPayments,loadTeacherSalary,
                        searchInterViewed,findActives,findFrozen,findCanceled,findCanceled,findEducated,searchStudentBaseGroupLesson,prepareSearchStudentPayments,loadPersonnel4FishRegister,
                        prepareGroups4Score,prepareSearchStudentStatus,loadPersonnelFullPayment,listGroupLesson,listGroupLesson4Assign,prepareNewTeacher,listTeacher,prepareSearchTeacherSalary,find,
                        prepareSearchPersonnelSalaryRedirect,prepareSearchCostsPaymentRedirect,prepareSearchTeacher2AssignAccountNumber,createOtherRedirect,
                        prepareSearchPersonnel2AssignAccountNumber,listHesabCari,updateInterViewed,loadTeacherPaymentDetails4Edit,loadTeacherFullPayment,
                        loadTeacherPaymentDetails4Delete,prepareSearchPersonnelSalaryRedirect</param>
                </interceptor-ref>
            </interceptor-stack>
        </interceptors>


        <default-interceptor-ref name="myStack"/>
        <default-action-ref name="index" />

        <global-results>
            <result name="error">/error.jsp</result>
        </global-results>

     <action name="listBank" class="bankInfoAction" method="list">
            <result name="success">/listBank.jsp</result>
        </action>

        <action name="createBank" class="bankInfoAction" method="create">
            <result name="success">/listBank.jsp</result>
            <result name="input">/createBank.jsp</result>

        </action>


        <action name="updateBank" class="bankInfoAction" method="update">
            <result name="input">/updateBank.jsp</result>
            <result name="success">/listBank.jsp</result>
        </action>

        <action name="deleteBankInfo" class="bankInfoAction" method="delete">
        <result name="input">/deleteBank.jsp</result>
        <result name="success">/listBank.jsp</result>
        </action>

        <action name="load2EditBank"  class="bankInfoAction" method="load">
            <result name="success">/editBank.jsp</result>
        </action>

        <action name="load4DeleteBank"  class="bankInfoAction" method="load">
            <result name="success">/deleteBank.jsp</result>
        </action>


BankInfoAction-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="bankInfo.bankName">
        <field-validator type="required">
            <message key="errors.required"/>
        </field-validator>
    </field>

</validators>


这是我的实体

BankInfo.java

    @Entity
public class BankInfo {

    @Id
    @TableGenerator(name = "ct_key", table = "keys_tbl", allocationSize = 70, initialValue = 11)
    @GeneratedValue(strategy = GenerationType.TABLE, generator = "ct_key")
    long id;
    String bankName;

    public BankInfo() {
    }

    public BankInfo(String bankName) {
        this.bankName = bankName;


    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getBankName() {
        return bankName;
    }

    public void setBankName(String bankName) {
        this.bankName = bankName;
    }

    public String toString(){
        return bankName  ;
    }

}


我的行动

BankInfoAction.java

    import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.struts2.interceptor.ParameterAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
public class BankInfoAction extends ActionSupport implements ParameterAware,ModelDriven {

    @Autowired
    BankInfoService bankInfoService;
    BankInfo bankInfo = new BankInfo();
    List<BankInfo> banks = new ArrayList<BankInfo>();
    Map<String, String[]> request;

    public String create(){
        bankInfoService.save(bankInfo);
        list();
        return "success";
    }


    public String update(){
        bankInfoService.update(bankInfo);
        list();
        return "success";
    }


    public String delete(){
        bankInfoService.delete(bankInfo);
        list();
        return "success";
    }


    public String list(){
        banks = bankInfoService.list();
        return "success";

    }


    public String load(){
        bankInfo = bankInfoService.load(bankInfo.getId());
        return "success";
    }


    public String cancel(){
        return "success";
    }


    public BankInfoService getBankInfoService() {
        return bankInfoService;
    }

    public void setBankInfoService(BankInfoService bankInfoService) {
        this.bankInfoService = bankInfoService;
    }

    public BankInfo getBankInfo() {
        return bankInfo;
    }

    public void setBankInfo(BankInfo bankInfo) {
        this.bankInfo = bankInfo;
    }

    public List<BankInfo> getBanks() {
        return banks;
    }

    public void setBanks(List<BankInfo> banks) {
        this.banks = banks;
    }

    public Map<String, String[]> getRequest() {
        return request;
    }

    public void setRequest(Map<String, String[]> request) {
        this.request = request;
    }


    @Override
    public void setParameters(Map<String, String[]> maps) {
        request = maps;
    }

    @Override
    public Object getModel() {
        return bankInfo;

    }
}


createBank.jsp

    <%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Create Bank </title>
</head>
<body>

     <s:form action="createBank" method="Post" validate="true">
            <table style="border: 1px solid #C1D0C8;" align="center" bgcolor="#f4f7f5" cellpadding="20" cellspacing="0" height="320"
                   width="550">
            <tr>
            <td align="center" valign="top">
            <table border="0" width="95%">
                <div class="divForms">
                <div class="item">
                    <div class="label">
                        <span><s:text name="Bank.Name"/></span>
                        <span style="color:red;"> * </span>
                    </div>
                    <div class="control">
                        <s:textfield name="bankName" cssClass="normal_input" theme="simple" />
                    </div>
                </div>


                    <div class="But">
                        <s:submit type="submit" cssClass="gradient-button"  name="newsletter_submit" id="newsletter_submit" key="Save" theme="simple"/>
                        <s:submit type="submit" cssClass="Cancel" name="newsletter_submit" id="newsletter_submit" key="Cancel" theme="simple"/>
                    </div>

            </div>

            </table>
                </tr>
            </table>

            </s:form>
</body>
</html>


这是我的库:​​http://i.stack.imgur.com/V2KkW.jpg

另一件事是BankInfoAction-validation.xml和BankInfoAction.java在同一目录中

最佳答案

你应该用

<field name="bankName">
    <field-validator type="requiredstring">
        <message key="errors.required"/>
    </field-validator>
</field>


因为此字段已填充。 modelDriven拦截器将模型放在值堆栈的顶部。因此,您不必担心设置值。

10-06 05:46