Incorporating CSS and JavaScript into a Spring application

I have been searching on stackoverflow for a solution to my problem, but I am still struggling to make it work despite following the suggested steps.

What I want to achieve is to include static files (css, js, img) in my project and access them using HTML tags.

This is my project structure :

https://i.sstatic.net/5bfVu.jpg

Everything was working fine in my project until I tried adding the CSS. Following other suggestions, I added this line to my dispatcher file :

<mvc:resources mapping="/**" location="/jsp/website/" />

However, after adding this line, I encountered deployment issues and received the following error message :

GlassFish Server, deploy, null, false

Note :

I made some changes in the configuration files to remove the .htm extension from the URL.

This is how my dispatcher looks like:

<mvc:resources mapping="/**" location="/" />
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

<!--
Most controllers will use the ControllerClassNameHandlerMapping above, but
for the index controller we are using ParameterizableViewController, so we must
define an explicit mapping for it.
-->
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
        <props>
            <prop key="/">indexController</prop>
        </props>
    </property>
</bean>

<bean id="viewResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver"
      p:prefix="/WEB-INF/jsp/"
      p:suffix=".jsp" />

<!--
The index controller.
-->
<bean name="indexController"
      class="indexContr"/>

My web.xml file:

<web-app version="3.1" 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 http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>
<welcome-file-list>
    <welcome-file>/</welcome-file>
</welcome-file-list>

Thank you for any assistance you can provide.

Answer №1

  1. All available namespace for the dispatcher servlet has been mapped as resources, but it's recommended to reduce it from /** to /jsp/website/**

    <mvc:resources mapping="/resources/**" location="/jsp/website/" />

  2. It is advisable not to store static resources under /jsp/, that area should be reserved for JSP files. Consider using "static" or "resources" in the path instead.

    <mvc:resources mapping="/resources/**" location="/resources/" />

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Automatic Clicks in the Chrome Console

I've come across a website that requires me to click on multiple elements scattered throughout the page Here is the code for one of the elements: <span class="icon icon-arrow-2"></span> Is there a way to provide me with the console comm ...

Unusual CSS inconsistencies between running on VS Web Server and IIS

My current dilemma involves a discrepancy in the appearance of my site when viewed locally through the visual studio web server (http://localhost:3452/) compared to when accessed on IIS7 (http://server/myproject/). Initially, I suspected a CSS issue causi ...

Is the data that appears only after scrolling (paging) included in the dom when the page is initially loaded?

Can someone help me decipher this code snippet? WebElement elem = driver.findElement(By.id("123")); JavascriptExecutor js = (JavascriptExecutor) driver js.executeScript("arguments[0].scrollIntoView(true);",elem); I've come across ...

Tips for applying multiple style properties to an element using JavaScript

I have been experimenting with different versions of this code in an attempt to modify a specific element for a coding challenge, but none of them seem to successfully change multiple styling properties of the element upon clicking on a button. Would great ...

Unleashing the power of curl commands in Node.js

How can I achieve the functionality of sending XML data from a file and writing the response to a specific path in Node.js, similar to my curl command with XML data? cd C:/test/ && curl localhost:9001 --data @ C:/test/Basic/SPND.xml -o C:/test ...

Log4J: Alert - Logger error encountered due to lack of appenders available

Here is the content of my log4j.properties file, which is located in the src folder within Eclipse: #Application Logs log4j.rootlogger=INFO, logg log4j.appender.logg=org.apache.log4j.RollingFileAppender log4j.appender.logg.File=D:\\SandhyaFiles& ...

The application experiences a crash if the user tries to select more than 10 images

Currently, I am developing an Android app using Android Studio and Java. I have encountered a issue when trying to pick more than 10 images from the gallery and insert them into a Sqlitedatabase table. The problem arises when inserting the 10th image, as i ...

Challenges with implementing fluent wait on Internet Explorer in Webdriver

My current project involves automating a test using Java and webdriver. This specific test focuses on two dropdown menus with drilldown functionality. The first dropdown presents a list of options, and based on the user's selection in the first dropdo ...

What is the process for generating Java objects from XML tags that reference one another?

Within my XML file, I have tags that correspond to three distinct types of Java objects that can be created from the XML. These objects are structured as follows: A - static Map<String, A> - String name - String aInfo1 - String aInfo2 B - static Ma ...

Trouble in paradise: Redis keys command failing to display all keys

Currently, Redis is being utilized as the caching layer for my project. One of the notable aspects is the presence of numerous instances where @Cacheable annotation is used in various services and layers. Consider the following code snippet: @Cacheable(v ...

Having trouble accessing the database using JDBC in phpMyAdmin MySQL?

I have set up a web application on OpenShift and configured a MySQL database using phpMyAdmin. I believe the server name is reflected as shown on the phpMyAdmin page (127.01.201.302). Afterwards, I created a user named 'localhostOnly' with matchi ...

Store <td> in a variable

At the moment, I have a script that allows users to input an item name, along with its size, color, and other options. Each of these entries is saved as individual items with their custom specifications, such as a black t-shirt in large size. The script c ...

Learning Java for Automated Number Plate Recognition (ANPR) with a focus on various

While working with the JavaANPR library, I noticed that the alphabet included does not match the one used for license plates in my country. I have already made changes to the syntax.xml file to reflect our plates' format (three letters followed by th ...

Utilizing Adjacent Sibling Selectors within the context of a specific enclosing class

I have a question regarding the following HTML structure: <div class="a"> <div class="b"></div> <div class="c"></div> </div> My goal is to apply a specific CSS rule only within the a class: .b + .c { margin-le ...

Tips for aligning specific items to the right side of the navigation bar

I've been grappling with creating a navigation bar for a website, aiming to position 4 elements on the left side of the screen and 2 on the right. Despite knowing that there's likely a straightforward solution, I'm struggling to get the elem ...

Secret button concealed within a div, revealing only a helpful hint

On my website, there is a list of items where each item is represented by either a picture or a name enclosed in an <article> tag. Here is an example of how it looks: <article class="client-card"> <p><img width="211" height="106" ...

Tips for managing browser glitch with selenium?

There are instances when automating a test case, I encounter situations where the page becomes unresponsive due to different factors. What is the best way to mark a specific test case as failed and proceed to the next ones? I am currently using Java to w ...

Having trouble deserializing with mixin

Here is the json data I am receiving from an endpoint, which contains object types success, message, and loggedInMember. The feeClasses field in the JSON holds a lot of information, but I am only interested in success and message. { "header": ...

Activate div2 visibility upon hovering over div1 using jQuery

I have a shopping basket on my website, and I want to display a list of products when a user hovers over the basket icon. However, I am facing an issue where the list disappears as soon as the mouse leaves the basket logo. Is there a way to keep the list ...

What is the process of transforming a list of JSON objects into a list of POJO strings that includes specific fields from each object?

I am working with a JSON document that describes a list of objects, and the structure looks like this: [ { "txId": "ffff", "sender" : "0xwwwwwww", "recepient" : "0xeferfef" }, { "txId": "ffff", "sender" : "0xwwwwwww", ...