Special Char'#'在struts验证器中创建问题

时间:2017-09-05 12:51:46

标签: java struts2

我在jsp中创建了一个简单的表单

<table class="entry">
    <tr>
        <th width="30%">
            <s:text name="location.label.country"/>
        </th>
        <td width="70%">
            <s:select list="@com.dmainc.commons.stuff.geo.StateUtils@getFormattedCountries()" headerKey="" headerValue="[Please Select]" listKey="abbreviation" listValue="prettyName" name="location.country" id="country_%{tabId}"/>
        </td>
    </tr>
    <tr>
        <th>
            <s:text name="location.label.address1"/>
        </th>
        <td>
            <s:textfield cssStyle="width: 19em" name="location.address1" id="address1_%{tabId}" cssClass="full"/>
        </td>
    </tr>
</table>

我正在使用struts验证器来验证字段。 因此,每当我在address1(例如Test#)文本框中放置带符号#的内容时,验证器就会在国家/地区字段上抛出NullPointerException。它也会从地址1字段中修剪#。 它适用于其他符号。 有没有charset问题? 任何帮助/建议都将受到高度赞赏

编辑: 验证器:

   @Override
   public void validate()
   {
      if(location.getCountry().equalsIgnoreCase("US"))
      {
         addFieldError("country", "State & Country mismatch!");
         addFieldError("state", "State & Country mismatch!");
      }
   } 

在if块

中获取空指针

更新:完成jsp页面

<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<s:action namespace="/org" name="list_all" var="ol" />
<s:form id="edit_location_%{tabId}">
<s:hidden name="location.locationId" id="locationId_%{tabId}"/>
<s:hidden name="changeOrder.changeOrderId" id="changeOrderId"/>
<s:hidden name="parentChangeOrderId" id="parentChangeOrderId_%{tabId}"/>
<s:hidden name="requiresEndDating" id="requires_end_dating_%{tabId}"/>
<s:hidden name="updateEffectiveDate" id="update_effective_date_%{tabId}"/>
<div id="location_<s:property value='tabId'/>">
   <ul>
      <li><a href="#general_<s:property value='tabId'/>"><s:text name="location.tab.general-info"/></a></li>
      <s:if test="showDetailsTab == true">
        <li><a href="#detail_<s:property value='tabId'/>"><s:text name="location.tab.details"/></a></li>
      </s:if>
      <s:if test="outputAdapters.size > 0">
        <li><a href="#outputadapter_<s:property value='tabId'/>"><s:text name="location.tab.outputadapters"/></a></li>
      </s:if>
   </ul>
       <div>
      <div id="general_<s:property value='tabId'/>">
           <div>
              <table class="placeholder" style="width: 100%">
                <tr>

                    <td style="width: 50%;">
                      <div class="field_maintain_div">
                         <fieldset class="field_maintain_fieldset">
                            <legend><s:text name="location.heading.address-info"/></legend>

                            <table class="entry">
                               <tr>
                                  <th width="30%"><s:text name="location.label.country"/></th>
                                  <td width="70%"><s:select list="@com.dmainc.commons.stuff.geo.StateUtils@getFormattedCountries()" headerKey="" headerValue="[Please Select]" listKey="abbreviation" listValue="prettyName" name="location.country" id="country_%{tabId}"/></td>
                               </tr>
                               <tr>
                                  <th><s:text name="location.label.address1"/></th>
                                  <td><s:textfield cssStyle="width: 19em" name="location.address1" id="address1_%{tabId}" cssClass="full"/></td>
                               </tr>
                               <tr>
                                  <th><s:text name="location.label.address2"/></th>
                                  <td><s:textfield cssStyle="width: 19em" name="location.address2" id="address2_%{tabId}" cssClass="full"/></td>
                               </tr>
                               <tr>
                                  <th><s:text name="location.label.city"/></th>
                                  <td><s:textfield cssStyle="width: 19em" name="location.city" id="city_%{tabId}" cssClass="half"/></td>
                               </tr>
                               <tr>
                                  <th><s:text name="location.label.state"/></th>
                                  <td><s:select list="@com.dmainc.commons.stuff.geo.StateUtils@STATES_PROVINCES" headerKey="" headerValue="[Please Select]" listKey="abbreviation" listValue="prettyName" name="location.state" id="state_%{tabId}"/></td>
                               </tr>
                               <tr>
                                  <th><s:text name="location.label.postal-code"/></th>
                                  <td><s:textfield cssStyle="width: 6em" name="location.zip" id="zip_%{tabId}"/></td>
                               </tr>
                            <tr>
                               <th><s:text name="location.label.county"/></th>
                               <td><s:textfield cssStyle="width: 19em" name="location.county" id="county_%{tabId}" cssClass="half"/></td>
                            </tr>
                               <tr>
                                  <th><s:text name="location.label.jurisdiction-code"/></th>
                                  <td><s:textfield name="location.jurisdictionCode" id="jurisdictionCode_%{tabId}" disabled="true"/></td>
                               </tr>
                               <tr>
                                  <th><s:text name="location.label.compliance-jurisdiction-code"/></th>
                                  <td><s:textfield name="location.complianceJurisdictionCode" id="complianceJurisdictionCode_%{tabId}" /></td>
                               </tr>
                            </table>
                         </fieldset>
                      </div>
                    </td>
                </tr>
              </table>
           </div>
      </div>
   </div>
</div>
</s:form>

2 个答案:

答案 0 :(得分:0)

#是OGNL使用的关键字。尝试通过\#

转义它

答案 1 :(得分:0)

我的问题通过在javascript中用#23替换#来解决。

address1 = address1.replace("#","%23");