给出以下情况:
我如何定义我的viewResolver,Spring控制器通过Apache Tiles定义名称查找视图?
示例:
<definition name="loginForm" extends="baseLayout">
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>
答案 0 :(得分:4)
我认为这不是完整的答案。是的,你不能有两个这样的定义,但是你可以设置解析器的顺序。
viewResolver.setOrder(1);
答案 1 :(得分:3)
我可以看到您有ViewResolver
的两个定义:setupViewResolver()
中的一个和viewResolver()
中的另一个定义。你应该删除第一个并离开第二个。