当前位置: 首页 > 知识库问答 >
问题:

带Primefaces问题的Spring Boot

栾耀
2023-03-14

Spring启动2.4.1 Primeface 8

我有两个问题:h:outputStylesheet没有呈现链接标记,因此我在浏览器中没有获得任何样式。primefaces菜单栏正在呈现一个普通的旧ul列表。

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>org.gca</groupId>
    <artifactId>GCA-WEB</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>GCA-WEB</name>
    <description>Website for Gleneagle Civic Association (HOA)</description>

    <properties>
        <java.version>11</java.version>
        <primefaces.version>8.0</primefaces.version>
        <jsf.ver>2.2.20</jsf.ver>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <!--        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.primefaces</groupId>
            <artifactId>primefaces</artifactId>
            <version>${primefaces.version}</version>
        </dependency>
        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-api</artifactId>
            <version>${jsf.ver}</version>
        </dependency>
        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-impl</artifactId>
            <version>${jsf.ver}</version>
        </dependency>
        <dependency>
            <groupId>javax.el</groupId>
            <artifactId>javax.el-api</artifactId>
            <version>3.0.0</version>
            <scope>provided</scope>
        </dependency>
               
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

JSF配置:

package org.gca.config;

import com.sun.faces.config.ConfigureListener;
import java.util.Arrays;
import javax.faces.webapp.FacesServlet;
import javax.servlet.ServletContext;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
public class JsfConfig {

    @Bean
    ServletRegistrationBean jsfServletRegistration(ServletContext servletContext) {
        //spring boot only works if this is set
        servletContext.setInitParameter("com.sun.faces.forceLoadConfiguration", Boolean.TRUE.toString());

        //registration
        ServletRegistrationBean srb = new ServletRegistrationBean();
        srb.setServlet(new FacesServlet());
        srb.setUrlMappings(Arrays.asList("*.xhtml"));
        srb.setLoadOnStartup(1);
        return srb;
    }

    @Bean
    public ServletListenerRegistrationBean<ConfigureListener> jsfConfigureListener() {
        return new ServletListenerRegistrationBean<>(
                new ConfigureListener());
    }

    @Bean
    public ViewResolver getViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setSuffix(".xhtml");
        return resolver;
    }
}

我手动创建了webapps/WEB-INF资源目录

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

模板:

<?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:ui="http://java.sun.com/jsf/facelets">
    <h.head>
        <h:outputStylesheet library="css" name="gca.css"/>
        <title>
            <ui:insert name="title">GCA</ui:insert>
        </title>
        <ui:insert name="style"></ui:insert>
        <ui:insert name="script"> </ui:insert>
    </h.head>
    <h.body>
        <h:form id="bannerForm">
            <ui:insert name="banner">
                <h:panelGroup styleClass="bannerPanel" layout="block">
                    <h:panelGrid
                        id="bannerGrid" 
                        columns="2" columnClasses="bannerColLeft, bannerColRight"
                        style="margin: 0px;padding: 0px;width:100%;">
                        <h:graphicImage
                            url="/resources/images/GleneagleSign.png"
                            style="height: 100px;width: 100px;"/>
                        <h:outputText value="Gleneagle Civic Association"/>
                    </h:panelGrid>
                </h:panelGroup>
            </ui:insert>
            <ui:insert name="menu">
                <ui:include id="mnuIncld" src="gcaMenu.xhtml"/>
            </ui:insert>
        </h:form>
        <h:panelGroup id="workPanel" layout="block" styleClass="alignLeft" style="margin-top:100px;">
            <h:panelGrid id="wrkPnlGrd" columns="1" styleClass="alignLeft" columnClasses="workColumnLeft" rowClasses="workRow">
                <h:outputLabel id="ajaxErrMsg" styleClass="errorLeft"/>
                <ui:insert name="work">

                </ui:insert>
            </h:panelGrid>
        </h:panelGroup>
        <ui:insert name="bottomScript">

        </ui:insert>
    </h.body>
</html>

菜单:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html  xmlns="http://www.w3.org/1999/xhtml"
       xmlns:ui="http://java.sun.com/jsf/facelets"
       xmlns:p="http://primefaces.org/ui">
    <ui:composition id="mnuComp">
        <p:menubar>
            <p:menuitem value="Home" url="#"/>
            <p:submenu label="About GCA">
                <p:menuitem value="Leadership" action="/about/leadership"/>
                <p:menuitem value="Our History" action="/about/history"/>
                <p:menuitem value="Map of GCA" action="/about/map"/>
                <p:menuitem value="Surrounding HOAs" action="/about/hoas"/>
                <p:menuitem value="Related Links" action="/about/links"/>
            </p:submenu>
            <p:submenu label="Documents">
                <p:submenu label="Board Meetings">
                    <p:menuitem value="Board Meeting Agenda" action="/doc/board/agenda"/>
                    <p:menuitem value="Board Meeting Minutes" action="/doc/board/minutes"/>
                </p:submenu>
                <p:submenu label="Membership Meetings">
                    <p:menuitem value="Membership Meeting Minutes" action="/doc/member/minutes"/>
                </p:submenu>
                <p:submenu label="Architectural Control">
                    <p:menuitem value="Approval Process"  action="/doc/archCtl/approval"/>
                    <p:menuitem value="Variance Process"  action="/doc/archCtl/variance"/>
                    <p:menuitem value="House Color Standards"  action="/doc/archCtl/houseColors"/>
                    <p:menuitem value="Mailbox Specifications"  action="/doc/archCtl/mailbox"/>
                </p:submenu>
                <p:submenu label="Covenants">
                    <p:menuitem value="Covenants, Restrictions and Charges" action="/doc/covenant"/>                            
                    <p:menuitem value="Amendment One" action="/doc/covenant/ammendment1"/>                            
                    <p:menuitem value="Amendment Four" action="/doc/covenant/ammendment4"/>
                    <p:menuitem value="Rules &amp; Regulations" action="/doc/covenant/ruleRegs"/>
                    <p:menuitem value="Fine Schedule" action="/doc/covenant/findSchedule"/>
                </p:submenu>
            </p:submenu>
            <p:submenu label="Contact">
                <p:menuitem value="Feedback" action="/contact/feedback"/>
                <p:menuitem value="Join Mailing List" action="/contact/mailList"/>
            </p:submenu>
        </p:menubar>
        <p:growl id="messages"/>
    </ui:composition>
</html>

我还试图以编程方式创建菜单,但菜单托管Bean没有被实例化:

package org.gca.mbeans;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.primefaces.model.menu.DefaultMenuItem;
import org.primefaces.model.menu.DefaultMenuModel;
import org.primefaces.model.menu.DefaultSubMenu;
import org.primefaces.model.menu.MenuModel;
import org.springframework.stereotype.Component;

@Slf4j
@Getter
@Component
@ManagedBean(name = "menuBean")
@ViewScoped
public class MenuMB {

    private final MenuModel menuModel = new DefaultMenuModel();
    
    @PostConstruct
    public void postConstruct() {
        addMenu("Home");
        addMenu("About GCA", "Leadership", "Our History", "Map of GCA", "Surrounding HOAs", "Related Links");
        addMenu("Documents");
        addMenu("Contacts");
        log.debug("Added 4 menu items");
    }

    public DefaultSubMenu addMenu(String label, String... items) {
        return addMenu(null, label, items);
    }

    public DefaultSubMenu addMenu(DefaultSubMenu parentMenu,
            String label, String... items) {
        DefaultSubMenu theMenu = new DefaultSubMenu(label);
        for (Object item : items) {
            DefaultMenuItem mi = new DefaultMenuItem(item);
            mi.setUrl("#");
            theMenu.getElements().add(mi);
        }
        if (parentMenu == null) {
            menuModel.addElement(theMenu);
        } else {
            parentMenu.getElements().add(theMenu);
        }
        return theMenu;
    }

}

指数xhtml

<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:p="http://primefaces.org/ui">
    <body>
        <ui:composition template="/gcaMain.xhtml">
            <ui:define name="work">
                <p:menubar model="#{menuBean.menuModel}"/>[enter image description here][1]
            </ui:define>
        </ui:composition>
    </body>
</html>

共有1个答案

鲁德佑
2023-03-14

以下@Melloware建议:

pom.xml更新

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.joinfaces</groupId>
                <artifactId>joinfaces-dependencies</artifactId>
                <version>${joinfaces.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.primefaces.themes</groupId>
                <artifactId>overcast</artifactId>
                <version>${primefaces-theme.version}</version>
            </dependency>
        </dependencies>        
    </dependencyManagement>
........
        <dependency>
            <groupId>org.joinfaces</groupId>
            <artifactId>primefaces-spring-boot-starter</artifactId>
        </dependency>
<!--        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.primefaces</groupId>
            <artifactId>primefaces</artifactId>
            <version>${primefaces.version}</version>
        </dependency>
        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-api</artifactId>
            <version>${jsf.ver}</version>
        </dependency>-->


删除了JmsConfig文件和面配置。xml

更正模板中的打字错误

 类似资料:
  • 我正在尝试在PrimeFaces中使用accordionPanel实现paginator(或类似的东西),PrimeFaces演示网站中没有一个示例,我也在web中搜索过,是否可以使用accordionPanel实现paginator?

  • 我正在构建一个JSF表单,其中包含一个primefaces p:autoComplete组件。下面是我的xhtml页面的摘录,显示了有关自动完成组件的相关信息。 请注意,是类的一个实例,而 返回一个列表 <代码>宇宙卡。classname和CosmoCard。id和代码都是字符串。 我知道我很讨厌POJO,但由于我所有的值都是POJO中的字符串字段,我认为我不需要转换器。无论如何,我的“自动完成”

  • 我使用的是PrimeFaces 2.2和JSF 2.0.3。 我有一个包含以下数据表的xhtml视图: ..具有以下背衬豆: 注意母亲姓名列上的“commandLink”和出生日期列上的“sortBy”。 我遇到了一个奇怪的问题,似乎仅限于IE,如果我按DOB对数据进行排序,然后分页到最后一页并单击表中最后一条记录的commandLink,它会触发两个action事件。第一个事件正确地报告了我单击

  • 晚上好,有经验的人可以告诉我如何制作一个像下面这样的pnel吗?我想制作一个面板,并在里面嵌入一个,这是可能的吗?如果不是,那么如何将包装在下面这样的内容中呢?下面是我想要创建一个类似于它的实际面板: 注意:我尝试了,但我认为它与图像中的不同

  • Microsoft Windows[版本10.0.18362.1016](c)2019 Microsoft Corporation。保留所有权利。