Add css to JSP on JAX-RS

193 views Asked by At

I need to add css and js to the jsp files in a jax-rs app running on glassfish5. 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>

i tryed to add css using relative path or c:url but no chance:

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

when i view page source and click on css url i see a 404 page. i guess i should do some config via Application or web.xml .but i dont know how

P.S: it is a restfull mvc app. so all routes are controlled through the controller class .
so (maybe) there is only access to paths that are defined in controller or defined in a config file. relative or absolute paths not work.

the picture shows the structure of the app: enter image description here

1

There are 1 answers

9
Paul Samsotha On BEST ANSWER

Static files don't need to be in the WEB-INF folder. We put JSP files in there to protect them from the public. The assets can go straight in the src/main/webapp folder. This is the root where the default servlet will serve the static files.

So let's say you have the following structure

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

What you can use is the following link in you products.jsp page

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

Since we are using c:url, it will add the base URL for us all the way to the context-path, wich would be http://localhost:8080/<app-name>/. Adding the path to the CSS page, we would have

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

This is exactly what we want. When the browser makes the request for this page, the default servlet will serve it up from where we currently have it.