这是我的jsp文件:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Company Data</title>
</head>
<body>
<center><h3>Company Data</h3></center>
<s:form action="companyData" validate="true">
<s:textfield label="Name" key="companyName" />
<s:textfield label="ISIN" key="companyISIN" />
<s:checkboxlist list="exchangesList" label="Exchange" name="exchange" />
<s:select list="sectorsList" label="Sector" name="sector" />
<s:checkboxlist list="shareholdersList" label="Shareholders" name="shareholders" />
<s:submit />
</s:form>
</body>
</html>
在这里,shareholdersList
是ArrayList<String>
,shareholders
是String
。
这是我的动作验证文件:
<!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd">
<validators>
<field name="companyName">
<field-validator type="required">
<message>You must enter some name for companyName</message>
</field-validator>
</field>
<field name="companyISIN">
<field-validator type="required">
<message>You must enter some id for companyISIN</message>
</field-validator>
</field>
<field name="exchange">
<field-validator type="fieldexpression">
<param name="expression">
<![CDATA[ isDoNotCheck() ? true : (exchange != null && !exchange.isEmpty()) ]]>
<!-- OR -->
<!-- isDoNotCheck() ? true : !object.field.isEmpty() -->
</param>
<message>You must check some checkboxes for exchange</message>
</field-validator>
</field>
<field name="shareholders">
<field-validator type="fieldexpression">
<message>You must check some checkboxes for shareholders</message>
</field-validator>
</field>
</validators>
以下是提交表单的屏幕截图:
文本框的验证工作正常,但对于checkboxlist,它根本不起作用。我想要复选框列表验证,以便用户必须从列表中选择至少一个复选框。应该怎么做才能纠正代码?
此外,我有一个类似的选择列表而不是其他一些jsp中的checkboxlist,所以如何验证该列表。我在互联网上发现了很多东西,但直到现在都没有用。
尝试使用以下代码:
<field name="exchange">
<field-validator type="fieldexpression">
<param name="expression">exchange.length() > 0</param>
<message><![CDATA[ You must check some checkboxes for exchange ]]></message>
</field-validator>
</field>
<field name="shareholders">
<field-validator type="fieldexpression">
<param name="expression">shareholders.length() > 0</param>
<message><![CDATA[ You must check some checkboxes for shareholders ]]></message>
</field-validator>
</field>
如果可行,请开始添加自定义条件(isDoNotCheck()
...)。
否则,请使用非字段验证器,它确实有效:
<validator type="fieldexpression">
<param name="fieldname">exchange</param>
<param name="expression"><![CDATA[exchange.length() > 0]]></param>
<message><![CDATA[ You must check some checkboxes for exchange ]]></message>
</validator>
<validator type="fieldexpression">
<param name="fieldname">shareholders</param>
<param name="expression"><![CDATA[shareholders.length() > 0]]></param>
<message><![CDATA[ You must check some checkboxes for shareholders ]]></message>
</validator>
使用以下代码。复选框List的值存储为string.Hence,您可以使用requiredstring。
<field name="exchange">
<field-validator type="requiredstring">
<message>Please select exchange type</message>
</field-validator>
</field>
<field name="shareholders">
<field-validator type="requiredstring">
<message>Please select shareholders</message>
</field-validator>
</field>