对于一个Web应用程序,如果我必须在Spring和Struts中选择与Freemarker一起使用,那么哪个更好,或者我更愿意问,哪个MVC框架可以与Freemarker顺利集成?
发布于 2011-04-04 21:33:18
Spring框架为您的视图层提供了使用FreeMarker所需的一切。
发布于 2011-08-16 10:18:07
两者都有相当好的freemarker支持。它很容易打开。Struts2更像是基于pojo的。Spring更接近于servlet api。Spring在spring.ftl中的默认宏需要做一些工作,您可能需要使用自己的宏。如果一个对象不存在,一些宏就会爆炸,而不是优雅地测试它,如果它不在那里,就继续执行。
与Struts2默认验证相比,我更喜欢Spring通过注释进行验证的应用程序。但是,在Struts2中,通过重定向保留验证错误会更容易。对于Spring,你最终需要推出自己的解决方案,我觉得框架应该隐藏更多的解决方案。需要将容易出错的spring.bind宏与freemarker模板一起使用比需要的要麻烦得多。
Spring3.1应该能够更好地支持这种通过重定向发生的验证错误。
还要注意,在Spring中,我通常使用多个视图解析器。例如,我仍然保留对.jsp的支持。
<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>https://stackoverflow.com/questions/5539202
复制相似问题