mvc:view-controller 导致 Spring Tiles2 中的 PageNotFound

发布于 2024-12-03 14:00:25 字数 1690 浏览 1 评论 0原文

我有一个基于 Spring 3.0.6 的 web 应用程序,它在 Tomcat 7.0 上运行良好。

web.xml 定义调度程序如下:

<servlet>
  <servlet-name>dispatcher</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>dispatcher</servlet-name>
  <url-pattern>/</url-pattern>
</servlet-mapping>

调度程序以通常的方式定义视图解析器:

<bean id="tilesViewResolver"
    class="org.springframework.web.servlet.view.UrlBasedViewResolver">
  <property name="viewClass"
      value="org.springframework.web.servlet.view.tiles2.TilesView" />
</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>

我有一个用 @RequestMapping("/home") 注释的控制器,并且在 tiles-def.xml 中定义的 "home" 视图。当我将浏览器指向 /myapp/home.html 时,Tiles 页面将打开。

如果我添加 到我的调度程序 xml 文件,将浏览器指向 /myapp/home.html 结果是 404。日志显示:

21:34:22,128  WARN PageNotFound:947 – No mapping found for HTTP request with URI [/myapp/home.html] in DispatcherServlet with name 'dispatcher'

What am我做错了吗?

多谢

I have a webapp based on Spring 3.0.6 which works fine on Tomcat 7.0.

The web.xml defines the dispatcher as following:

<servlet>
  <servlet-name>dispatcher</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>dispatcher</servlet-name>
  <url-pattern>/</url-pattern>
</servlet-mapping>

The dispatcher defines the view resolver in the usual way:

<bean id="tilesViewResolver"
    class="org.springframework.web.servlet.view.UrlBasedViewResolver">
  <property name="viewClass"
      value="org.springframework.web.servlet.view.tiles2.TilesView" />
</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>

I have a controller annotated with @RequestMapping("/home") and a "home" view defined in tiles-def.xml. When I point my browser to the /myapp/home.html, the Tiles page is opened.

If I add <mvc:resources mapping="/resources/**" location="/resources/" /> or <mvc:view-controller path="/" view-name="home.html"/> to my dispatcher xml file, pointing the browser to /myapp/home.html results in a 404. The log says:

21:34:22,128  WARN PageNotFound:947 – No mapping found for HTTP request with URI [/myapp/home.html] in DispatcherServlet with name 'dispatcher'

What am I doing wrong?

Thanks a lot

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

メ斷腸人バ 2024-12-10 14:00:25

我的应用程序中的问题是由于自动视图名称解析造成的。我的 @Controller 中带注释的方法返回了 void,并且框架尝试使用请求路径猜测图块视图名称。

我将带注释的方法修改如下,返回一个 String

@RequestMapping(value="/page", method = RequestMethod.GET)
public String showForm(HttpServletRequest request, Model model) {
    // TO BUSINESS LOGIC

    // return tiles view name as configured in 'tiles-def.xml'
    return "my_tiles_view_name";
}

通过此更改,一切正常。

The problem in my application was due to the automatic view name resolution. My annotated method in my @Controller returned void, and the framework tried to guess the tiles view name using the request path.

I modified my annotated method as following, returning a String:

@RequestMapping(value="/page", method = RequestMethod.GET)
public String showForm(HttpServletRequest request, Model model) {
    // TO BUSINESS LOGIC

    // return tiles view name as configured in 'tiles-def.xml'
    return "my_tiles_view_name";
}

With this change, everything works fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文