代码之家  ›  专栏  ›  技术社区  ›  Surya Mandal

特殊字符“#”在struts验证器中产生问题

  •  -1
  • Surya Mandal  · 技术社区  · 7 年前

    我在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)文本框中放入带有符号#的内容时,验证器就会在country字段上抛出NullPointerException。此外,它还从地址1字段中修剪。 是否存在字符集问题?

    验证人:

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

    在if块中获取空指针

    <%@ 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 回复  |  直到 7 年前
        1
  •  0
  •   Yasser Zamani    7 年前

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

        2
  •  0
  •   Surya Mandal    7 年前

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