Jsf 当Primefaces主题与@ManagedBean一起使用时,加载带有@Named bean的Primefaces主题时出错

Jsf 当Primefaces主题与@ManagedBean一起使用时,加载带有@Named bean的Primefaces主题时出错,jsf,primefaces,cdi,Jsf,Primefaces,Cdi,我在使用JSF2.2和Primefaces 5加载主题时出错。我有下面的bean: package beans; import java.io.Serializable; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; @ManagedBean(name = "themeSwitcher") @SessionScoped public class ThemeSwitcher impleme

我在使用JSF2.2和Primefaces 5加载主题时出错。我有下面的bean:

package beans;

import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean(name = "themeSwitcher")
@SessionScoped
public class ThemeSwitcher implements Serializable {

private String theme;

public ThemeSwitcher () {      
    theme = "afterdark";                   
}   

public String getTheme() {

    return theme;
}

public void setTheme(String theme) {
    this.theme = theme;
}
}

这很好用。当我试图将其更改为@Named以便能够将其注入到我的其他bean时,会出现以下错误:

FATAL:   JSF1073: javax.faces.FacesException caught during processing of RENDER_RESPONSE 6 : UIComponent-ClientId=, Message=Error loading theme, cannot find "theme.css" resource of "primefaces-" library
FATAL:   Error loading theme, cannot find "theme.css" resource of "primefaces-" library
javax.faces.FacesException: Error loading theme, cannot find "theme.css" resource of "primefaces-" library
    at org.primefaces.renderkit.HeadRenderer.encodeTheme(HeadRenderer.java:117)
    at org.primefaces.renderkit.HeadRenderer.encodeBegin(HeadRenderer.java:72)
    at javax.faces.component.UIComponentBase.encodeBegin(UIComponentBase.java:869)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1854)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:443)
    at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
    at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:337)
    at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:337)
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:647)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:344)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
    at filters.AuthenticationFilter.doFilter(AuthenticationFilter.java:136)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:316)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
    at java.lang.Thread.run(Thread.java:745)
My web.xml:

   <?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" 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-app_3_1.xsd">
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <context-param>
        <param-name>primefaces.THEME</param-name>
        <param-value>#{themeSwitcher.theme}</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>/PropertyWS/*</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            60
        </session-timeout>
    </session-config>
    <security-constraint>
        <web-resource-collection>
            <web-resource-name>Security</web-resource-name>
            <url-pattern>*.xhtml</url-pattern>
        </web-resource-collection>
        <user-data-constraint>
            <transport-guarantee>CONFIDENTIAL</transport-guarantee>
        </user-data-constraint>
    </security-constraint>
    <welcome-file-list>
        <welcome-file>/public/index.xhtml</welcome-file>
    </welcome-file-list>
</web-app>

我正在使用Netbeans 8,我已经将JAR添加到库中。我还尝试将它们放在WEB-INF/lib文件夹中,但没有帮助。有什么建议吗?

为什么不使用@ManagedProperty注释注入bean呢?例如,在中学课堂上:

@ManagedBean
@ViewScoped  
public class Foo extends Bar {
    @ManagedProperty(value="#{themeSwitcher}")
    private ThemeSwitcher themeSwitcher;

我试图摆脱ManagedBeans,只使用命名的Bean。并且不能将ManagedProperty从ManagedBean使用到命名的ManagedBean。是否尝试更改包导入?由于您正在从JSF转移到CDI,您可能需要使用javax.enterprise.context.SessionScoped或等效的CDI。