2
votes

Following situation is given:

  • I define my JSP-views in tiles.xml
  • All my JSP files are located under WEB-INF/pages and have their own subfolder

How must I define my viewResolver that the Spring controller finds the view by Apache Tiles definition name?

Example:

  • <definition name="loginForm" extends="baseLayout">
    --> will be found by new ModelAndView("loginForm");

ViewResolver

@Configuration
@EnableWebMvc
@ComponentScan("com.itensis.holidayplanner")
public class WebAppConfig extends WebMvcConfigurerAdapter {

    @Bean
    public UrlBasedViewResolver setupViewResolver() {
        UrlBasedViewResolver resolver = new UrlBasedViewResolver();
        resolver.setPrefix("/WEB-INF/pages/");
        resolver.setSuffix(".jsp");
        resolver.setViewClass(JstlView.class);
        return resolver;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/webjars/**").addResourceLocations("/webjars/");
        registry.addResourceHandler("/css/**").addResourceLocations("/css/");
        registry.addResourceHandler("/img/**").addResourceLocations("/img/");
    }

    @Bean
    public UrlBasedViewResolver viewResolver() {
        UrlBasedViewResolver viewResolver = new UrlBasedViewResolver();
        viewResolver.setViewClass(TilesView.class);
        return viewResolver;
    }

    @Bean
    public TilesConfigurer tilesConfigurer() {
        TilesConfigurer tilesConfigurer = new TilesConfigurer();
        tilesConfigurer.setDefinitions(new String[] { "/WEB-INF/tiles.xml" });
        tilesConfigurer.setCheckRefresh(true);
        return tilesConfigurer;
    }
}

LoginController:

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class LoginController {

    @RequestMapping(value={"/login"}, method=RequestMethod.GET)
    public ModelAndView showLoginPage() {
        return new ModelAndView("loginForm");
    }

    @RequestMapping(value="/success-login", method=RequestMethod.GET)  
    public ModelAndView successLogin() {  
        return new ModelAndView("dashboardForm");  
    } 

    @RequestMapping(value="/error-login", method=RequestMethod.GET)  
    public ModelAndView invalidLogin() {  
        ModelAndView modelAndView = new ModelAndView("loginForm");  
        modelAndView.addObject("error", true);  
        return modelAndView;  
    }  

    @RequestMapping(value="/logout", method=RequestMethod.POST)  
    public ModelAndView logout() {  
        return new ModelAndView("loginForm");
    }  
}

Apache Tiles defintion (tiles.xml):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE tiles-definitions PUBLIC "-//Apache Software Foundation//DTD Tiles Configuration 3.0//EN" "http://tiles.apache.org/dtds/tiles-config_3_0.dtd">

<tiles-definitions>
    <definition name="baseLayout" template="/WEB-INF/pages/template/layout.jsp">
        <put-attribute name="title" value="Holiday Planner" />
        <put-attribute name="header" value="/WEB-INF/pages/template/header.jsp" />
        <put-attribute name="navigation" value="/WEB-INF/pages/template/navigation.jsp" />
        <put-attribute name="content" value="" />
        <put-attribute name="footer" value="/WEB-INF/pages/template/footer.jsp" />
        <put-attribute name="include" value="/WEB-INF/pages/template/include.jsp" />
    </definition>

    <definition name="loginForm" extends="baseLayout">
        <put-attribute name="navigation" value="" />
        <put-attribute name="content" value="/WEB-INF/pages/login/login.jsp" />
        <put-attribute name="footer" value="" />
    </definition>

    <definition name="dashboardForm" extends="baseLayout">
        <put-attribute name="content" value="/WEB-INF/pages/dashboard/dashboard.jsp" />
    </definition>

</tiles-definitions>
2

2 Answers

3
votes

As I can see you have two definitions of ViewResolver: one in setupViewResolver() and another in viewResolver(). You should remove the first and leave the second.

3
votes

I think that's not the complete answer. Yes you can't have two defined like that, but then you can set the order of the resolver.

viewResolver.setOrder(1);