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个回答

改成以下试试,,我没测试,,只是怀疑 /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]

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?

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问