lpwork2012
2010-01-22 15:45
浏览 335
已采纳

SpringMVC 整合FreeMarker时的问题

springMVCr的配置如下:
[code="java"]
<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:component-scan base-package="com.ahmi.controller" />
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />

<bean id="freemarkerResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
    <property name="order" value="1"/>
    <property name="prefix" value="/WEB-INF/freemarker"/>
    <property name="suffix" value=".ftl"/>
    <property name="contentType" value="text/html;charset=utf-8"/>
    <property name="viewClass">
        <value>org.springframework.web.servlet.view.freemarker.FreeMarkerView</value>
    </property>
</bean>

<bean id="jspResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" p:order="2" />

<bean id="freemarkerConfig"
    class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">

    <property name="defaultEncoding" value="UTF-8"/>  
</bean>


[/code]
Controller如下:
[code="java"]
@Controller
@RequestMapping("/user")
@Scope("prototype")
public class UserController {

@RequestMapping
public String show(@RequestParam long userId,ModelMap model){
    System.out.println(Config.getString("user#Test"));
    User u = userService.getUser(userId);
    System.out.println(u.getUser2().size());
    model.addAttribute("u",u);
    return "/user/test";
}

}
[/code]
在WEB-INF的freemarker/user目录下有test.ftl文件,但是通过路径:http://localhost:8080/Test/user?method=show&userId=20访问的时候却不能跳到ftl模板处。
分析日志时发现有这么几句话:
2010-01-22 15:48:41,468 [http-8080-1] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'freemarkerConfig'
2010-01-22 15:48:41,468 [http-8080-1] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Invoking afterPropertiesSet() on bean with name '/user/test'
2010-01-22 15:48:41,468 [http-8080-1] DEBUG freemarker.cache - Could not find template in cache, creating new one; id=[WEB-INF/freemarker/user/test.ftl[zh_CN,UTF-8,parsed] ]
[color=red]2010-01-22 15:48:41,500 [http-8080-1] DEBUG org.springframework.web.servlet.view.freemarker.FreeMarkerView - No FreeMarker view found for URL: /WEB-INF/freemarker/user/test.ftl[/color]
2010-01-22 15:48:41,500 [http-8080-1] DEBUG org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver - Cached view [/user/test]
2010-01-22 15:48:41,500 [http-8080-1] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Invoking afterPropertiesSet() on bean with name '/user/test'
2010-01-22 15:48:41,500 [http-8080-1] DEBUG org.springframework.web.servlet.view.InternalResourceViewResolver - Cached view [/user/test]

非常郁闷啊,我在这个目录下确实存在模板文件啊,为什么它说不存在呢??有人知道为什么吗?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • CaiHuajiang 2010-01-22 20:13
    已采纳

    改成以下试试,,我没测试,,只是怀疑 /WEB-INF/freemarker/user/test.ftl 是说在模板位置找不到 /WEB-INF/freemarker/user/test.ftl 。。
    那么你配置一下模板位置

    [code="xml"]<?xml version="1.0" encoding="UTF-8"?>
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="com.ahmi.controller" />
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
    
    <bean id="freemarkerResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
        <property name="order" value="1"/>
        <property name="suffix" value=".ftl"/>
        <property name="contentType" value="text/html;charset=utf-8"/>
        <property name="viewClass">
            <value>org.springframework.web.servlet.view.freemarker.FreeMarkerView</value>
        </property>
    </bean>
    
    <bean id="jspResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" p:order="2" />
    
    <bean id="freemarkerConfig"
        class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
        <property name="templateLoaderPath" value="/WEB-INF/freemarker/"/>
        <property name="defaultEncoding" value="UTF-8"/>  
    </bean>
    


    [/code]

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • hermit1216 2010-01-22 16:07

    First of all, /WEB-INF/freemarker would only work as a path from within WebApplicationContext; otherwise Spring would attempt to resolve it as file system path rather than servlet context path. Is the excerpt you've posted above from the context being loaded by DispatcherServlet?

    Secondly, is there any reason why are you using configuration directly instead of using Spring's ViewResolver?

    Finally, IOException can mean many different things. Can you post a full stack trace?

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题