gpt4 book ai didi

java - Tomcat 7、JSF 2.0 和@PostConstruct

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:56:55 24 4
gpt4 key购买 nike

我不知道我做错了什么,请帮忙:

  1. 新鲜的 Tomcat 7,/lib 文件夹中没有多余的 jar
  2. 在 WEB-INF/lib(jsf-api.jar、jsf-impl.jar)中带有 mojarra 2.0.3 库的简单网络应用
  3. 工作正常,除了我的 bean 中的 @PostConstruct - 它们根本没有被调用

日志:

Mar 12, 2011 11:19:54 PM org.apache.catalina.startup.HostConfig deployWAR
INFO: Deploying web application archive test_web_app.war
Mar 12, 2011 11:19:54 PM com.sun.faces.config.ConfigureListener contextInitialized
INFO: Initializing Mojarra 2.0.3 (FCS b03) for context '/test_web_app'
Mar 12, 2011 11:19:54 PM com.sun.faces.spi.InjectionProviderFactory createInstance
INFO: JSF1048: PostConstruct/PreDestroy annotations present. ManagedBeans methods marked with these annotations will have said annotations processed.

网络.xml

<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_3_0.xsd"
version="3.0">

<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>

faces-config.xml

<faces-config 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-facesconfig_2_0.xsd"
version="2.0">
</faces-config>

方法不可达的Bean:

@ManagedBean
@ApplicationScoped
public class AppBean {

@PostConstruct
public void test() {
throw new RuntimeException("test");
}
}

就是这样。有什么想法吗?

最佳答案

如果您的应用程序范围内的托管 bean 没有在任何页面上使用,您必须用它进行注释

@ManagedBean(eager=true)

为了在启动时对其进行初始化。

关于java - Tomcat 7、JSF 2.0 和@PostConstruct,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5285293/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com