Spring MVC ModelAndView无法正常工作404

时间:2015-10-06 18:31:20

标签: java spring jsp spring-mvc java-ee

我在尝试获取网址时获得了404" http://localhost/demo_spring/letsgo/find" 它坚决到search.jsp页面(如下面的代码片段)

错误消息: HTTP状态404 - /demo_spring/letsgo/WEB-INF/views/search.jsp

我已经检查过它的工作了 " http://localhost/demo_spring/letsgo/go"它产生JSON,也用于

root url" http://localhost/demo_spring/letsgo"返回index.jsp

目录结构:
WEB-INF /视图
-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;
        }
    }

调度程序映射

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

Sprin-mvc.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: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>

1 个答案:

答案 0 :(得分:2)

InternalViewResolver配置中,前缀应为

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

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

注意WEB-INF之前的/。

您还应该按照JavaGhost提及的方法@ResponseBody删除findLoc@ResponseBody表示应将方法返回值绑定到Web响应主体