如何使用 Spring Controller 解析 Apache Tiles 定义
Posted
技术标签:
【中文标题】如何使用 Spring Controller 解析 Apache Tiles 定义【英文标题】:How to resolve Apache Tiles defintion with Spring Controller 【发布时间】:2013-11-25 14:26:55 【问题描述】:给出以下情况:
我在tiles.xml 中定义了我的JSP 视图 我所有的 JSP 文件都位于 WEB-INF/pages 下并有自己的子文件夹我必须如何定义 Spring 控制器通过 Apache Tiles 定义名称查找视图的 viewResolver?
示例:
<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;
登录控制器:
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 定义 (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>
【问题讨论】:
【参考方案1】:我可以看到您对ViewResolver
有两个定义:一个在setupViewResolver()
中,另一个在viewResolver()
中。您应该删除第一个并保留第二个。
【讨论】:
【参考方案2】:我认为这不是完整的答案。是的,您不能像这样定义两个,但是您可以设置解析器的顺序。
viewResolver.setOrder(1);
【讨论】:
以上是关于如何使用 Spring Controller 解析 Apache Tiles 定义的主要内容,如果未能解决你的问题,请参考以下文章
源码剖析Spring MVC如何将请求映射到Controller?
Spring MVC注解Controller源码流程解析--HandlerAdapter执行流程--上