Pretty URL Mapping with Spring 3.0

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-11 19:46:03

问题


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:

  1. Try changing your servlet mapping to this (change /* to /):

    <servlet-mapping>
        <servlet-name>springguestbook</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    
  2. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!