初次使用Freemarker,在整合Spring mvc时出现在ftl页面中标签直接显示出来了,如下图: 搞了很久,都没找出问题所在,工程配置如下: web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app id="cms" version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:spring/applicationContext*.xml </param-value> </context-param> <!-- spring mvc 配置 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- spring 配置文件,名称的路径都可以配置,如果不配置此项则自动扫描/WEB-INF/springmvc-servlet.xml,此文件名规则为servlet名-servlet.xml --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/springmvc-*.xml</param-value> <!-- 加载多个配置文件 <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/conf/applicationContext_*.xml, /WEB-INF/conf/user-servlet.xml </param-value> --> </init-param> <!--servlet加载方式,启动容器时加载 --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <!-- 拦截所有请求 --> <url-pattern>*.action</url-pattern> </servlet-mapping> <filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- spring 加载log4j配置文件 --> <context-param> <param-name>log4jConfigLoaction</param-name> <param-value>classpath:log4j.properties</param-value> </context-param> <!-- 刷新Log4j配置文件的间隔,60秒 --> <context-param> <param-name>log4jRefreshInterval</param-name> <param-value>60000</param-value> </context-param> <!--可选配置,定义web 应用的别名,如果不设置,缺省为"webapp.root",当tomcat加载多个项目时,会发生名称冲突 --> <context-param> <param-name>webAppRootKey</param-name> <param-value>webName.root</param-value> </context-param> <!-- 静态资源处理 --> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.css</url-pattern> <url-pattern>*.gif</url-pattern> <url-pattern>*.js</url-pattern> <url-pattern>*.jpg</url-pattern> </servlet-mapping> <!-- Spring 加载 Log4j 的监听器 --> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> <!-- 防止内存泄漏 --> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <!-- init servlet --> <servlet> <servlet-name>SystemInitServlet</servlet-name> <servlet-class> com.dhf.cms.init.SystemInitServlet </servlet-class> <load-on-startup>10</load-on-startup> </servlet> <welcome-file-list> <welcome-file>base/admin/index.action</welcome-file> </welcome-file-list> <error-page> <error-code>500</error-code> <location>/WEB-INF/view/error500.jsp</location> </error-page> <error-page> <error-code>404</error-code> <location>/WEB-INF/view/error404.jsp</location> </error-page> </web-app> springmvc-servlet.xml也加入了: <!-- Freemarker配置 --> <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> <property name="templateLoaderPath" value="/WEB-INF/view/" /> <property name="freemarkerSettings"> <props> <prop key="template_update_delay">0</prop> <prop key="default_encoding">UTF-8</prop> <prop key="number_format">0.##########</prop> <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop> <prop key="classic_compatible">true</prop> <prop key="template_exception_handler">ignore</prop> </props> </property> </bean> <!--视图解释器 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> <property name="suffix"> <value>.ftl</value> </property> <property name="contentType" value="text/html;charset=UTF-8"></property> </bean> Controller代码: import java.util.HashMap; import java.util.Map; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import org.apache.log4j.Logger; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView; import com.dhf.cms.base.bean.AdminInfo; import com.dhf.cms.base.bean.BaseBean; import com.dhf.cms.base.service.AdminService; import com.dhf.cms.global.LogKeys; import com.dhf.cms.global.ReturnCode; import com.dhf.cms.init.manager.PropertiesManager; import com.dhf.cms.response.BaseResponse; import com.dhf.cms.tool.Coder; import com.dhf.cms.tool.CommonTool; import com.dhf.cms.validation.Validationor; @Controller @RequestMapping(value = "base/admin") public class AdminController { @RequestMapping(value = "/index", method = RequestMethod.GET) public ModelAndView index(HttpServletRequest request) { Map<String,String> map = new HashMap<String,String>(); map.put("name", "dinghuafeng"); request.setAttribute("name", "message is added"); ModelAndView mv = new ModelAndView("index"); mv.addObject("name","aaaabbbbb"); //return new ModelAndView("/index",map); return mv; } } 通过http://localhost:8080/cms/base/admin/index.action跳转都是正常,就是标签取值无法显示,是不是Freemarke还依赖了其他的包?请各位大侠解救,谢谢! 引入包如下: |
|
10分 |
Maybe
<bean id=”viewResolver” class=”org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver”> <property name=”suffix”> <value>.ftl</value> </property> <property name=”contentType” value=”text/html;charset=UTF-8″></property> </bean> |
看了下,楼主好像是参照这个写的吧?http://blog.csdn.net/yakson/article/details/9813531
|
|
建议看看完整的FreeMarkerViewResolver配置
|
|
行,我试试看! |
|
改成这样 类似的试试:
<bean id=”viewResolver” class=”org.springframework.web.servlet.view.InternalResourceViewResolver”> <property name=”prefix”> <value>/jsp/</value> </property> <property name=”suffix”> <value>.jsp</value> </property> <property name=”order” value=”1″ /> </bean> |
|
好的,最好告诉我结果,还有如果有异常,请贴出异常信息.. |
|
按照这个例子,还是: 加入代码如下: <!-- 定义FreeMarker视图解析器与属性配置 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> <property name="cache" value="true" /> <property name="prefix" value="" /> <property name="suffix" value=".ftl" /> <property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView" /> <property name="exposeSpringMacroHelpers" value="true" /> <property name="exposeRequestAttributes" value="true" /> <property name="exposeSessionAttributes" value="true" /> <property name="requestContextAttribute" value="rc" /> <property name="contentType" value="text/html;charset=UTF-8" /> </bean> <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> <property name="templateLoaderPath" value="/WEB-INF/view/" /> <property name="freemarkerSettings"> <props> <prop key="template_update_delay">5</prop> <prop key="defaultEncoding">UTF-8</prop> <prop key="url_escaping_charset">UTF-8</prop> <prop key="locale">zh_CN</prop> <prop key="boolean_format">true,false</prop> <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop> <prop key="time_format">HH:mm:ss</prop> <prop key="number_format">0.######</prop> <prop key="whitespace_stripping">true</prop> </props> </property> </bean> index.ftl代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>${title}</title> </head> <body> ${name} </body> </html> |
|
后台无异常,没有任何错误!后台截图如下: |
|
恩,改了配置没? 现在可以了么? 还是原来的? 现在用的是jsp还是html |
|
按照指示将试图解析类改为:InternalResourceViewResolver,后台报错了: org.springframework.beans.factory.BeanCreationException: Error creating bean with name “”viewResolver”” defined in file [E:\Software\apache-tomcat-7.0.12\webapps\cms\WEB-INF\classes\spring\springmvc-servlet.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property “”exposeSpringMacroHelpers”” of bean class [org.springframework.web.servlet.view.InternalResourceViewResolver]: Bean property “”exposeSpringMacroHelpers”” is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter? |
|
页面时index.ftl,配置前缀匹配改为.ftl了!不知道改得对不对! |
|
90分 |
下班了,回去帮你看
|
恩恩,非常感谢!这个问题纠结了好久,网上找的资源都没有这样的情况;唯一找到一个说是缺少一个依赖包javarebel.jar,但是下载了导入没用! |
|
对了,忘了说了,你页头有没有导入下面这几个 <%@ page language=”java” pageEncoding=”UTF-8″%> |
|
<%@ 的引用应该是针对jsp的吧? 页面中也没有用到jstl标签喔。加入你的代码后,还是一样的! |
|
已经解决了,是我在spring配置文件中配置了2个ViewResolver,冲突了,但是启动没有报错,所以导致的,多谢各位回复!结贴
|
|
学习下,遇到同样的问题。
|
|
对了,忘了说了,你页头有没有导入下面这几个
<%@ page language=”java” pageEncoding=”UTF-8″%> <%@ 的引用应该是针对jsp的吧? 页面中也没有用到jstl标签喔。加入你的代码后,还是一样的! sorry,昨天没看清, 现在找了两个应该有用的。 一个还是修改视图解析器,一个是添加strust2标签配置 这是struts2标签的 |
|
我去… |
|
在web.xml加上这段试试
<filter> <filter-name>freemarkerFilter</filter-name> <filter-class>com.uws.core.freemarker.FreemarkerFilter</filter-class> </filter> <filter-mapping> <filter-name>freemarkerFilter</filter-name> <url-pattern>*.ftl</url-pattern> <dispatcher>REQUEST</dispatcher> <dispatcher>FORWARD</dispatcher> </filter-mapping> |