views:

42

answers:

1

When I'm looking at Spring FrameWork 3.0 I see the following code example:

@RequestMapping("/index.dlp")
public ModelAndView index(){
    logger.info("Return View");
    return new ModelAndView("index");
}

This option doesn't work for me. Only when I change the code the following way:

@RequestMapping("/index.dlp")
    public ModelAndView index(){
        logger.info("Return View");
        return new ModelAndView("index.jsp");
    }

It works fine. Can anybody tell me why?

+7  A: 

View names are resolved into the actual views by ViewResolvers.

To refer JSP pages by short names, you need to supply InternalResourceViewResolver with prefix and suffix. The following configuration maps index to /WEB-INF/jsp/index.jsp:

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/"/>
    <property name="suffix" value=".jsp"/>
</bean>

See also:

axtavt
This is what I like: both question and answer are clear, understandable and correct. Hence +1 to both
seanizer
Thank you. Perfect and what If I would like to create two different beans one for /WEB-INF/jsp/ and second for /WEB-INF/forms/ or /WEB-INF/forms/jsp/? Thank you. Danny.
danny.lesnik
@danny: Then you can use `/WEB-INF/` as a prefix and `jsp/index` and `forms/index` as view names.
axtavt
Unfortunately, I did not get you. what "view name" actually refers to? Could you give small example? thank you.
danny.lesnik
OfCouse :) return "/forms/loginform"; sorry for stupid question. works perfectly.
danny.lesnik