amit_vickey
amit_vickey

Reputation: 45

JSP and CSS file not loading in browser Java WebApp

I have the following folder structure for my Maven WebApp Project

src
 +-----main
        +-----java
        |       +-----com
        |               +------controller
        |                          +-----HomeController.java
        |
        +-----resources
        +-----webapp
                 +-----resources
                 |          +-----css
                 |                 +-----home.css
                 +-----WEB-INF
                         +-----jsp
                         |      +------home.jsp
                         +-----eLibrary-servlet.xml
                         +-----web.xml

web.xml:

<servlet>
    <servlet-name>eLibrary</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>eLibrary</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<display-name>eLibrary</display-name>

eLibrary-servlet.xml:

<mvc:annotation-driven />

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEF-INF/jsp/" />
    <property name="suffix" value=".jsp" />
</bean>

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

HomeController.java:

@Controller
    public class HomeController {
        @RequestMapping("/")
        public String getHomePage() {
            return "home";
        }
    }

And in my home.jsp, I'm trying to access the CSS file using ${pageContext.request.contextPath}/resources/css/home.css

But neither my JSP file(i.e. home.jsp) nor the CSS file(i.e. home.css) is loading in browser and I'm getting HTTP Status 404 when accssing http://localhost:8080/eLibrary/ with a message

/eLibrary/WEF-INF/jsp/home.jsp

and description

The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

Upvotes: 0

Views: 662

Answers (3)

Avinash Kumar
Avinash Kumar

Reputation: 21

You have a typo error in your elibrary-servlet.xml

<mvc:annotation-driven />

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
</bean>

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

Upvotes: 0

Stack Programmer
Stack Programmer

Reputation: 679

There is a mistake in Configuring the Spring DispatcherServlet. You need to mention your eLibrary-servlet.xml in web.xml. Need to rearrange the web.xml as mentioned in the below link.

https://docs.spring.io/spring-flex/docs/1.0.x/reference/html/ch02s02.html

<servlet>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/eLibrary-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

Hope this is usefull.

Upvotes: 0

benjamin c
benjamin c

Reputation: 2338

You have a type, in here /WEF-INF/jsp/

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEF-INF/jsp/" />
    <property name="suffix" value=".jsp" />
</bean>

change it to /WEB-INF/jsp/

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
</bean>

Upvotes: 1

Related Questions