使用<mvc:view-controller>进行切片</mvc:view-controller>

时间:2013-04-01 08:20:18

标签: spring model-view-controller tiles

我们可以使用mvc:view-controller标记来显示切片视图。

例如:如果我们有表单定义的tile定义,

name="userLogon.page" 
template="webapp/app/layout/baseLayoutExterior.jsp" 
extends="basePageDefinition"

我们可以在mvc:view-controller path="/logon" view-name="userLogon.page"文件中写servlet.xml

我在此处获得 HTTP 404资源未找到错误,而正常的hello.jsp页面显示没有问题。

请帮助我被困住。

1 个答案:

答案 0 :(得分:1)

您需要添加TilesConfigurerTilesView

<bean class="org.springframework.web.servlet.view.UrlBasedViewResolver"
    id="tilesViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.tiles2.TilesView" />
</bean>
<bean class="org.springframework.web.servlet.view.tiles2.TilesConfigurer"
    id="tilesConfigurer">
    <property name="definitions">
        <list>
            <value>/WEB-INF/layouts/layouts.xml</value>
            <!-- Scan views directory for Tiles configurations -->
            <value>/WEB-INF/views/**/views.xml</value>
        </list>
    </property>
</bean>

位于webapp/WEB-INF/layouts

中的Layout.xml示例
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE tiles-definitions PUBLIC
       "-//Apache Software Foundation//DTD Tiles Configuration 2.1//EN"
       "http://tiles.apache.org/dtds/tiles-config_2_1.dtd">

<tiles-definitions>

  <definition name="default" template="/WEB-INF/layouts/default.jspx">
    <put-attribute name="header" value="/WEB-INF/views/header.jspx" />
    <put-attribute name="menu" value="/WEB-INF/views/menu.jspx" />
    <put-attribute name="footer" value="/WEB-INF/views/footer.jspx" />
  </definition>

</tiles-definitions>

位于webapp/WEB-INF/views/bookmarks

中的一个view.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE tiles-definitions PUBLIC "-//Apache Software Foundation//DTD Tiles Configuration 2.1//EN" "http://tiles.apache.org/dtds/tiles-config_2_1.dtd">
<tiles-definitions>
    <definition extends="default" name="bookmarks/show">
        <put-attribute name="body" value="/WEB-INF/views/bookmarks/show.jspx" />
    </definition>       
    <definition extends="default" name="bookmarks/list">...</definition>
    <definition extends="default" name="bookmarks/create">...</definition>      
    <definition extends="default" name="bookmarks/update">...</definition>
</tiles-definitions>

一种控制器方法:

@RequestMapping("/bookmarks")
@Controller
public class BookmarkController {
    ...
    /** Return the show page. */
    @RequestMapping(method = RequestMethod.GET, value = "/{id}")
    public ModelAndView show(@PathVariable("id") long bookmarkId) {
        return new ModelAndView("bookmarks/show", "bookmark", bookmarkDao.getBzId(bookmarkId));
    }
}