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

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]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error