gpt4 book ai didi

java - Spring MVC ModelAndView 不工作 404

转载 作者:行者123 更新时间:2023-11-30 03:14:24 25 4
gpt4 key购买 nike

我在尝试获取 URL 时收到 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,也用于

返回index.jsp的根url“http://localhost/demo_spring/letsgo

目录结构:
WEB-INF/ View
-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>

最佳答案

InternalViewResolver配置中,前缀应该是

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

不是

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

注意 WEB-INF 之前的/。

您还应该从方法findLoc中删除@ResponseBody,如JavaGhost所述。 @ResponseBody 表示方法返回值应绑定(bind)到 Web 响应正文

关于java - Spring MVC ModelAndView 不工作 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32977107/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com