开发者

Trouble with client side validation using Struts 2. Xml based validation rules not recognized

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 = 开发者_StackOverflow社区
        new ActionItemTrackingService();
    this.actionItems = service.getByField(this.actionItem);
    return super.execute();
}
}


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜