tag) .Which, once caused problem when i try to add select items to a component us开发者_如何学Going javascript" />
开发者

JSF validation for client side injected elements

I know there is a property in asp.net (probably this EnableEventValidation of "<%@ Page%> tag) .Which, once caused problem when i try to add select items to a component us开发者_如何学Going javascript ,I want to know how jsf handling this. That is,

if i send a h:select* like below and client add a new item "option3 " to item list, is jsf detect this automaticly before update model values .

<h:selectOneMenu  id="type"  
                  value="#{foo.value}"
                  required="true" 
                  requiredMessage="Type is required"   
                  style="width:100px">
  <f:selectItem  value="option1}"/>
  <f:selectItem  value="option2}"/>

</h:selectOneMenu>


I think what you need to understand regarding JSF is that the client/server parts of the components are tightly coupled together. You are probably better off thinking of them strictly as one entity, and forget about fiddling with one side only, reverting to custom Javascript when that is the only solution left.

The other way to think of it is that the server side renders the client side, not vice versa! So whenever you need to update a component the update must be done on the server side first, which will propagate to the client side (the browser).

In your example the proper way to add and element to the select* items is to store the selectable items in a data structure within a bean (probably @ViewScoped), and then do a partial update via AJAX for the select* component or its container component, when the server side gets the chance to become aware of the changes and can update the client side properly as well.

Sure, you can hack your way through Javascript only, but then why use JSF? The whole point of JSF is to avoid the need for hacks like this.

Remember, JSF is not JSP, which is basically a println macro for html output. JSF stores the page components' representation on the server side, mirroring the browser's representation.

Check the Primefaces showcase for examples on how to do partial updates. More specifically this is the example you could be looking for. This is available in standard JSF2, for JSF 1.2 you must use a component library to get AJAX support.


You should not add the new option by JavaScript, but you should add the new option by JSF. JSF needs to know about the new option somehow in order to allow the submitted value. The new option really needs to be served by <f:selectItem(s)>. Otherwise you will face Validation error: Value not valid all the time when submitting the option value which is added by JS. This is after all just a safeguard of JSF to prevent clients from manipulating the request and submitting values which they are not supposed to submit.

The following kickoff example should work:

<h:form>
    <h:selectOneMenu id="menu" value="#{bean.item}">
         <f:selectItems value="#{bean.items}" />
    </h:selectOneMenu>

    <h:inputText id="newItem" value="#{bean.newItem}" />
    <h:commandButton value="add" action="#{bean.addNewItem}">
        <f:ajax execute="@this newItem" render="menu" />
    </h:commandButton>

    <h:commandButton value="submit" action="#{bean.submit}" />
</h:form>

with a @ViewScoped managed bean something like follows:

private String item;
private List<String> items = Arrays.asList("option1", "option2");
private String newItem;

public void addNewItem() {
    items.add(newItem);
}

// ...
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜