0

我目前正在尝试将在 wildfly 上运行的 java EE 8 项目移动到最新的 spring boot。

为此,我将需要 primefaces、primefaces-extensions 和 omnifaces,因为它的组件我已经在使用并且还设计(购买)主题。

从最新的 spring boot 和 joinfaces 开始,我目前尝试定义欢迎页面或重定向到 index.xhtml 页面,但我尝试的任何操作都失败了。

这是我目前的来源:

pom.xml

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

<!-- Project settings -->
<groupId>com.skf.ecocalcweb</groupId>
<artifactId>EcoCalcDD4Web</artifactId>
<version>2.0.0</version>
<packaging>jar</packaging>

<!-- Using spring boot as parent -->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.4.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<!-- Project properties -->
<properties>
    <!-- General settings -->
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <java.version>1.8</java.version>
    <!-- Libraries versions -->
    <lombok.version>1.18.12</lombok.version>
    <joinfaces.version>4.1.4</joinfaces.version>
    <primefaces.core.version>7.0.11</primefaces.core.version>
    <primefaces.extensions.version>7.0.3</primefaces.extensions.version>
    <omnifaces.version>3.4.1</omnifaces.version>
    <fontawesome.version>5.12.0</fontawesome.version>
</properties>

<!-- Use joinfaces for jsf starters -->
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.joinfaces</groupId>
            <artifactId>joinfaces-dependencies</artifactId>
            <version>${joinfaces.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<!-- JAR-Dependencies -->
<dependencies>
    <!-- Spring boot starters -->
    <!-- Exclude mojarra, so use myfaces instead -->
    <dependency>
        <groupId>org.joinfaces</groupId>
        <artifactId>primefaces-spring-boot-starter</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.joinfaces</groupId>
                <artifactId>mojarra-spring-boot-starter</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.joinfaces</groupId>
        <artifactId>myfaces-spring-boot-starter</artifactId>
    </dependency>
    <!-- Also use omnifaces3, so latest jsf version must be used -->
    <dependency>
        <groupId>org.joinfaces</groupId>
        <artifactId>omnifaces3-spring-boot-starter</artifactId>
    </dependency>
    <!-- Java Enterprise API for using primefaces in JSF -->
    <dependency>
        <groupId>javax.enterprise</groupId>
        <artifactId>cdi-api</artifactId>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
    <!-- OmniFaces -->
    <dependency>
        <groupId>org.omnifaces</groupId>
        <artifactId>omnifaces</artifactId>
        <version>${omnifaces.version}</version>
    </dependency>
    <!-- Primefaces core -->
    <dependency>
        <groupId>org.primefaces</groupId>
        <artifactId>primefaces</artifactId>
        <version>${primefaces.core.version}-SNAPSHOT</version>
    </dependency>
    <!-- Primefaces extensions -->
    <dependency>
        <groupId>org.primefaces.extensions</groupId>
        <artifactId>primefaces-extensions</artifactId>
        <version>${primefaces.extensions.version}</version>
    </dependency>
    <!-- Primefaces theme -->
    <dependency>
        <groupId>com.skf.ecocalcweb</groupId>
        <artifactId>PrimefacesTheme</artifactId>
        <version>1.0.0</version>
    </dependency>
    <!-- Font Awesome -->
    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>font-awesome</artifactId>
        <version>${fontawesome.version}</version>
    </dependency>
    <!-- Lombok -->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>${lombok.version}</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

<!-- Package as an executable jar -->
<build>
    <!-- Final name of war-file -->
    <finalName>${project.artifactId}</finalName>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

/src/main/java/com/test/Application.java

    @SpringBootApplication
public class Application {

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

  @Bean
  public ServletRegistrationBean facesServletRegistration() {
    FacesServlet servlet = new FacesServlet();
    ServletRegistrationBean registration = new ServletRegistrationBean(servlet, new String[]{"*.xhtml"});
    registration.setName("Faces Servlet");
    registration.setLoadOnStartup(1);
    registration.setEnabled(true);
    registration.setOrder(1);
    return registration;
  }

  @Bean
  public ServletContextInitializer servletContextInitializer() {
    return servletContext -> {
      // MyFaces
      servletContext.setInitParameter("org.apache.myfaces.LOG_WEB_CONTEXT_PARAMS", Boolean.FALSE.toString());
      // Primefaces
      servletContext.setInitParameter("primefaces.THEME", "harmony-fate");
      servletContext.setInitParameter("primefaces.MOVE_SCRIPTS_TO_BOTTOM", Boolean.FALSE.toString());
      servletContext.setInitParameter("primefaces.FONT_AWESOME", Boolean.TRUE.toString());
      // Omnifaces
      servletContext.setInitParameter("org.omnifaces.SOCKET_ENDPOINT_ENABLED", Boolean.TRUE.toString());
      // JavaX
      servletContext.setInitParameter("javax.faces.PROJECT_STAGE", "Production");
      servletContext.setInitParameter("javax.faces.DEFAULT_SUFFIX", ".xhtml");
      servletContext.setInitParameter("com.sun.faces.enableRestoreView11Compatibility", Boolean.TRUE.toString());
      servletContext.setInitParameter("javax.faces.FACELETS_SKIP_COMMENTS", Boolean.TRUE.toString());
      servletContext.setInitParameter("javax.faces.STATE_SAVING_METHOD", "server");
      servletContext.setInitParameter("com.sun.faces.compressViewState", Boolean.FALSE.toString());
      servletContext.setInitParameter("com.sun.face.serializeServerState", Boolean.FALSE.toString());
      servletContext.setInitParameter("javax.faces.FACELETS_REFRESH_PERIOD", "-1");
      servletContext.setInitParameter("javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL", Boolean.TRUE.toString());
      servletContext.setInitParameter("javax.faces.DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE", Boolean.TRUE.toString());
      servletContext.setInitParameter("javax.faces.ENABLE_CDI_RESOLVER_CHAIN", Boolean.TRUE.toString());
    };
  }
}

/src/main/java/com/test/controllers/Index.java

@Getter
@Setter
@RequestScope
@Named
public class Index {

  private String firstName = "John";
  private String lastName = "Doe";

  public String showGreeting() {
    return "Hello " + firstName + " " + lastName + "!";
  }
}

/src/main/resources/META-INF/resources/index.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:p="http://primefaces.org/ui">

<h:head>
  <title>PrimeFaces Hello World Example</title>
</h:head>

<h:body>
  <h:form id="helloworld-form">

    <p:panel header="PrimeFaces Hello World Example">
      <h:panelGrid columns="2" cellpadding="4">
        <h:outputText value="First Name: " />
        <p:inputText id="first-name" value="#{index.firstName}" />

        <h:outputText value="Last Name: " />
        <p:inputText id="last-name" value="#{index.lastName}" />

        <p:commandButton id="submit" value="Submit"
          update="greeting-panel"
          oncomplete="PF('greetingDialog').show()" />
      </h:panelGrid>
    </p:panel>

    <p:dialog header="Greeting" widgetVar="greetingDialog"
      modal="true" resizable="false">
      <h:panelGrid id="greeting-panel" columns="1" cellpadding="4">
        <h:outputText value="#{index.showGreeting()}" />
      </h:panelGrid>
    </p:dialog>

  </h:form>
</h:body>
</html>

/src/main/resources/META-INF/resources/application.properties

# the embedded application server config
server.port=80
server.servlet.context-path=/

# thymeleaf templating engine config
spring.thymeleaf.enabled=false

那么这里需要什么来正确设置 jsf 应用程序的欢迎页面?

当我直接调用 http://localhost/index.xhtml 时,primefaces 页面将正确呈现,但是当我输入 http://localhost/时 ,不会重定向到 index.xhtml 并且我目前没有发现任何工作“重定向” ...

4

1 回答 1

2

我想我目前找到了解决方案:
所以我将它发布给所有其他开始使用 spring boot 和 primefaces 的人:

添加此依赖项将解决我的欢迎页面问题,如 xhtml :

<!-- Add spring boot web needed for navigation -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

然后我可以使用以下类文件来设置我的欢迎页面:

/src/main/java/com/test/navigation/WelcomePageRedirect.java

@Configuration
public class WelcomePageRedirect implements WebMvcConfigurer {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/")
                .setViewName("forward:/index.xhtml");
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
    }
}

现在,以下技术正在协同工作:

  • Java 8(还有 Java 11!)
  • Primefaces 7.0.11 + Primefaces 扩展
  • Omnifaces 3.4.1
  • 最新的 FontAwesome 5.12.0 与 Primefaces“旧”FontAwesome 相结合
  • 春季启动 2.2.4
  • JoinFaces 4.1.4
  • 我的面孔 2.3
于 2020-02-20T07:53:54.997 回答