JSF - 在页面加载时调用bean方法

时间:2016-11-18 11:16:37

标签: jsf methods invoke pageload

在阅读了多篇关于它的帖子后,在页面加载时调用我的bean方法就行不通了。

以下是我的面孔示例:

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:f="http://java.sun.com/jsf/core">

    <h:head>
        <title>foo</title>
    </h:head>

    <h:body>
        <ui:composition template="resources/template/mainTemplate.xhtml">
            <f:metadata>
                <f:viewAction action="#{myBean.createObject}" />
            </f:metadata>   
            <ui:define name="header">
                <!-- Bunch of outputTexts -->
            </ui:define>
            <ui:define name="body">
                <!-- Bunch of outputTexts -->
            </ui:define>
            <ui:define name="footer">
                <!-- Bunch of outputTexts -->
            </ui:define>
        </ui:composition>
    </h:body>
</html>

我的模板(mainTemplate.xhtml):

<?xml version='1.0' encoding='UTF-8' ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
      xmlns:h="http://xmlns.jcp.org/jsf/html">

    <h:head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <h:outputStylesheet name="/css/default.css"/>
        <h:outputStylesheet name="/css/cssLayout.css"/>
        <title>#{msg['application.name']}</title>
    </h:head>

    <h:body>
        <div id="top">
            <ui:insert name="top">Top</ui:insert>
        </div>
        <div id="content" class="center_content">
            <ui:insert name="content">Content</ui:insert>
        </div>
        <div id="bottom">
            <ui:insert name="bottom">Bottom</ui:insert>
        </div>
    </h:body>
</html>

和我的托管bean一样:

@Named(value = "orderDetailsBean")
@SessionScoped
public class OrderDetailsBean implements Serializable {

    @Inject
    Services services;
    private Order order;

    public OrderDetailsBean() {
        this.order = new Order();
    }

    public Order getOrder() {
        return order;
    }

    public void setOrder(Order order) {
        this.order = order;
    }

    public void createOrder() {
        // this method is not triggered at page load
    }
}

我正在使用:

  • JSF 2.2
  • GlassFish 4.1.1

0 个答案:

没有答案