我的问题是,当我没有为该字段输入任何值时,即使根据需要配置了该字段,我也没有看到客户端验证错误消息。页面重新加载并转到结果页面,客户端验证失败。我不确定我做错了什么。
我有一个简单的表单,我有一个名为选择标准的下拉菜单。必须选择一个值。如果未选择值,则应使用配置的错误消息重新加载页面。我的输入表单action_item_search.jsp如下:
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Action Item Search</title>
</head>
<body>
<s:actionerror/>
<s:fielderror />
<s:form action="action_item_search" validate="true">
<s:select label="Search Criterion" name="searchCriterion"
list="#{'': 'Select One', 'creatorName':'creator name',
assignedTo':'assigned to'}" required="true" />
<s:submit name="search" value="Search"></s:submit>
</s:form>
</body>
我在爆炸战争文件的WEB-INF / classes目录中添加了validators.xml,如下所示:
<!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator Config 1.0//EN"
"http://www.opensymphony.com/xwork/xwork-validator-config-1.0.dtd">
<validators>
<validator name="required"
class="com.opensymphony.xwork2.validator.validators.RequiredFieldValidator"/>
<validator name="requiredstring"
class="com.opensymphony.xwork2.validator.validators.RequiredStringValidator"/>
<validator name="int"
class="com.opensymphony.xwork2.validator.validators.IntRangeFieldValidator"/>
<validator name="long"
class="com.opensymphony.xwork2.validator.validators.LongRangeFieldValidator"/>
<validator name="short"
class="com.opensymphony.xwork2.validator.validators.ShortRangeFieldValidator"/>
<validator name="double"
class="com.opensymphony.xwork2.validator.validators.DoubleRangeFieldValidator"/>
<validator name="date"
class="com.opensymphony.xwork2.validator.validators.DateRangeFieldValidator"/>
<validator name="expression"
class="com.opensymphony.xwork2.validator.validators.ExpressionValidator"/>
<validator name="fieldexpression"
class="com.opensymphony.xwork2.validator.validators.FieldExpressionValidator"/>
<validator name="email"
class="com.opensymphony.xwork2.validator.validators.EmailValidator"/>
<validator name="url"
class="com.opensymphony.xwork2.validator.validators.URLValidator"/>
<validator name="visitor"
class="com.opensymphony.xwork2.validator.validators.VisitorFieldValidator"/>
<validator name="conversion"
class="com.opensymphony.xwork2.validator.validators.ConversionErrorFieldValidator"/>
<validator name="stringlength"
class="com.opensymphony.xwork2.validator.validators.StringLengthFieldValidator"/>
<validator name="regex"
class="com.opensymphony.xwork2.validator.validators.RegexFieldValidator"/>
<validator name="conditionalvisitor"
class="com.opensymphony.xwork2.validator.validators.ConditionalVisitorFieldValidator"/>
</validators>
WEB-INF / classes目录中的ActionItemTrackingAction-findByCriteria-validation.xml如下:
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
<field name="searchCriterion" >
<field-validator type="required">
<message>You must enter a search criterion.</message>
</field-validator>
</field>
</validators>
我的struts映射xml:
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<!-- <include file="example.xml"/> -->
<package name="action-item" extends="struts-default">
<action name = "action_item_search_input">
<result name = "success">/action-item-search.jsp</result>
</action>
<action name="action_item_search" class="gov.nasa.spacebook.ActionItemTrackingAction" method="fetchByCriteria">
<result name = "success">/action-item-result.jsp</result>
<result name = "input">/action-item-search.jsp</result>
<result name = "error">/action-item-search.jsp</result>
</action>
</package>
</struts>
我的动作类
public class ActionItemTrackingAction extends ActionSupport {
private List<ActionItem> actionItems;
public List<ActionItemTracking> getActionItems() {
return actionItems;
}
public void setActionItems(List<ActionItemTracking> actionItems) {
this.actionItems = actionItems;
}
private String searchCriterion;
public String getSearchCriterion() {
return searchCriterion;
}
public void setSearchCriterion(final String criterion) {
this.searchCriterion = criterion;
}
public String fetchByCriteria() throws Exception {
final ActionItemTrackingService service =
new ActionItemTrackingService();
this.actionItems = service.getByField(this.actionItem);
return super.execute();
}
}
答案 0 :(得分:0)
当我的开发机器上没有互联网连接时,我遇到同样的问题。
建立与我的开发机器的互联网连接后,我重新编译并运行应用程序,验证工作正常。似乎validators.xml需要互联网。
答案 1 :(得分:0)
您必须在jsp中包含<s:head/>
标记。 struts2x无法实现其他明智的客户端验证。默认情况下,它不会显示任何警告框。您必须使用HTML生成的函数名称后面的onSubmit=<true/false>
。您可以通过在浏览器中查看生成的表单的源代码来检查函数的名称。希望这会帮助你。