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

Posted by Kartik on Stack Overflow See other posts from Stack Overflow or by Kartik
Published on 2010-04-15T17:30:13Z Indexed on 2010/04/15 17:33 UTC
Read the original article Hit count: 292

Filed under:
|
|

Hi, This is my first post to ask a question on stack overflow and 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 input page is reloaded but no error message is seen. I am not sure what I am doing wrong. Any advice would be greatly appreciated. My scenario is given below:

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

ActionItemTrackingAction-findByCriteria-validation.xml 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">

You must enter a search criterion.

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>

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

© Stack Overflow or respective owner

Related posts about java

Related posts about struts2