【问题标题】:Links in Spring MVC with TilesSpring MVC 中带有 Tiles 的链接
【发布时间】:2014-07-17 15:38:56
【问题描述】:

我仍在尝试熟悉 Tiles,我之前在 Struts 1.x 中使用过它,没有任何问题,但 Spring MVC 并非如此。使用 Struts,我知道您必须创建操作或使用其转发标签才能通过链接访问在图块中定义的页面,我假设使用 Spring 您同样需要使用控制器。但是,它似乎没有找到我的图块定义,因为每当控制器执行时,我都会收到此错误:

2014 年 7 月 17 日上午 11:21:07 org.springframework.web.servlet.DispatcherServlet noHandlerFound 警告:在名称为“spring-mvc”的 >DispatcherServlet 中找不到具有 URI [/BudgeAllocatorWeb/home] 的 HTTP 请求的映射

我的控制器非常简单,因为我只是想学习并弄清楚它是如何工作的:

@Controller
@RequestMapping("/login")
public class LoginController {
    @RequestMapping(method=RequestMethod.GET)
    public String login(ModelMap map) {
        return "home";
    }
}

我的 tiles-def.xml 看起来像:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE tiles-definitions PUBLIC
       "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
       "http://tiles.apache.org/dtds/tiles-config_2_0.dtd">
<tiles-definitions>
    <definition name="base" template="/pages/base_layout/layout.jsp">
        <put-attribute name="title" value="" />
        <put-attribute name="header" value="/pages/base_layout/header.jsp" />
        <put-attribute name="menu" value="/pages/base_layout/menu.jsp" />
        <put-attribute name="body" value="" />
        <put-attribute name="footer" value="/pages/base_layout/footer.jsp" />
    </definition>

    <definition name="home" extends="base">
        <put-attribute name="title" value="Budgeter" />
        <put-attribute name="body" value="/pages/home.jsp" />
    </definition>

</tiles-definitions>

还有我的 spring-mvc-servlet XML 文件:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

       <context:component-scan base-package="controllers" />

       <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <!-- <property value="/" name="prefix" />
            <property value=".jsp" name="suffix" /> -->
       </bean>  

    <!-- Spring Tiles -->

    <bean id="tilesviewResolver" class="org.springframework.web.servlet.view.tiles2.TilesViewResolver">
        <property name="viewClass">
            <value>
                org.springframework.web.servlet.view.tiles2.TilesView
            </value>
        </property>
    </bean> 

    <bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
        <property name="definitions">
            <list>
                <value>/WEB-INF/tiles-def.xml</value>
            </list>
        </property>
    </bean>    
</beans>

所以我的控制器返回“home”,打算点击我的 tiles-def 中定义的“home”。它适用于常规页面,但不适用于平铺定义。我在这里有什么遗漏吗?

【问题讨论】:

    标签: spring-mvc tiles2


    【解决方案1】:

    删除InternalViewResolver 或使用resolver.setOrder(x) 为解析器设置订单。尝试删除此代码

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <!-- <property value="/" name="prefix" />
            <property value=".jsp" name="suffix" /> -->
    </bean>
    

    这样只会使用图块视图解析器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-27
      相关资源
      最近更新 更多