开发者

How do I create a Spring 3 + Tiles 2 webapp using REST-ful URLs?

开发者 https://www.devze.com 2022-12-27 19:26 出处:网络
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.

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.


<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


You may try the MVC-AJAX Spring 3 sample.

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号