问题
I'm getting the following error when trying to map a URL path called "new" to a Controller method which does some logic, then displays the view "index.jsp":
WARNING: No mapping found for HTTP request with URI [/springguestbook/index.jsp] in DispatcherServlet with name 'springguestbook'
This is my Controller method:
@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";
}
These are the contents of my web.xml file:
<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>
And this is the configuration of my ViewResolver:
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
It seems to me that instead of actually reading the contents of index.jsp and displaying them, the method just redirects to index.jsp instead. How do I fix this?
回答1:
Two things:
Try changing your servlet mapping to this (change
/*
to/
):<servlet-mapping> <servlet-name>springguestbook</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
Make sure you're using a view resolver that can find your JSP, like
InternalResourceViewResolver
:@Bean public ViewResolver viewResolver() { InternalResourceViewResolver b = new InternalResourceViewResolver(); b.setPrefix("/WEB-INF/jsp/"); b.setSuffix(".jsp"); return b; }
or
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
来源:https://stackoverflow.com/questions/17031513/pretty-url-mapping-with-spring-3-0