开发者

How can I populate data in JSF [duplicate]

开发者 https://www.devze.com 2023-04-12 08:25 出处:网络
This question already has answers here: 开发者_StackOverflow社区 How to populate options of h:selectOneMenu from database?
This question already has answers here: 开发者_StackOverflow社区 How to populate options of h:selectOneMenu from database? (5 answers) Closed 7 years ago.

Up until now, I have always been using JSP to display pages. When a user request for a page such as "Add Item", I will load all Item Category in an Array List and display them as options in select box like this:

<select name="category>
    <%
        ArrayList<Category> categories = (ArrayList<Category>) request.getAttribute("categories");
        for (Category c : data) {
    %>
    <option value="<%= c.getId() %>"><%= c.getName() %></option>
    <%
        }
    %>
</select>

From the book "JavaServer Faces 2.0, The Complete Reference", I learnt that: "JSF enforces clean Model-View-Controller separation by disallowing the inclusion of Java code in markup pages". Hence, I'd be very grateful if someone could show me how I can handle the above task using JSF since I cannot use Java code as I have always done anymore.

Best regards,

James Tran


JSF 2.0 uses Facelets as the templating method, which in a nutshell is XHTML with some additional elements.

While technically you can perform method calls from Facelets, in general the idea is to access a JavaBean with proper geter/setter methods to perform your data moving. You can accomplish this as the below segment of code shows:

<h:selectOneMenu value="#{backingBean.selectedCategory}">
    <f:selectItems value="#{backingBean.categoryList}"/>
</h:selectOneMenu>

On the bean side of things, you want to expose a bean to JSF using either faces-config (which is largely discouraged) or a mechanism such as CDI or the Managed Bean infrastructure. I highly recommend you look into using SEAM if you go the CDI route, as it will unify the (currently really strangely disparate) Managed Bean and CDI frameworks, so you can use JSF scopes in CDI, and have CDI beans available in JSF scopes.

@ManagedBean(name="backingBean")
@ViewScoped
public class MyJavaBackingBean {

    @ManagedProperty("#{param.categories}")
    protected List<String> categoryList

    public void setSelectedCategory(String value) {
        this.selectedCategory = value;
    }
    public String getSelectedCategory() {
        return this.property;
    }

    ...

}

You can also make the getters do lazy initialization of your values (for pulling categoryList from a database for example), and use some other JSF annotations to do various initialization tasks.

You can also code action methods which return a String representing the JSF action (this gets coded into your faces-context.xml file) to take after returning. Phase listeners on the backing bean can also be called at various stages of page rendering, validation and submission, getting you very fine grained control.

categoryList in the above example is not limited to basic types of course, and <f:selectItems> also has some syntax for writing out the textual version of your select items, so you can make some quite complex expressions to display each item in a friendly way.


Create a bean and make it known with e.g. @Named so you can refer to it from your JSF script. Then give that bean a method returning the data you want to show, and invoke that method from your JSF script in a location where that data is expected e.g. a loop construct.


Store the data you want to display in a Java list, and expose that list as a property of a backing bean. The use the appropriate JSF tag to display that property.


In JSF 2.0 you can include the tag h:selectOneMenu in which you get the value where you store the item value selected. The value in f:selectItems could be a collection of any object the most of times SelectItem in this object your declare value object and the label to display.

<h:selectOneMenu value="#{backingBean.selectedvalue}">
    <f:selectItems value="#{backingBean.List}"/> </h:selectOneMenu>

if you required values and labels of another object in you must declare

<h:selectOneMenu value="#{backingBean.selectedvalue}">
    <f:selectItems value="#{backingBean.ListCar}" var="car" itemLabel="#{car.model}" itemValue="#{car.modelId}"/>
</h:selectOneMenu>
0

精彩评论

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

关注公众号