【问题标题】:My EL Expressions are not being evaluated under a MyFaces 2.3 and Spring Boot 2.0.3 application我的 EL 表达式未在 MyFaces 2.3 和 Spring Boot 2.0.3 应用程序下进行评估
【发布时间】: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


【解决方案1】:

经过几天尝试解决这个问题,改变和组合不同的代码和环境,从JDK 8 to 10,Tomcat 8 and 9,在MacOS和Windows 10中进行测试,从JSF 2.1实现MyFaces到全新的JSF 2.3 实现支持CDI,将最后一个部署在WildFly 服务器上,我刚刚注意到,在我昂贵的32 英寸高分辨率显示器上,我的Managed Bean 使用indexBean 命名陌生字符i 顶部有小圆点,这是一个日记ï,我不知道它是如何放置或输入的。 这就是无论我使用什么配置都找不到我的托管 bean 的原因。

我在 MacOS 上注意到了这个字符。在我的 Windows 上似乎更难注意到它。我认为这是因为字体差异,而不是硬件。

我很惭愧,但我不会删除这个问题。希望这可以帮助将来的人。编程和生活中的问题是这样的:大多数时候错误都在意料之外的地方。

经验教训。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-24
    • 2020-12-03
    • 2014-06-21
    • 1970-01-01
    • 2012-12-06
    • 2016-09-18
    • 2011-05-19
    • 1970-01-01
    相关资源
    最近更新 更多