开发者

Spring Vs Struts + Freemarker

For a Web application If I've choices开发者_开发问答 between Spring and Struts to use with Freemarker, which one go well, Or I would rather ask, which MVC framework integrates smoothly with Freemarker?


The Spring framework provides everything you need to use FreeMarker for your view layer.


Both have pretty good freemarker support. Its easy to turn on. Struts2 is a little more pojo based. Spring is a little closer to the servlet api. Spring's default macros in spring.ftl need a little work and you will likely need to roll your own. Some of the macros blow up if an object is not present rather than gracefully testing for it and moving on if it is not there.

I like Spring's application of validation via annotations better than Struts 2 default validation. However, persisting validation errors over redirects is easier in Struts2. For Spring you'll end up needing to roll your own solution where I feel the framework should hide more of that. Needing to use the error prone spring.bind macro with freemarker templates is more cumbersome than it needs to be.

Spring 3.1 is supposed to provide better support for this validation errors living over redirects.

Also note, with Spring I typically use more than one view resolver. e.g. I still leaving support for .jsp on.

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <property name="mediaTypes">
        <map>
            <entry key="html" value="text/html"/>
            <entry key="ftl" value="text/html"/>
            <entry key="xml" value="application/xml"/>
            <entry key="json" value="application/json"/>
        </map>
    </property>
    <property name="favorPathExtension" value="true"/>
    <property name="defaultViews">
        <list>
            <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"/>
        </list>
    </property>
    <property name="viewResolvers">
        <list>
            <bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
                <property name="cache" value="true"/>
                <property name="order" value="1"/>
                <property name="prefix" value="/"/>
                <property name="suffix" value=".ftl"/>
                <property name="contentType" value="text/html;charset=UTF-8"/>
                <property name="exposeSpringMacroHelpers" value="true"/>
                <property name="requestContextAttribute" value="rc"/>
                <property name="exposeSessionAttributes" value="true"/>
                <property name="exposeRequestAttributes" value="true"/>
            </bean>

     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                <property name="prefix" value="/WEB-INF/views/"/>
                <property name="suffix" value=".jsp"/>
     </bean>
        </list>
    </property>
</bean>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜