1
votes

I am using Eclipse to create a Spring MVC web application development project. My directory structure looks like this -

src

-main

--webapp

---resources

---WEB-INF

-----classes

-----spring

------appServlet

-------servlet-context.xml

------root-context.xml

-----views

------css

-------style.css

------home.jsp

-----web.xml

-test

Contents are as follows -

1. home.jsp

2. Home Controller

*package edu.northeastern.library;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class HomeController {
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String home() {
        return "home";
    }
}*

3. Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Processes application requests -->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.css</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.js</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.gif</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.jpg</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.png</url-pattern>
    </servlet-mapping>

</web-app>

- servlet-context.xml

*<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
 xmlns:mvc="http://www.springframework.org/schema/mvc"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
 http://www.springframework.org/schema/mvc
 http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <mvc:resources mapping="/resources/**" location="/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <context:component-scan base-package="edu.northeastern.library" />



</beans>*

Please help as to why the css file does not get connected to the jsp file.

2
It's easier if you tell us what the full URL of both the JSP page and the CSS file is (as you see in browser's address bar when you successfully open it). This way we can tell you if the relative URL which you used in <link> is right or not and explain the right URL. By the way, I'd pay a bit more attention and love to the question formatting. Consult the formatting help behind the orange question mark on the right top of the editor. Regularly check/verify the formatting result in the message preview below the editor. - BalusC
@BalusC I got the answer. I moved the CSS folder under the resources folder and in the jsp file - <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/resources/css/style.css" media="screen"> I had also tried editing the question to as best as I could. - ultimate.divinity
Why you just use "/" as RequestMapping value, I think that's the problem, when you request the CSS, can you debug whether it gets here or not? - OQJF
@OQJF: as far as I can tell about Spring MVC, the pass-through resources are configured as <mvc:resources> in the XML config. Notwithstanding that I agree that the choice for / as servlet mapping is a poor choice. - BalusC
@Vivek Nair , OK, I see it. But where did you config the <mvc:annotation-driven /> and can you format you web.xml code? - OQJF

2 Answers

0
votes

I moved the CSS folder under the resources folder and in the jsp file added the following code to link to the css -

<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/resources/css/style.css" media="screen">
0
votes

Move all yours assets such as css,js,imgs,fonts inside the resources folder

and then on your links add this code ${pageContext.request.contextPath}/resources/

the link of your .jsp files

<link href="css/style.css">

replace it with this:

<link href="${pageContext.request.contextPath}/resources/css/style.css">

and that should work.