【发布时间】:2018-12-17 16:51:45
【问题描述】:
我有一个在Spring Boot 2.0.3 下运行的MyFaces 应用程序,所有Servlets 和Context Listeners 都已正确注册和配置,并且显然可以正常工作。甚至我的index.jsf 页面也正在呈现。标记在.xhtml 文件上得到正确处理。
问题是 index.jsf 页面的所有EL expressions 都没有被处理/评估。没有引发错误,但我放置#{myBean.property} 的位置始终呈现为空String。调试它我看到我的托管 bean 的服务器代码没有被调用。
我尝试为许多版本更改 el-api 和 el-impl 库,但没有一个起作用。我使用的最终版本是el-api 2.2 规范,紧随页面https://myfaces.apache.org/core23/myfaces-impl/dependencies.html
由于没有抛出错误,我无法弄清楚问题出在哪里。有人有这个错误吗?是否可以在打包为 jar 文件的 Spring Boot 应用程序下运行 MyFaces 2.3?
以下是我在 Gradle 构建文件中使用的依赖项:
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
compile("org.jetbrains.kotlin:kotlin-reflect")
testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile('org.springframework.boot:spring-boot-starter-test')
compile 'io.github.microutils:kotlin-logging:1.5.4'
compile group: 'org.apache.myfaces.core', name: 'myfaces-impl', version: '2.3.1'
compile group: 'org.apache.myfaces.core', name: 'myfaces-api', version: '2.3.1'
compile group: 'javax.enterprise', name: 'cdi-api', version: '2.0' //CDI vem embutido no JavaEE 6, mas não no Tomcat 9
compile group: 'org.glassfish.web', name: 'el-impl', version: '2.2'
// https://mvnrepository.com/artifact/org.springframework.boot/spring-boot
compile 'org.springframework.boot:spring-boot-starter-web:2.0.3.RELEASE'
compile 'org.apache.tomcat.embed:tomcat-embed-jasper:8.5.32'
compile 'com.fasterxml.jackson.module:jackson-module-kotlin:2.9.6'
compile group: 'org.ocpsoft.rewrite', name: 'rewrite-servlet', version: '3.4.2.Final'
}
这是用于加载 Faces servlet 的 Spring 配置:
@Component
open class ConfigureJSF : ServletContextInitializer {
private val logger = KotlinLogging.logger {}
@Throws(ServletException::class)
override fun onStartup(servletContext: ServletContext) {
//necessary to myfaces be enabled and work in spring boot, once servlets are loaded dynamically.
servletContext.setInitParameter("org.apache.myfaces.INITIALIZE_ALWAYS_STANDALONE", "true")
servletContext.setInitParameter("com.sun.faces.forceLoadConfiguration", "true");
servletContext.setInitParameter("javax.faces.FACELETS_SKIP_COMMENTS", "true");
servletContext.setInitParameter("javax.faces.DEFAULT_SUFFIX", ".xhtml")
servletContext.setInitParameter("javax.faces.FACELETS_REFRESH_PERIOD", "1")
servletContext.setInitParameter("org.apache.myfaces.EXPRESSION_FACTORY", "com.sun.el.ExpressionFactoryImpl")
servletContext.setInitParameter("org.apache.myfaces.CACHE_EL_EXPRESSIONS", "alwaysRecompile")
}
// Register ServletContextListener, necessary for Myfaces.
@Bean
open fun listenerRegistrationBean1(): ServletListenerRegistrationBean<ServletContextListener> {
val bean = ServletListenerRegistrationBean<ServletContextListener>()
bean.setListener(org.apache.myfaces.webapp.StartupServletContextListener())
return bean
}
@Bean
fun requestContextListener(): RequestContextListener {
return RequestContextListener()
}
//The faces servlet
@Bean
open fun facesServlet(): ServletRegistrationBean<MyFacesServlet> {
logger.info { "Criando Faces Servlet..." }
val servlet = org.apache.myfaces.webapp.MyFacesServlet() ;
val servletRegistrationBean = ServletRegistrationBean(servlet, "*.jsf", "*.xhtml")
servletRegistrationBean.setLoadOnStartup(1)
// servletRegistrationBean.order = 1;
return servletRegistrationBean;
}
编辑:
我复制了另一个工作的项目的依赖配置,并且发生了相同的结果。所以,问题不在于我在此处粘贴的代码,是的,与我的环境有关,我将开始对序列进行更详细的调查。我有问题的环境包含JDK 8, 9 and 10 和Tomcat 9。我的项目针对JDK 8。可能这里有一些不兼容,这就是一些编译的注解没有找到的原因,我相信我很快就会发现问题。
【问题讨论】:
-
我也按照所有这些说明进行操作,但没有成功。 stackoverflow.com/questions/45682309/…
-
1) 我不会添加另一个 EL impl 2) 你使用什么样的 ManagedBeans? JSF Native 还是 Spring?
-
@tandraschko 谢谢。我最近尝试了其他 EL 和 JSF impl,结果是一样的。我使用本机托管 Bean。由于我还使用了另一个项目的相同依赖项,但它不起作用,我很确定问题出在我的环境中。因为我安装了 JDK 8,9 和 10,我才意识到我的带注释的类可能因为这个原因而找不到。我正在针对 Java 8 进行编译,我将测试更多观察正在使用的 JDK。我也使用 Tomcat 9。不抛出错误很困难,但现在我想我很快就会找到解决方案。
-
我将我的新发现添加为编辑,以帮助更多可能面临同样问题的用户。
标签: java spring-boot kotlin myfaces