Pavel
Pavel

Reputation: 2101

Why Spring MVC get 404?

I have a simple Spring MVC app. I get HTTP status 404 the report. Help me fix this issue. Thank You.

HTTP Status 404 – Not Found

Type Status Report

Message /WEB-INF/users_view.jsp

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

Apache Tomcat/9.0.0.M26

My controller:

@Controller
public class UserController {

    @RequestMapping(value = "/users", method = RequestMethod.GET)
    public String showUsers(ModelMap model) {
        return "users_view";
    }
}

Project structure:

enter image description here

My web.xml:

<display-name>User</display-name>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.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>

<!-- Process application servlet -->
<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:applicationContext.xml
        </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>

And applicationContext.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"
       xsi:schemaLocation= "http://www.springframework.org/schema/beans
                            http://www.springframework.org/schema/beans/spring-beans.xsd
                            http://www.springframework.org/schema/context
                            http://www.springframework.org/schema/context/spring-context.xsd">

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

    <context:component-scan base-package="ru.pravvich" />

</beans>

Upvotes: 3

Views: 1687

Answers (3)

shanedust
shanedust

Reputation: 54

Problem with InternalViewResolver

Solution 1: Change Prefix /WEB-INF/ to /WEB-INF/view

Solution 2: While returning String from controller change “users_view” to “/view/users_view”

If you are starting new project using spring mvc try java based configuration refer Spring mvc hello world example

Upvotes: 1

Pradeep Pati
Pradeep Pati

Reputation: 5919

Since your jsp is under WEB-INF/view and InternalResourceViewResolver prefix points to /WEB-INF/, you have a 404 error. To fix, change the prefix to /WEB-INF/view/.

Upvotes: 3

bananas
bananas

Reputation: 1196

in dispatcher servlet you have provided path /WEB-INF/ but in application it is WEB-INF/view/

So changing it inside dispatcher servlet to /WEB-INF/view/ will solve the problem

Upvotes: 3

Related Questions