开发者

Text input for multi-valued attribute

开发者 https://www.devze.com 2022-12-27 18:54 出处:网络
I need to create an input form that will allow a user to enter any number of values for a particular attribute.

I need to create an input form that will allow a user to enter any number of values for a particular attribute.

I've tried several approaches, all of which seem to have various levels of failure. The latest model bean looks something like:

public class Product {
  private String name;
  private ArrayList<String> tags = new ArrayList(5);

  {
    tags.add("");  //seed with 1 non-null element
  }

  ...accessors...
}

The input fields look something like:

开发者_开发问答    <h:dataTable id="tags" value="#{product.tags}" var="tag">
      <h:column><h:inputText id="t" value="#{tag}"></h:inputText></h:column>

My plan was to allow the user is to use javascript to add additional form fields as needed.

This type of setup gives me a 'Target Unreachable' error. What am I missing?

My problem now is that the setter for tags does not get called. Oddly enough, the setter for name does.

I am using JSF 1.1 on WebSphere 6.1


You need to preinstantiate/preserve any non-standard objects yourself. EL won't preinstantiate them for you. This applies on anything else than String, Number and Boolean for which EL has built-in recognizions and coercions. No, arrays like String[] are not covered as you encountered.

Basically the following should do:

private String[] tags = new String[length];

You'll only need to pass the length as a <input type="hidden" name="length"> and let JS fill it, so that you can preserve the object in a @PostConstruct yourself:

@ManagedProperty(value="#{param.length}")
private int length;

@PostConstruct
public void init() {
    this.tags = new String[length];
}

Update: Sorry, the above applies on JSF 1.2 or newer only. Well, in your case you'll have to do some hacky works such as grabbing it from ExternalContext#getRequestParameterMap(). Please also keep in mind that JavaScript doesn't see anything from the JSF source code, it instead only sees its generated HTML output. Rightclick page in browser and choose View Source. Take this into account when coding JS.

0

精彩评论

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