spring mvc整合velocity,为什么页面上的变量不能正常显示
1个回答
2016-01-19 · 知道合伙人数码行家
huanglenzhi
知道合伙人数码行家
向TA提问 私信TA
知道合伙人数码行家
采纳数:117538
获赞数:517196
长期从事计算机组装,维护,网络组建及管理。对计算机硬件、操作系统安装、典型网络设备具有详细认知。
向TA提问 私信TA
关注
展开全部
配置:
1.在pom.xml增加依赖的velocity包
[html] view plain copy print?
<dependency>
<groupId>velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.5</version>
</dependency>
2.在servlet-context.xml中增加以下内容,如果有jsp的配置先注释掉
[html] view plain copy print?
<beans:bean id="velocityConfig"
<span style="white-space:pre"> </span>class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<beans:property name="resourceLoaderPath" value="/WEB-INF/views" />
<beans:property name="configLocation" value="classpath:common/velocity.properties" />
</beans:bean>
<beans:bean id="velocityViewResolver"
class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<beans:property name="suffix" value=".htm" />
</beans:bean>
3.在resources/common目录下创建velocity.properties
[html] view plain copy print?
#encoding
input.encoding =UTF-8
output.encoding=UTF-8
contentType=text/html;charset=UTF-8
#autoreload when vm changed
file.resource.loader.cache=false
file.resource.loader.modificationCheckInterval =1
velocimacro.library.autoreload=false
4.新建testController
[java] view plain copy print?
@RequestMapping(value="/test")
@Controller
public class TestController {
@RequestMapping(value="/index")
public String index(Model model) {
String name = "tester";
model.addAttribute("name", name);
return "test/index";
}
}
5.新建test/index.htm模板
<html>
<head>
</head>
<body>
hello $name!
</body>
</html>
6.访问http://localhost/test/index
显示 hello tester!
1.在pom.xml增加依赖的velocity包
[html] view plain copy print?
<dependency>
<groupId>velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.5</version>
</dependency>
2.在servlet-context.xml中增加以下内容,如果有jsp的配置先注释掉
[html] view plain copy print?
<beans:bean id="velocityConfig"
<span style="white-space:pre"> </span>class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<beans:property name="resourceLoaderPath" value="/WEB-INF/views" />
<beans:property name="configLocation" value="classpath:common/velocity.properties" />
</beans:bean>
<beans:bean id="velocityViewResolver"
class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<beans:property name="suffix" value=".htm" />
</beans:bean>
3.在resources/common目录下创建velocity.properties
[html] view plain copy print?
#encoding
input.encoding =UTF-8
output.encoding=UTF-8
contentType=text/html;charset=UTF-8
#autoreload when vm changed
file.resource.loader.cache=false
file.resource.loader.modificationCheckInterval =1
velocimacro.library.autoreload=false
4.新建testController
[java] view plain copy print?
@RequestMapping(value="/test")
@Controller
public class TestController {
@RequestMapping(value="/index")
public String index(Model model) {
String name = "tester";
model.addAttribute("name", name);
return "test/index";
}
}
5.新建test/index.htm模板
<html>
<head>
</head>
<body>
hello $name!
</body>
</html>
6.访问http://localhost/test/index
显示 hello tester!
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询