【问题标题】:multiple spaces are been removed while reRendering the panel for any component in java在为 java 中的任何组件重新渲染面板时,删除了多个空格
【发布时间】:2013-04-19 10:07:01
【问题描述】:

我在 itemValue "03 R MAIN1" 中有 2 个空格。在进行 ajax 提交时,面板正在重新渲染,当时我可以看到 2 个空格已被替换为 1 个空格,因为此值与选中的值匹配,并且该值在下拉列表中。所以我什至得到“验证错误:值无效”。请看下面的代码:

<a4j:form>
    <a4j:outputPanel ajaxRendered="true" id="TEST">
        <h:messages  layout="table" errorClass="errormsg noticeMsg" fatalClass="errormsg noticeMsg" 
             infoClass="infomsg noticeMsg" styleClass="table-center dont-capitalize" id="err_succ_message" />
        <h:outputText value="Display DropDown Values" />
        <h:selectOneMenu value="#{nonLocationSpecificBackingBean.testing}">
            <f:selectItem itemValue="03 R  MAIN1"/>
        </h:selectOneMenu>
        <a4j:commandButton value="Save" action="#{nonLocationSpecificBackingBean.test}" reRender="err_succ_message,TEST"></a4j:commandButton>
        <a4j:commandButton value="Test"  reRender="TEST"></a4j:commandButton>
    </a4j:outputPanel>
</a4j:form>

请帮忙。谢谢

【问题讨论】:

标签: java ajax4jsf commandbutton


【解决方案1】:

这与 HTML 中如何处理多个空格有关。但是,在您的情况下,我认为 CSS 技巧不是答案(如链接问题中所建议的那样)。

我认为一个好的解决方案是在表单中使用 HTML 编码值,并在您的服务器组件中对其进行解码。

更新:转换器中的可能实现将如下所示(在此处添加代码而不是注释,因为格式问题:))

public Object getAsObject(FacesContext arg0, UIComponent arg1, String newValue) {
    return (Object) newValue.replaceAll("&nbsp;", " ");
}

public String getAsString(FacesContext arg0, UIComponent arg1, Object value) { 
    return value.toString().replaceAll(" ", "&nbsp;"); 
}

【讨论】:

  • 谢谢阿克谢。我尝试使用转换器进行解码和编码。请看下面的代码:
  • 转换器代码 FacesConverter("SpaceConverter") public class SpaceConverter implements Converter{ Override public Object getAsObject(FacesContext arg0, UIComponent arg1, String newValue) { return (Object)newValue.replace(" ", "");} 覆盖 public String getAsString(FacesContext arg0, UIComponent arg1, Object value) { return value.toString().replace(" ", ""); } } xhtml 代码:
  • 请查看添加到答案中的代码,希望对您有所帮助。我无法测试它,因为我现在没有配置合适的项目。
猜你喜欢
  • 2021-02-04
  • 2019-03-09
  • 2019-06-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多