java - JSF 标签不会在 XHTML 页面中呈现 - Spring Boot + JSF 2.2

标签 java spring spring-boot jsf

我正在尝试使用 Spring Boot 和 JSF 2.2 实现一个 Web 应用程序。

我已经成功地正确设置了 Spring Boot,并且可以使用 Controller 显示正常的 HTML。但 XHTML 页面无法正确呈现。

我相信这与我的配置有关,但我无法弄清楚它是什么。

我在 macOS High Sierra 上使用 IntelliJ IDEA Ultimate 2018.1。

这是我的 pom.xl

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

    <groupId>group</groupId>
    <artifactId>spring-jsf-webapp</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
    </parent>

    <properties>
        <start-class>com.latwoa.Application</start-class>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-api</artifactId>
            <version>2.2.17</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-impl</artifactId>
            <version>2.2.17</version>
            <scope>compile</scope>
            <optional>true</optional>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!-- Package as an executable jar/war -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

我的 web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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_4_0.xsd"
     version="4.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>*.xhtml</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.xhtml</welcome-file>
    </welcome-file-list>
</web-app>

我的 faces-config.xml:

<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.2" 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">

    <application>
        <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
    </application>

</faces-config>

以及 JSF bean:

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean
@RequestScoped
public class JsfBean {

    private String welcomeMessage = "Populated by JSF created bean";

    public String getWelcomeMessage() {
        return welcomeMessage;
    }
}

和 XHTML 页面:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
   xmlns:ui="http://java.sun.com/jsf/facelets"
   xmlns:h="http://xmlns.jcp.org/jsf/html"
   xmlns:f="http://xmlns.jcp.org/jsf/core"
   xmlns:jsf="http://xmlns.jcp.org/jsf">
<f:view>

    <h:head></h:head>

    <!--Main Body -->
    <h:body>

        <h1>Welcome to JSF spring boot tutorial</h1>

        <h:outputText value="foo bar" />

        <h3>#{jsfBean.welcomeMessage}</h3>

    </h:body>

</f:view>
</html>

就像我上面提到的,<h1>显示良好。但是<h:outputText value="foo bar" />#{jsfBean.welcomeMessage}没有渲染。

最佳答案

在此列出 https://dzone.com/articles/developing-jsf-applications-with-spring-boot

您似乎缺少使用 Spring Boot 配置 JSF 的步骤

@EnableAutoConfiguration
@ComponentScan({"com.auth0.samples.bootfaces"})
public class Application extends SpringBootServletInitializer {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
    @Bean
    public ServletRegistrationBean servletRegistrationBean() {
        FacesServlet servlet = new FacesServlet();
        return new ServletRegistrationBean(servlet, "*.jsf");
    }
}

我还会添加这些监听器以将 spring 上下文与您的托管 bean 集成:

   <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>

关于java - JSF 标签不会在 XHTML 页面中呈现 - Spring Boot + JSF 2.2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50992644/

相关文章:

java - 通过 Intent 传递数据

java - 在 Spring Boot 2.1.1.RELEASE 中的 RestTemplate 中添加 Http header

java - Spring MVC : empty value for form field of type long results in 400 bad request

java - SonarQube 在 Spring Framework Controller 和 Spring Framework 应用程序主类中显示安全错误

java - 如何从 spring-boot-starter-parent 中排除特定的依赖项

java - 如何调用 setMargins();在android中的自定义ViewGroup上?

java - 在java中读取属性文件的最佳方法?

java - 有没有从javascript到Java bean(spring bean)执行RPC的框架?

java - org.springframework.beans.factory.BeanCreationException 创建名为 'certificateOrderRepository' 的 bean 时出错

java - 创建自定义 OAuth 登录页面和自定义 jwt token