空字段验证在 Struts 2 中不起作用

问题描述 投票:0回答:2

我正在尝试在我的表单上使用验证,我做了示例中的所有操作,并多次通过 Google 搜索来找到解决方案,但不幸的是,我无法解决我的问题。

我的问题是:

当我单击带有空字段的提交按钮时,它会将空值存储到数据库中,并且验证不起作用!

我正在使用 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>  

这是我的库:

另一件事是

BankInfoAction-validation.xml
BankInfoAction.java
位于同一目录中。

java spring jsp validation struts2
2个回答
0
投票

三天后我终于在老师的帮助下解决了我的问题: 正如您所见,我完成了所有验证配置,但是: 第一个问题是:提交后 validatoin 不起作用 >> 我的 struts 版本是 struts2-core-2.3.14.3.jar 我将其更改为 struts2-core-2.3.15.x.jar 并且它起作用了:)

第二个问题是:提交验证消息后没有显示>>> 1天后我找到了解决方案>>>>>问题是我的文本字段中的主题=“简单”我将其更改为主题=“css_xhtml”


0
投票

你应该使用

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

因为此字段已填充。

modelDriven
拦截器将模型放在
top
valueStack
上。因此,您不必担心设置值。

© www.soinside.com 2019 - 2024. All rights reserved.