---
title: "Spring3 mvc 视图缓存的一点注意"
author: "BestUpon"
date: "2010-12-13T09:42:26Z"
tags: []
categories: ["企业架构"]
slug: "spring3-mvc"
draft: false
lastmod: "2010-12-13T09:42:26Z"
id: 840491
url: "https://bestupon.iteye.com/blog/840491"
---

Spring3 MVC 中如果使用如下代码：查询navigations之后，put到model中，

```
	@Override
	@RequestMapping(value = "/frontpage", method = RequestMethod.GET)
	public ModelAndView index() {
		List<Navigation> navigations = navigationManager.queryAll(new LinkedHashMap<String, String>());
		HashMap<String, Object> model = new HashMap<String, Object>();
		model.put("currentDate", getCurrentDate());
		model.put("basePath", getBasePath());
		model.put("navigations", navigations);
		return new ModelAndView("product/frontpage/productlist",model);
	}
```

 在页面采用如下形式访问：

```
<c:forEach items="${navigations}" var="navigation">
				<LI id="ProducType${navigation.id}Home">
					<a href="${basePath}/${navigation.navigationURL}" target="${navigation.target}"><span>${navigation.navigationName }</span> </a>
				</LI>
			</c:forEach>
```

 是正常的，但是要是你手动的更改了数据库中记录，比如删掉了一条数据之后，Spring会认为你这个视图将不存在了！这是为什么呢？原因是Sping的缓存机制，我们在配置viewResolvers 的时候使用了如下的属性：

 

```
<bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
					<property name="cache" value="true" />
					<property name="prefix" value="" />
					<property name="suffix" value=".ftl" />
					<property name="exposeSpringMacroHelpers" value="true" />
					<property name="exposeRequestAttributes" value="true" />
					<property name="exposeSessionAttributes" value="true" />
					<property name="contentType" value="text/html;charset=UTF-8" />
				</bean>
```

 将其&lt;property name="cache" value="true" /&gt;更改为：&lt;property name="cache" value="false" /&gt; 之后将不会有这种事情发生了