特殊字符“#”在 struts 验证器中造成问题
Special Char '#' is creating issue in struts validator
我在 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 字段中删除#。
它适用于其他符号。
有任何字符集问题吗?
任何 help/suggestion 将不胜感激
编辑:
验证者:
@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>
#
是 OGNL 使用的关键字。尝试通过 \#
转义它
我的问题通过在 javascript 中用 %23 替换 # 得到解决。
address1 = address1.replace("#","%23");
我在 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 字段中删除#。 它适用于其他符号。 有任何字符集问题吗? 任何 help/suggestion 将不胜感激
编辑: 验证者:
@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>
#
是 OGNL 使用的关键字。尝试通过 \#
我的问题通过在 javascript 中用 %23 替换 # 得到解决。
address1 = address1.replace("#","%23");