0

My JSP pages in my dynamic web application (in Eclipse) are not being styled by my CSS code. I have included a stylesheet in index.jsp as follows:

index.jsp

<html>
    <head>
        <title>To Do List - Home</title>
        <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/stylesheet.css">
    </head>
    <body>
        <a href="tasks">Tasks</a>
    </body>
</html>

And my project structure is as follows:

enter image description here

I thought that href="${pageContext.request.contextPath}/css/stylesheet.css would look for stylesheet.css in ToDoList/WebContent/css/. If I try navigating directly to the stylesheet in the browser via http://localhost:8080/ToDoList/css/stylesheet.css it returns a 404 error.

I am aware this question has been asked before but from looking at the other questions I still can't figure out what is wrong with my project structure.

Update:

So I added <mvc:resources mapping="/css/**" location="/css/" /> to my servlet config, but now when I navigate to any page other than index.jsp I get a 404 error.

todolist-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans     
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- Scan for JavaConfig, annotated with @Configuration -->
    <context:component-scan base-package="com.petehallw.todolist.main" />

    <context:annotation-config/>

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

    <!-- Configure Spring view resolver -->
    <bean id="viewResolver"
    class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
        value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>

The error I get is:

WARNING: No mapping found for HTTP request with URI [/ToDoList/tasks] in DispatcherServlet with name 'todolist'

This is upon clicking a link to "tasks" in index.jsp which was previously returning the tasks.jsp page.

13
  • It looks like you need a static handler in todolist-servlet. Check out that answer . It might help. Commented Jul 19, 2017 at 15:02
  • Can you just drag and drop it from the tree into your project, and have it auto fill it for you? Commented Jul 19, 2017 at 15:14
  • @spas2k If I drag it in the value is "WebContent/css/stylesheet.css" and it doesn't work either. Commented Jul 19, 2017 at 15:30
  • @GurkanYesilyurt Thanks for the info, I tried your suggestion but I am now getting 404 error from my other pages. I have updated the question with the details. Commented Jul 20, 2017 at 8:54
  • Use c:url every url in the app. <a href="<c:url value="/tasks"/>">Tasks</a>. If you have @requestmapping("/tasks") method, will be invoked. Or <a href="${pageContext.request.contextPath}/tasks">Tasks</a> like you use at css files. Commented Jul 20, 2017 at 9:04

1 Answer 1

1

For every URL in the app, you can make use of c:url tag in JSTL lib.

<a href="<c:url value="/tasks"/>">Tasks</a>. 

So, if you have controller method mapping @requestmapping("/tasks"), it will be invoked.

Or try as <a href="${pageContext.request.contextPath}/tasks">Tasks</a> like you use at css files.

Important: You should add <mvc:annotation-driven /> in xml configuration for the support of annotation-driven MVC controllers like @RequestMapping, @Controller.

Sign up to request clarification or add additional context in comments.

2 Comments

Just to note, I do need to use the <c:url/> tag when including static content in my JSP page but for the link to "/tasks" it seems I don't need it. I did move my index.jsp file into WEB-INF-->jsp with the rest of the JSP files and create a root controller with RequestMapping("/") though so maybe that is why I don't need to specify the context path.
@petehallw yeah, you're right. Relative URLs will work, too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.