gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 15:11:54 24 4
gpt4 key购买 nike

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

我的问题是:

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

我正在使用 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 位于同一目录中。

最佳答案

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

第二个问题是:提交验证消息后未显示 >>> 1 天后我找到了解决方案 >>>>> 问题是我的文本字段中的 theme="simple"我将其更改为 theme="css_xhtml"

关于java - 空字段验证在 Struts 2 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21239971/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com