Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jsf @SessionScoped bean作为@ViewScoped的@ManagedProperty注入,其行为类似于MyFaces中的@RequestScoped,在Mojarra中运行良好_Jsf_Myfaces_Mojarra - Fatal编程技术网

Jsf @SessionScoped bean作为@ViewScoped的@ManagedProperty注入,其行为类似于MyFaces中的@RequestScoped,在Mojarra中运行良好

Jsf @SessionScoped bean作为@ViewScoped的@ManagedProperty注入,其行为类似于MyFaces中的@RequestScoped,在Mojarra中运行良好,jsf,myfaces,mojarra,Jsf,Myfaces,Mojarra,以下是我的简单示例: Index.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"

以下是我的简单示例:

Index.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:h="http://java.sun.com/jsf/html">
    <h:head>
        <title>Title</title>
    </h:head>
    <h:body>
        <h:form>
            <h:inputText value="#{index.variable}"></h:inputText>
            <h:commandButton action="#{index.submit()}" type="submit"></h:commandButton>
        </h:form>
    </h:body>
</html>
/next/index.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:h="http://java.sun.com/jsf/html">
    <h:head>
        <title>Check variable</title>
    </h:head>
    <h:body>
        #{sessionBean.asd}
    </h:body>
</html>

如果我使用mojarra实现,一切都会按预期工作:表单提交后,用户会被重定向到
root/
,并查看以
index.xhtml
格式打印的值

但如果我使用myfaces,
asd
在现有表单提交后立即变为null
SessionScoped
bean的行为类似于
RequestScoped

为什么?


这是我的
web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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_3_0.xsd">
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <listener>
        <listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
        <!--listener-class>com.sun.faces.config.ConfigureListener</listener-class-->
    </listener>
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <welcome-file-list>
        <welcome-file>index.xhtml</welcome-file>
    </welcome-file-list>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>

javax.faces.PROJECT_阶段
发展
org.apache.myfaces.webapp.StartupServletContextListener
Facesservlet
javax.faces.webapp.FacesServlet
1.
index.xhtml
Facesservlet
/
30

服务器:ApacheTomcat 7.0.34


UPDATE:如果我们将
Index.java bean的
ViewScoped
注释更改为
RequestScoped
SessionScoped
,它就会起作用。但是为什么呢?

我目前在Mojarra的帮助下编写了一个JSF2.1项目。只是为了进行实验,我将实现更改为MyFaces,运行应用程序时只看到与您类似的问题(我所有注入的@ManagedProperty变量在提交后都为null)。我切换回Mojarra,应用程序运行良好。因此,MyFaces实现中基本上有些不同

通过谷歌搜索,我找到了这个尚未解决的问题。以下是问题记者关于如何解决问题的摘录:

如果将设置为false 并重新部署应用程序,然后一切正常

这有什么帮助?答案是:

我认为所描述的行为是预期的(不同于说 所描述的行为是期望的或有意的)

这里发生的是MyFaces序列化被设置为true 默认值(JSF1.0规范中的一些旧行是这样说的,,即使RI没有这样做 以这种方式实施它)。在JSF2.2规范中,序列化会话中的状态 参数将标准化,默认设置为false

序列化导致视图范围下的所有bean实际上都是 “重新创建”。如果参数设置为false,则bean将存储到 使用会话和其他请求,看起来一切正常 好的,但事实并非如此,因为在集群配置中 同一应用程序将失败

只有在第一次创建视图范围bean时,引用 从托管属性生效,但如果bean 序列化/反序列化后,引用不会还原回来,因为 在序列化步骤中,甚至应用程序和会话范围 bean也被序列化

如何解决?我还没有找到解决这个问题的好办法。一个 可以考虑恢复视图范围bean并重新应用 @在faces-config.xml中找到ManagedProperty批注或条目,但 问题是视图范围bean仍然在存储 从一开始就不应出现(仅将字段标记为瞬态 会成功的)。有可能定义一种特殊模式 hack或其他变体已经完成,但它只会出现在myfaces和it中 默认情况下无法启用

类似问题已在本报告中报告并回复了上述解释

现在,由于您没有显式设置
状态保存方法
,因此默认为服务器。因此,
SERIALIZE\u STATE\u IN\u SESSION
将生效并默认为true


我在Tomcat上用MyFaces尝试了你的代码,并将SESSION中的SERIALIZE_STATE_设置为false,它就可以工作了。但是,在这种情况下,如果将STATE\u SAVING\u方法设置为client,则不会有任何效果,并且会出现视图STATE not found错误。

为什么要使用外部上下文重定向?假设您使用JSF2,您可以通过
“return/root/index.xhtml;”
返回下一个位置。这样重定向时,myfaces和Mojara的行为可能不同。您应该同时拥有myfaces api+impl JAR文件或单个myfaces捆绑JAR文件。您不应该有api+impl+bundle JAR文件。捆绑文件是捆绑在一个JAR中的api+impl,因此在类路径中当前有两个myfacesapi+impl。这是错误的。@BalusC是的,这是我的错,但在我改变它之后,这个问题仍然存在。这不是一个答案,而是一个评论。
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean
@SessionScoped
public class SessionBean implements Serializable {

    private String asd;

    public String getAsd() {
        return asd;
    }

    public void setAsd(String asd) {
        this.asd = asd;
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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_3_0.xsd">
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <listener>
        <listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
        <!--listener-class>com.sun.faces.config.ConfigureListener</listener-class-->
    </listener>
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <welcome-file-list>
        <welcome-file>index.xhtml</welcome-file>
    </welcome-file-list>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>