使用 Struts 2 进行客户端验证时出现问题。无法识别基于 Xml 的验证规则

发布于 2024-08-28 16:09:11 字数 5576 浏览 5 评论 0原文

我的问题是,当我没有为该字段输入任何值时,即使按要求配置了该字段,也看不到客户端验证错误消息。页面重新加载并转到结果页面,客户端验证失败。我不确定我做错了什么。

我有一个简单的表单,其中有一个称为选择标准的下拉菜单。必须选择一个值。如果未选择值,则页面应重新加载并显示配置的错误消息。我的输入表单 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>

我已在展开的 war 文件的 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();
}
}

My issue is that when I don't see a client side validation error message when I don't enter any values for that field even when it is configured as required. The page is reloaded and goes to the result page and client validation fails. I am not sure what I am doing wrong.

I have a simple form where I have a pull down menu called selection criterion. A value must be selected. If a value is not selected, then the page should reload with configured error message. My input form action_item_search.jsp is given below:

<%@ 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>

I have add validators.xml in my WEB-INF/classes directory of exploded war file as given below:

<!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>

ActionItemTrackingAction-findByCriteria-validation.xml in WEB-INF/classes directory is given below:

<!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>

My struts mapping 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>

My action class

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();
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

離人涙 2024-09-04 16:09:11

当我的开发机器上没有互联网连接时,我也遇到同样的问题。

一旦建立了与我的开发机器的互联网连接,我就重新编译并运行应用程序并且验证工作。似乎 validators.xml 需要互联网。

I have the same issue when I do not have internet connection on my development machine.

Once internet connection to my development machine is established, I recompile and run the application and the validation works. Seems like the validators.xml requires internet.

黑凤梨 2024-09-04 16:09:11

您必须在 jsp 中包含 标记。否则,struts2x 无法进行客户端验证。默认情况下,它不会显示任何警报框。您必须使用 onSubmit= ,后跟 HTML 生成的函数名称。您可以通过在浏览器中查看生成表单的源代码来检查函数名称。希望这会对您有所帮助。

You must inlcude <s:head/> tag in your jsp. Other wise client side validations are not possible with struts2x. By default it will not show any alert boxes. you must use onSubmit=<true/false> folowed by function name generated by the HTML . you can check name of the function by seeing source code of generated form in your browser. Hope this will help you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文