Dowemo
0 0 0 0


Question:

Basic configuration files looks unintuitive.

If I create simple hello world example, and then rename home.jsp to home.html and edit servlet-context.xml file from

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

to

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

I start to get an error

WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/myapp/WEB-INF/views/home.html] in DispatcherServlet with name 'appServlet'

Why? What suffix property means?

UPDATE

My controller is follows. As you see it does not contain file extension

@Controller
public class HomeController {
    private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
    /**
     * Simply selects the home view to render by returning its name.
     */
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String home(Locale locale, Model model) {
        logger.info("Welcome home! The client locale is {}.", locale);
        Date date = new Date();
        DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
        String formattedDate = dateFormat.format(date);
        model.addAttribute("serverTime", formattedDate );
        return "home";
    }
}

Best Answer:


well, it seems you didn' t set the view' s order.

for example, if your project has view like jsp, json, velocity, freemarker, etc. you can use all of them(maybe you need new version of spring, 3.1+), but only one view will be select to render to client, that depends on your view' s order, the lower the order, the prefer the view.

for example, you set jsp view' s order is 1, and freemarker view' s order is 2, both of their view name is "home", the spring will choose view.jsp(if you set suffix to .jsp). Well, if your view name is "index", no index.jsp but index.ftl(suppose you set freemarker' s view to .ftl), spring will choose the later.

the sample code using spring' s java config, you can easily convert to xml-style.

@Bean
public InternalResourceViewResolver jspViewResolver() {
    InternalResourceViewResolver jsp = new InternalResourceViewResolver();
    jsp.setOrder(4);
    jsp.setCache(true);
    jsp.setViewClass(org.springframework.web.servlet.view.JstlView.class);
    jsp.setPrefix("/WEB-INF/jsp/");
    jsp.setSuffix(".jsp");
    return jsp;
}
@Bean
public FreeMarkerViewResolver freeMarkerViewResolver() {
    FreeMarkerViewResolver viewResolver = new FreeMarkerViewResolver();
    viewResolver.setCache(true);
    viewResolver.setPrefix("");
    viewResolver.setSuffix(".ftl");
    viewResolver.setContentType(ViewConstants.MEDIA_TYPE_HTML);
    viewResolver.setRequestContextAttribute("request");
    viewResolver.setExposeSpringMacroHelpers(true);
    viewResolver.setExposeRequestAttributes(true);
    viewResolver.setExposeSessionAttributes(true);
    viewResolver.setOrder(2);
    return viewResolver;
}

please see the setOrder() method!

the json, jsonp and other type of views may use ontentNegotiation, and you can find it on spring' s docs.

finally, the html view, I mean, totally static files, which is not support by spring default. I suppose the static file desn' t need render by java. you can use the static mapping using the code below:

<mvc:resources mapping="/static/**" location="/static/" />

or use java config:

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    int cachePeriod = 3600 * 24 * 15;
    registry.addResourceHandler("/static/**").addResourceLocations("/static/").setCachePeriod(cachePeriod);
    registry.addResourceHandler("/favicon.ico").addResourceLocations("/").setCachePeriod(cachePeriod);
    registry.addResourceHandler("/robots.txt").addResourceLocations("/").setCachePeriod(cachePeriod);
}

and in your @RequestMapping method, you should redirect it!

well, if you don' t want redirection, just set the html view to an dynamic view (freemark, velecity, etc), which will be ok!

hope it useful!




Copyright © 2011 Dowemo All rights reserved.    Creative Commons   AboutUs