Viewscoped Managed Bean未实例化

时间:2017-01-25 11:14:58

标签: jsf jsf-2 weblogic

环境

  • Weblogic 12.1.3.0.0
  • Java 1.7
  • JSF 2.2(由于Weblogic Server的问题,提供来自应用程序的库)
  • 基于Ant的构建工具(庞大的公司意味着旧工具)

文件

Portal.java

body.lock-position {
  height: 100%;
  overflow: hidden;
  width: 100%;
  position: fixed;
}

portal.xhtml

@ManagedBean(name="portal")
@ViewScoped
public class Portal {
    public String foo ="foo";

    @PostConstruct
    public void init() {
        System.out.println("instantiated");
    }
}

faces-config.xml中

<!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://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:c="http://java.sun.com/jsp/jstl/core">

<ui:composition template="/WEB-INF/templates/wide_template.xhtml">
    <ui:define name="mainContent"> 

        <!-- other markup removed -->

        <h:outputText value="hello:#{portal.foo}"></h:outputText>
    </ui:define>
</ui:composition>
</html>

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
    version="2.2">

</faces-config>

问题

我的页面正确呈现所有元素。例如<?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" version="3.0"> <context-param> <param-name>facelets.REFRESH_PERIOD</param-name> <param-value>2</param-value> </context-param> <context-param> <param-name>com.sun.faces.validateXml</param-name> <param-value>true</param-value> </context-param> <context-param> <param-name>com.sun.faces.verifyObjects</param-name> <param-value>true</param-value> </context-param> <context-param> <param-name>javax.faces.DEFAULT_SUFFIX</param-name> <param-value>.xhtml</param-value> </context-param> <context-param> <param-name>javax.faces.STATE_SAVING_METHOD</param-name> <param-value>server</param-value> </context-param> <context-param> <param-name>javax.faces.FACELETS_SKIP_COMMENTS</param-name> <param-value>true</param-value> </context-param> <context-param> <param-name>com.sun.faces.expressionFactory</param-name> <param-value>com.sun.el.ExpressionFactoryImpl</param-value> </context-param> <context-param> <param-name>javax.faces.PROJECT_STAGE</param-name> <param-value>Development</param-value> </context-param> <welcome-file-list> <welcome-file>portal.xhtml</welcome-file> </welcome-file-list> <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>/skinned/*</url-pattern> <url-pattern>*.faces</url-pattern> <url-pattern>/faces/*</url-pattern> <url-pattern>*.jsf</url-pattern> </servlet-mapping> </web-app> 等等。但似乎我的Managed Bean从未实例化,因为我没有在控制台上看到输出,也没有页面呈现h:selectOneMenu的内容。

我已经阅读了关于SO的其他一些问题:

有没有人知道SessionScope的问题?

我有什么遗失的东西吗?或者我是否必须提供更多内容,例如部署后的应用程序结构,更多代码,屏幕截图?

2 个答案:

答案 0 :(得分:0)

由于它是@ViewScoped bean,你必须实现Serializable,因为实现可能决定在需要时序列化会话或查看作用域bean。

@ManagedBean(name="portal")
@ViewScoped
public class Portal  implements Serializable{

理论上,NonSerializableException只应在序列化过程中发生,但也许您的环境设置会阻止创建本身。

无论如何,你的所有@SessionScoped / @ViewScoped都应按照定义标记。

答案 1 :(得分:0)

尝试使用@javax.inject.Named代替@ManagedBean