【问题标题】:JSF + Tomcat losing CSSJSF + Tomcat 丢失 CSS
【发布时间】:2014-04-20 22:42:34
【问题描述】:

我正在使用 JSF 2.2、Netbeans 7.4、Primefaces 3.5 Tomcat 7 和 Hibernate 3.6.10。

我正在将一个已完成且运行良好的 Web 项目从普通库和 glassfish 3.1.2 迁移到 maven 和 Tomcat 7.0.41.0。

最初一切似乎都正常,主页开始正常,并且可以使用他的所有 CSS 等完美查看,但是当我按下任何链接转到另一个部分时,所有 CSS 似乎都丢失了(甚至primefaces 风格)。

导航继续正常工作,但未应用 CSS。

Tomcat 控制台出现错误:

Grave: El Servlet.service() para el servlet [Faces Servlet] en el contexto con ruta [/AuPair] lanzó la excepción [javax/servlet/jsp/jstl/core/Config] con causa raíz
java.lang.NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config

我在网上搜索了这个错误,没有结果,我不知道它是否与我的问题有关。

我正在使用这样的模板:

<html lang="#{sesion.idiomaActual.country}"
      xmlns="http://www.w3.org/1999/xhtml"
  xmlns:ui="http://java.sun.com/jsf/facelets"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:f="http://java.sun.com/jsf/core">

 <f:view>
    <h:head>
        <link href="CSS/estilos.css" rel="stylesheet" type="text/css"/>
    </h:head>
    <h:body>           
        <ui:composition template="/plantilla.xhtml">
            <ui:define name="cabecera">
                <ui:include src="cabecera_vista.xhtml"/>
            </ui:define>
            <ui:define name="contenido">
                <ui:include src="index_vista.xhtml"/>
            </ui:define>
            <ui:define name="pie">
                <ui:include src="pie_vista.xhtml"/>
            </ui:define>
        </ui:composition>        
    </h:body>
  </f:view>
</html>

这是显示“index_vista”的第一页(起初看起来不错,但如果你离开然后回来就不行了):

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

  <f:view locale="#{sesion.idiomaActual}">
    <h:head>
        <link href="CSS/estilos.css" rel="stylesheet" type="text/css"/>           
    </h:head>
    <h:body>
        <h:form>            
            <div id="inicio" class="area_contenido"> 
                <br/>                                
                <p:imageSwitch effect="fade"  styleClass="galeria_fotos">  
                    <ui:repeat value="#{index_controlador.listafotos}" var="image">  
                        <p:graphicImage value="/Imagenes/#{image}" />  
                    </ui:repeat>  
                </p:imageSwitch> 

                <p:fieldset legend="#{msjs.inicio_tit1}" styleClass="panel_inicio" >   
                    <p:graphicImage value="/Imagenes/inicio1.jpg" styleClass="imagen_panel_inicio1"/>  
                    <h:outputText styleClass="texto_panel_inicio" escape="false" value="#{msjs.inicio_text1}" />                     
                </p:fieldset>  

                <p:fieldset legend="#{msjs.inicio_tit2}" styleClass="panel_inicio" >                       
                    <br/>
                    <h:outputText styleClass="texto_panel_inicio" escape="false" value="#{msjs.inicio_text2}" />

                    <p:graphicImage value="/Imagenes/inicio2.jpg" styleClass="imagen_panel_inicio2" alt="Foto Apreton manos"/>  
                </p:fieldset>  

                <p:fieldset legend="#{msjs.inicio_tit3}" styleClass="panel_inicio" >   
                    <p:graphicImage value="/Imagenes/inicio3.jpg" styleClass="imagen_panel_inicio3"/>  
                    <br/>
                    <h:outputText styleClass="texto_panel_inicio" escape="false" value="#{msjs.inicio_text3}"  />                     
                </p:fieldset>  

            </div>         
        </h:form>
    </h:body>
  </f:view> 
</html>

这是我的 POM 文件:

<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>com.jesus</groupId>
<artifactId>XXX</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>

<name>XXX</name>

<properties>
    <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies> 
    <dependency>
        <groupId>javassist</groupId>
        <artifactId>javassist</artifactId>
        <version>3.12.1.GA</version>
    </dependency>       
    <dependency>
        <groupId>org.glassfish</groupId>
        <artifactId>javax.faces</artifactId>
        <version>2.2.2</version>
    </dependency>
    <dependency>
        <groupId>org.primefaces</groupId>
        <artifactId>primefaces</artifactId>
        <version>3.5</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.6</version>
    </dependency>
    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.3</version>
    </dependency>
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.4</version>
    </dependency>
    <dependency>
        <groupId>com.lowagie</groupId>
        <artifactId>itext</artifactId>
        <version>2.1.7</version>
    </dependency>
    <dependency>
        <groupId>org.primefaces.themes</groupId>
        <artifactId>redmond</artifactId>
        <version>1.0.9</version>           
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>3.9</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>3.6.10.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>3.6.10.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-commons-annotations</artifactId>
        <version>3.2.0.Final</version>
    </dependency>
    <dependency>
        <groupId>javax.transaction</groupId>
        <artifactId>jta</artifactId>
        <version>1.1</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.0-api</artifactId>
        <version>1.0.1.Final</version>
    </dependency>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-web-api</artifactId>
        <version>6.0</version>
        <scope>provided</scope>
    </dependency>

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
                <compilerArguments>
                    <endorseddirs>${endorsed.dir}</endorseddirs>
                </compilerArguments>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.1.1</version>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.1</version>
            <executions>
                <execution>
                    <phase>validate</phase>
                    <goals>
                        <goal>copy</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${endorsed.dir}</outputDirectory>
                        <silent>true</silent>
                        <artifactItems>
                            <artifactItem>
                                <groupId>javax</groupId>
                                <artifactId>javaee-endorsed-api</artifactId>
                                <version>6.0</version>
                                <type>jar</type>
                            </artifactItem>
                        </artifactItems>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
<repositories>
    <repository>
        <url>http://repository.primefaces.org/</url>
        <id>PrimeFaces-maven-lib</id>
        <layout>default</layout>
        <name>Repository for library PrimeFaces-maven-lib</name>
    </repository>
</repositories>

项目结构是这样的:

http://i61.tinypic.com/2h3sfmo.jpg

我是 Tomcat 的初学者,有人可以帮我吗? 提前致谢!!

【问题讨论】:

    标签: java css jsf tomcat


    【解决方案1】:

    最后我自己发现了问题。 Tomcat 不适用于相对路径,而 Glassfish 可以。 所以不要使用:

    <link href="CSS/estilos.css" rel="stylesheet" type="text/css"/>
    

    使用这个:

    <link href="#{request.contextPath}/CSS/estilos.css" rel="stylesheet" type="text/css"/> 
    

    【讨论】:

      【解决方案2】:

      试试

      <h:outputStylesheet library="CSS" name="estilos.css"  />
      

      【讨论】:

      • 嗨!恐怕我已经尝试过了,但它不起作用。
      • 你检查浏览器是否加载了css文件?
      • 第一次加载“estilos.CSS”文件的第一页加载了50条规则(mozilla firefox样式编辑器),但是每当我导航到另一个页面时,文件就不再加载(0规则)。奇怪的是,当我在样式编辑器菜单中选择“estilos.CSS”时,会出现错误网页“error.xhtml”的HTML代码。所以我想CSS位置丢失了。知道发生了什么吗?
      猜你喜欢
      • 2013-11-04
      • 2012-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 2015-04-18
      • 1970-01-01
      相关资源
      最近更新 更多