1

我有非常简单的 JSF 2.0 应用程序(见下文)。问题是,当ui:repeat存在时,执行顺序(我在调试器中使用断点检查它)很奇怪。

在我提交表单之后,SecondBean.initSomething()在之前调用FirstBean.setFirstFormField()。如果我将类型更改somethingString并删除并使用,ui:repeat那么一切都按预期工作,之前调用。index.jsfh:outputTextFirstBean.setFirstFormField()SecondBean.initSomething()

我做错了什么?

我正在使用 JDeveloper Studio Edition 11.1.2.2.0 及其堆栈(WebLogic 10.3.5.0、Java 6 和 JSF 2.0)。

这是代码:

index.jsf:

<?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">
<f:view xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" xmlns:ui="http://java.sun.com/jsf/facelets">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <h:head></h:head>
        <h:body>
            <h:form>
                <h:panelGrid columns="1">
                    <h:inputText value="#{firstBean.firstFormField}" />
                    <h:commandButton action="#{firstBean.processForm}" value="Submit" />
                </h:panelGrid>
            </h:form>
            <ui:repeat value="#{secondBean.something}" var="variable" >
                <h:outputText value="#{variable}" />
            </ui:repeat>
        </h:body>
    </html>
</f:view>

FirstBean.java:

package test.backing;

import javax.faces.bean.*;
import javax.faces.context.FacesContext;

@ManagedBean
@RequestScoped
public class FirstBean {
    private String firstFormField;
    public FirstBean() {
        super();
    }

    public String processForm() {
        FacesContext facesContext;

        facesContext = FacesContext.getCurrentInstance();

        return facesContext.getViewRoot().getViewId();            
    }

    public void setFirstFormField(String firstFormField) {
        this.firstFormField = firstFormField;
    }

    public String getFirstFormField() {
        return this.firstFormField;
    }
}

SecondBean.java:

package test.backing;

import java.util.*;

import javax.annotation.PostConstruct;

import javax.faces.bean.*;

@ManagedBean
@RequestScoped
public class SecondBean {
    private List<String> something;

    public SecondBean() {

    }

    @PostConstruct
    public void initSomething() {
        this.something = Arrays.asList("abc", "cde");
    }

    public void setSomething(List<String> something) {
        this.something = something;
    }

    public List<String> getSomething() {
        return this.something;
    }
}

面孔-config.xml:

<?xml version="1.0" encoding="UTF-8"?>
<faces-config version="2.0" xmlns="http://java.sun.com/xml/ns/javaee">

</faces-config>

网页.xml:

<?xml version = '1.0' encoding = 'UTF-8'?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5">
  <context-param>
    <param-name>javax.faces.FACELETS_VIEW_MAPPINGS</param-name>
    <param-value>*.jsf;*.xhtml</param-value>
  </context-param>
  <servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
  </servlet-mapping>
</web-app>
4

3 回答 3

1

我做错了什么?

没有。它按规定工作。它只是在恢复视图期间调用。

如果这对您造成了特定问题,不幸的是,问题中根本没有详细描述,那么需要在不同的方向上寻求解决方案,而不是依赖/依赖 bean 构造的某些特定执行顺序或相对的 getter 调用到视图。至少,出于某种原因,这似乎是您的目标。


与具体问题无关,在该processForm()操作方法中,您也可以返回null以导航回当前视图。这比当前笨拙的方法要简单得多。

于 2012-09-10T20:14:40.547 回答
0

这就是 JSF 循环的工作方式。顺序是对的...

您需要更改您正在做的方式以符合 JSF 框架的规则。了解 JSF 周期的各个阶段,希望对您有所帮助。

于 2012-11-19T12:09:34.097 回答
0

您可以使用 h:dataTable 它不会在还原阶段填充值,而是在 updateModel pahse 之后填充

于 2015-01-15T12:20:23.613 回答