我在尝试将名为“new”的URL路径映射到执行某些逻辑的Controller方法时出现以下错误,然后显示视图“index.jsp”:
警告:在DispatcherServlet中找不到带有URI [/springguestbook/index.jsp]的HTTP请求的映射,其名称为“springguestbook”
这是我的控制器方法:
@RequestMapping(value = "/list", method = RequestMethod.GET)
public String showGuestbookEntries(HttpServletRequest request, Model model) {
HttpSession session = request.getSession(true);
this.guestbookService = GuestbookService.getInstance(session);
model.addAttribute("guestbookEntries",
guestbookService.getGuestbookEntries());
return "index";
}
这些是我的web.xml文件的内容:
<display-name>Spring Guestbook</display-name>
<servlet>
<servlet-name>springguestbook</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springguestbook</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
这是我的ViewResolver的配置:
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
在我看来,该方法不是实际读取index.jsp的内容并显示它们,而是重定向到index.jsp。 我该如何解决这个问题?
答案 0 :(得分:1)
两件事:
尝试将servlet映射更改为此(将/*
更改为/
):
<servlet-mapping>
<servlet-name>springguestbook</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
确保您使用的是可以找到您的JSP的视图解析器,例如InternalResourceViewResolver
:
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver b = new InternalResourceViewResolver();
b.setPrefix("/WEB-INF/jsp/");
b.setSuffix(".jsp");
return b;
}
或
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>