如何创建 Spring 3 + Tiles 2 Web 应用程序使用 REST-ful URL?

发布于 2024-08-29 19:05:39 字数 1367 浏览 8 评论 0原文

我在使用 Spring 3.0 MVC 解析 URL 时遇到了很大的麻烦。我只是构建一个 HelloWorld 来尝试如何在 Spring 中构建 RESTful web 应用程序,理论上没什么复杂的。

我能够找到的所有示例都基于关注文件扩展名(“*.htm”或“*.do”)的配置,包括人工目录名称前缀(“/foo”)甚至前缀带有点的路径(丑陋),所有方法都使用一些人工正则表达式模式作为解析器的信号。对于 REST 方法,我希望避免所有这些麻烦,并仅使用应用程序的自然 URL 模式。我会假设(可能是错误的)在 web.xml 中我设置了“/*”的 url 模式并将所有内容传递给 DispatcherServlet 进行解析,然后仅依赖控制器中的 URL 模式。

我无法可靠地让我的解析器捕获 URL 模式,并且在我的所有试验中,这会导致资源未找到错误、堆栈溢出(循环)或某种不透明的 Spring 3 ServletException 堆栈跟踪 - 一个我对 Spring 一直感到不满的一个原因是,错误消息通常没有多大帮助。

我想使用 Tiles 2 解析器。我已将 *.jsp 文件放在 WEB-INF/views/ 中,并在应用程序根目录下有一个单行 index.jsp 文件,该文件重定向到由我的layout.xml(Tiles 2 配置器)设置的索引文件。

我做了所有正常的 Spring 3 高级配置:

<mvc:annotation-driven />

<mvc:view-controller path="/" view-name="index"/>

<context:component-scan base-package="com.acme.web.controller" />

...接下来是 UrlBasedViewResolver、InternalResourceViewResolver、UrlFilenameViewController 等的各种组合和配置,以及我的 Tiles 2 配置文件中的各种变体。然后在我的控制器中我尝试获取我的 URL 模式。问题是,我什至无法可靠地让解析器捕获发送到我的控制器的模式。

现在已经持续了好几天,而我认为实施起来非常简单的事情却没有取得真正的进展。我可能试图一次做太多事情,尽管我认为这应该是一个简单的(几乎是默认的)配置。我只是想创建一个简单的 HelloWorld 类型的应用程序,我不认为这是火箭科学。

不是我发布我自己的配置(遍布整个地图),有谁知道一个在线示例:显示一个使用 REST-ful URL 的简单 Spring 3 MVC + Tiles 2 Web 应用程序(即,避免强制 URL 模式)例如文件扩展名、添加的目录名或点)并仅依赖 Spring 3 代码/注释(即 Spring MVC 本身之外的任何内容)来完成此操作? 有没有简单的方法可以做到这一点?

非常感谢您的帮助。

I'm having a heck of a time resolving URLs with Spring 3.0 MVC. I'm just building a HelloWorld to try out how to build a RESTful webapp in Spring, nothing theoretically complicated.

All of the examples I've been able to find are based on configurations that pay attention to file extensions ("*.htm" or "*.do"), include an artificial directory name prefix ("/foo") or even prefix paths with a dot (ugly), all approaches that use some artificial regex pattern as a signal to the resolver. For a REST approach I want to avoid all that muck and use only the natural URL patterns of my application. I would assume (perhaps incorrectly) that in web.xml I'd set a url-pattern of "/*" and pass everything to the DispatcherServlet for resolution, then just rely on URL patterns in my controller.

I can't reliably get my resolver(s) to catch the URL patterns, and in all my trials this results in a resource not found error, a stack overflow (loop), or some kind of opaque Spring 3 ServletException stack trace — one of my ongoing frustrations with Spring generally is that the error messages are not often very helpful.

I want to work with a Tiles 2 resolver. I've located my *.jsp files in WEB-INF/views/ and have a single line index.jsp file at the application root redirecting to the index file set by my layout.xml (the Tiles 2 Configurer).

I do all the normal Spring 3 high-level configuration:

<mvc:annotation-driven />

<mvc:view-controller path="/" view-name="index"/>

<context:component-scan base-package="com.acme.web.controller" />

...followed by all sorts of combinations and configurations of UrlBasedViewResolver, InternalResourceViewResolver, UrlFilenameViewController, etc. with all manner of variantions in my Tiles 2 configuration file. Then in my controller I've trying to pick up my URL patterns. Problem is, I can't reliably even get the resolver(s) to catch the patterns to send to my controller.

This has now stretched to multiple days with no real progress on something I thought would be very simple to implement. I'm perhaps trying to do too much at once, though I would think this should be a simple (almost a default) configuration. I'm just trying to create a simple HelloWorld-type application, I wouldn't expect this is rocket science.

Rather than me post my own configurations (which have ranged all over the map), does anyone know of an online example that: shows a simple Spring 3 MVC + Tiles 2 web application that uses REST-ful URLs (i.e., avoiding forced URL patterns such as file extensions, added directory names or dots) and relies solely on Spring 3 code/annotations (i.e., nothing outside of Spring MVC itself) to accomplish this? Is there an easy way to do this?

Thanks very much for any help.

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

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

发布评论

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

评论(3

雪若未夕 2024-09-05 19:05:39
<bean id="tilesviewResolver" class="org.springframework.web.servlet.view.tiles2.TilesViewResolver" 
            p:order="0"/>

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

请参阅 TilesViewResolver

<bean id="tilesviewResolver" class="org.springframework.web.servlet.view.tiles2.TilesViewResolver" 
            p:order="0"/>

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

See TilesViewResolver

千紇 2024-09-05 19:05:39

您可以尝试 MVC-AJAX Spring 3 示例。

https://src.springframework.org/svn/spring-samples/

You may try the MVC-AJAX Spring 3 sample.

https://src.springframework.org/svn/spring-samples/

幸福丶如此 2024-09-05 19:05:39

春季示例应用程序:petcare 具有以轻松方式定义的链接。
您可以从以下位置下载: https://src.springframework.org/svn/spring-samples/
步骤是:
1.定义tiles配置bean(配置xml文件)

<!-- Initializes the Apache Tiles CompositeView system -->
    <beans:bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
        <beans:property name="definitions">
            <beans:value>
                /WEB-INF/**/tiles.xml
            </beans:value>
        </beans:property>
        <beans:property name="checkRefresh" value="true" />
    </beans:bean>
  1. 定义嵌套在UrlBasedViewResolver中的tiles视图类解析器

    value="org.springframework.web.servlet.view.tiles2.TilesView" />

  2. 为每个布局/页面等定义tiles.xml定义文件。

The spring sample application : petcare has the linkages defined in a restful way.
You can download these from : https://src.springframework.org/svn/spring-samples/
The steps are:
1. Define the tiles configuration bean (configuration xml file)

<!-- Initializes the Apache Tiles CompositeView system -->
    <beans:bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
        <beans:property name="definitions">
            <beans:value>
                /WEB-INF/**/tiles.xml
            </beans:value>
        </beans:property>
        <beans:property name="checkRefresh" value="true" />
    </beans:bean>
  1. Define the tiles view class resolver nested in UrlBasedViewResolver

    value="org.springframework.web.servlet.view.tiles2.TilesView" />

  2. Define the tiles.xml definition files for each layout/page etc.

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