您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Spring Boot和JSF / Primefaces / Richfaces

Spring Boot和JSF / Primefaces / Richfaces

在用Spring Boot设置FacesServlet ,大惊小怪地看到了这个问题 ,我通过添加几个配置文件以及来自spring-web的ServletRegistrationBean使其与JSF / primefaces一起使用。web.xml和faces-config.xml文件对Spring Boot的最小化设置(无大量xml文件)的想法有点伤害,但这是我可以用JSF找到的最小化设置,如果有人知道更好/更干净的话方式,请让我知道。

这是我的gradle.build文件中的依赖项:

compile group: "org.springframework.boot", name: "spring-boot-starter"
compile group: "org.springframework", name: "spring-web", version: "4.0.2.RELEASE"

compile group: "org.apache.tomcat.embed", name: "tomcat-embed-core", version: tomcatVersion
compile group: "org.apache.tomcat.embed", name: "tomcat-embed-logging-juli", version: tomcatVersion
compile group: "org.apache.tomcat.embed", name: "tomcat-embed-jasper", version: tomcatVersion

compile group: "org.primefaces", name: "primefaces", version: "4.0"
compile group: "com.sun.faces", name: "jsf-api", version: "2.1.21"
compile group: "com.sun.faces", name: "jsf-impl", version: "2.1.21"

哪里tomcatVersion是“ 7.0.34”。这里的主要更改是:

这是我的主班的新内容

package com.hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.embedded.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import javax.faces.webapp.FacesServlet;

@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public ServletRegistrationBean servletRegistrationBean() {
        FacesServlet servlet = new FacesServlet();
        return new ServletRegistrationBean(servlet, "*.xhtml");
    }
}

@EnableAutoConfiguration 如果您希望Spring Boot自动设置Tomcat,并且ServletRegistrationBean将xhtml请求映射到FacesServlet。

我的webapp目录中的WEB-INF / faces-config.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<faces-config 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/web-facesconfig_2_2.xsd"
              version="2.2">
    <application>
        <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
    </application>
</faces-config>

并且web.xml文件也位于webapp / WEB-INF中:

<?xml version="1.0" encoding="UTF-8"?>
<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_2_5.xsd"
           version="2.5">

    <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>
</web-app>

servlet定义似乎是多余的,但是由于某些原因,如果我从web.xml文件或servlet注册bean中删除它们,则xhtml页面的呈现将无法正常工作或根本无法工作。编辑:原来,在web.xml文件中不需要servlet映射,这只是一个IDE问题,在我的情况下,Intellij Idea不知道在servlet注册bean中定义了servlet映射,因此它抱怨关于缺少期望的servlet的映射的信息,但是该应用程序仍然可以正常运行。

感谢所有贡献者。

Java 2022/1/1 18:13:42 有595人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶