【问题标题】:Add css to JSP on JAX-RS在 JAX-RS 上将 css 添加到 JSP
【发布时间】:2021-03-11 01:53:03
【问题描述】:

我需要在 glassfish5 上运行的 jax-rs 应用程序的 jsp 文件中添加 css 和 js。 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>

    <groupId>com.example</groupId>
    <artifactId>elearning_ontology</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>elearning_ontology</name>
    <packaging>war</packaging>

    <properties>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.source>1.8</maven.compiler.source>
        <junit.version>5.6.2</junit.version>
    </properties>

    <dependencies>

      

        <dependency>
            <groupId>javax.mvc</groupId>
            <artifactId>javax.mvc-api</artifactId>
            <version>1.0.0</version>
        </dependency>

        <dependency>
            <groupId>org.eclipse.krazo</groupId>
            <artifactId>krazo-jersey</artifactId>
            <version>1.1.0-M1</version>
        </dependency>



        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>7.0</version>
        </dependency>

        <dependency>
            <groupId>net.sourceforge.owlapi</groupId>
            <artifactId>owlapi-distribution</artifactId>
            <version>5.1.17</version>

        </dependency>
        <dependency>
            <groupId>com.github.galigator.openllet</groupId>
            <artifactId>openllet-owlapi</artifactId>
            <version>2.6.5</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.3.0</version>
            </plugin>
        </plugins>
    </build>
</project>

我尝试使用相对路径或c:url 添加css,但没有机会:

     <link rel="stylesheet" href="<c:url value = "bootstrap-rtl.min.css"/>" media="screen">

<link rel="stylesheet" href="assets/css/bootstrap-rtl.min.css" media="screen">

当我查看页面源并单击 css url 时,我看到一个 404 页面。我想我应该通过 Application 或 web.xml 进行一些配置。但我不知道如何

P.S:它是一个 restfull mvc 应用程序。所以所有的路由都是通过控制器类来控制的。
所以(也许)只能访问在控制器中定义或在配置文件中定义的路径。相对或绝对路径不起作用。

图片显示了应用程序的结构:

【问题讨论】:

    标签: intellij-idea jax-rs glassfish-5


    【解决方案1】:

    静态文件不需要在 WEB-INF 文件夹中。我们将 JSP 文件放在那里以保护它们不被公开。资产可以直接进入src/main/webapp 文件夹。这是默认 servlet 为静态文件提供服务的根目录。

    假设你有以下结构

    src/main/webapp
               |
               +--- assets
               |      |
               +      +--- css
               |            |
               +            +--- styles.css
               |
               +--- WEB-INF
                      |
                      +--- jsp
                            |
                            +--- products.jsp
    

    您可以使用products.jsp页面中的以下链接

    <link rel="stylesheet" href="<c:url value="/assets/css/styles.css"/>" />
    

    由于我们使用的是c:url,它会一直为我们添加基本 URL 到上下文路径,即http://localhost:8080/&lt;app-name&gt;/。添加到 CSS 页面的路径,我们将拥有

    http://localhost:8080/<app-name>/assets/css/styles.css
    

    这正是我们想要的。当浏览器对该页面发出请求时,默认 servlet 将从我们当前拥有的位置提供它。

    【讨论】:

    • 没关系。控制器只处理 jsp 页面的方向。默认 servlet 将处理浏览器请求的静态页面的检索。
    • 您主页的 URL 是什么?另外我在您的图片中没有看到任何资产文件夹
    • 它是 http://localhost:8080/elearning_ontology-1.0-SNAPSHOT/elearning/homeelearningApplication 中定义的上下文。而home 是控制器方法@path
    • 如果你把所有东西都放在views文件夹中,那么你可以使用相对路径。只是文件名是相对的。
    • 如图所示,css位于我的home.jsp所在的views文件夹中。并使用 href="bootstrap-rtl.min.css" 作为 css 链接位置。但它不会将 css 加载到页面
    猜你喜欢
    • 2014-05-25
    • 1970-01-01
    • 1970-01-01
    • 2011-10-22
    • 2017-03-09
    • 2019-05-26
    • 1970-01-01
    • 1970-01-01
    • 2012-07-01
    相关资源
    最近更新 更多