开发者

JSF issue about space

I am a green bird in JSF, now I was puzzled by the text of it.

when I assign the value of the outputText with multi-spaces, the result shown in IE has only one space.

ex: the code is like

<h:outputText id="name" valu开发者_开发百科e="aa (multi-spaces here)  bbb" ></h:outputText>

the result text shown in IE is "aa bbb"

can anyone tell me why the spaces disappear without trace?


This behaviour is defined by the HTML spec:

For all HTML elements except PRE, sequences of white space separate "words" (we use the term "word" here to mean "sequences of non-white space characters"). When formatting text, user agents should identify these words and lay them out according to the conventions of the particular written language (script) and target medium.

Note that if you are using XHTML, there are some differences in how attributes and the code point U+000C are handled.

For most text, sequences of white space are not rendered any differently from a single space.


Since this is for a outputText control, you can use a one-way converter for a no-break space solution:

package myconverters;
// imports

public class SpacePreserver implements Converter {
  private static final char NO_BREAK_SPACE = '\u00A0';

  public String getAsString(FacesContext context, UIComponent component,
      Object value) {
    if (component instanceof EditableValueHolder) {
      throw new IllegalArgumentException(
          "Cannot use SpacePreserver converter on editable controls.");
    }
    return value == null ? null : value.toString().replace(' ', NO_BREAK_SPACE);
  }

  public Object getAsObject(FacesContext context, UIComponent component,
      String value) {
    throw new UnsupportedOperationException("Output converter only");
  }
}

This can be defined (among other ways) using a faces-config.xml entry:

<converter>
  <converter-id>spacePreserver</converter-id>
  <converter-class>myconverters.SpacePreserver</converter-class>
</converter>

You can then add this to your output control:

<h:outputText id="text1" value="a   b c" converter="spacePreserver" />

This code was tested using JSF 1.1 with a UTF-8 encoded JSP 2.0 view. Note that use of a no-break space will prohibit line-wrapping.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜