简体   繁体   中英

Spring MVC ModelAndView not working 404

I am getting 404 while trying to get URL" http://localhost/demo_spring/letsgo/find " where it resolutes to search.jsp page (as in snippet below)

Error Message: HTTP Status 404 - /demo_spring/letsgo/WEB-INF/views/search.jsp

I have checked and its working for " http://localhost/demo_spring/letsgo/go " which produces JSON and also for

root url " http://localhost/demo_spring/letsgo " which returns index.jsp

Directory structure:
WEB-INF/VIEWS
-index.jsp
-search.jsp

@Controller
@RequestMapping("/letsgo")
public class IntroController
{   
    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView welcome() {
        ModelAndView v1 = new ModelAndView("index");
        v1.addObject("attributeName", "attributeValue");
        return v1;
    }


    @RequestMapping(value="find",method = RequestMethod.GET)
    @ResponseBody
    public ModelAndView findloc() {
        ModelAndView op = new ModelAndView("search");
        op.addObject("attributeName", "attributeValue");
        return op;
    }


    @RequestMapping(value="go",method = RequestMethod.GET, produces="application/json")
    @ResponseBody
    public  String welcome3() {
        try
        {
            return new ObjectMapper().writeValueAsString("Check for json");
        }
        catch (JsonProcessingException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }
    }

Dispatcher mapping

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

Sprin-mvc.xml configuration file

<?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:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
                           http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">

    <mvc:annotation-driven/>
    <mvc:default-servlet-handler/>

      <!-- View Handler -->
                 <bean id="multipartResolver"
   class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="maxUploadSize" value="5000000"/>
</bean>

    <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
        <property name="favorPathExtension" value="true"/>
        <property name="mediaTypes">
            <map>
                <entry key="xml" value="text/xml"/>
                <entry key="json" value="application/json"/>
                <entry key="html" value="text/html"/>
                <entry key="less" value="text/html"/>
            </map>
        </property>
        <property name="viewResolvers">
            <list>


               <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">

    <property name="prefix" value="WEB-INF/views/"/>
    <property name="suffix" value=".jsp"/>
</bean>
            </list>
        </property>
    </bean>
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
                <bean class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter"/>
            </list>
        </property>
    </bean>

</beans>

In InternalViewResolver configuration, the prefix should be

   <property name="prefix" value="/WEB-INF/views/"/>

not

  <property name="prefix" value="WEB-INF/views/"/>

Notice the / before WEB-INF.

You should also remove @ResponseBody from the method findLoc as mentioned by JavaGhost . @ResponseBody indicates a method return value should be bound to the web response body

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