简体   繁体   中英

Getting Error(HTTP Status 404 -) in Spring Mvc

I am new in SpringMVC and trying to execute simple hello world program. However, when run this in my browser ( http://localhost:8080/FirstspringMVCwithannotation/welcome ) I got HTTP Status- 404 Error. Here, is the code:

HelloController.java

package com.example;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.portlet.ModelAndView;


@Controller
public class HelloController {
    @RequestMapping("/welcome")
    public ModelAndView helloWorld(){
        ModelAndView model=new ModelAndView("HelloPage");
        model.addObject("msg","hello world");
        return model;
    }


}

web.xml

 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>FirstspringMVCwithannotation</display-name>

  <servlet>
    <servlet-name>spring-dispatcher</servlet-name>
        <servlet-class>
                  org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>spring-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
  </servlet-mapping>

</web-app>

spring-dispatcher-servlet.xml

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

  <context:component-scan base-package="com.example" />
  <mvc:annotation-driven/> 

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

HelloPage.jsp

<html>
    <body>
        <h1>
            First Spring MVC Application Demo 
        </h1>   
        <h2>
           ${msg}
        </h2>
    </body>
</html>

Here is my project structure and I added all spring jar files under lib folder

在此处输入图片说明

I tried to see other solution but that does not solved my problem..can anyone help me please, why i get HTTP Status- 404 Error? Thanks in advance

You received HTTP 404 because your dispatcher controller can't find you view.

Your view is under lib folder, which is under WEB-INF folder.

All you need to do is the reconfigure you view resolver, instead of what you wrote:

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

write it like this: configure your view resolver to extract views from lib folder

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

One more thing, I would recommend that you place your jsp files under views folder under WEB-INF and not under lib folder.

lib under WEB-INF is a folder dedicated to 3rd party libraries and jars.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM