spring3.2+velocity 实例

阅读更多
Velocity,名称字面翻译为:速度、速率、迅速,用在Web开发里.Velocity是一种Java模版引擎技术,该项目由Apache提出,由另外一种引擎技术Webmacro引深而来。那什么是官方的Velocity定义呢?Apache对它的定义是:一种基于Java的模板引擎,但允许任何人使用简单而强大的模板语言来引用定义在Java代码中的对象.

程序代码在附件压缩包中:boce-all-velocityweb.zip

1下载依赖包配置文件在附件中

2.设置layout-servlet.xml 文件
class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">








class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">




text/html;charset=UTF-8


















web.xml文件


layout
org.springframework.web.servlet.DispatcherServlet

2



layout
*.htm
*.do
*.json
*.html
*.vm



3.实现代码:
package boce.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping(value = "/demo")
public class DemoVelocity {
// Logger logger = LoggerFactory.getLogger(VelocityDemo.class);
@RequestMapping(value = "/welcome")
public String index(Model model) throws Exception {
model.addAttribute("name", "highkgao");

model.addAttribute("age", 20);

System.out.println(model.toString());

return "welcome";
}



@RequestMapping(value = "/test.do")
public String test(Model model) throws Exception {
model.addAttribute("name", "highkgao");
System.out.println(model.toString());
return "index";
}


@RequestMapping(value="/autoBind", method={RequestMethod.GET})
  public String autoBindLogin(Model model){
      model.addAttribute("accountmodel", new AccountModel());
      return "login";
  }


@RequestMapping(value="/autoBind2", method={RequestMethod.POST})
    public String autoBindResult(Model model,AccountModel am){
        model.addAttribute("accountmodel",am );
        return "autoBindResult";
    }

}


运行结果:
运行1

spring3.2+velocity 实例_第1张图片

运行2

spring3.2+velocity 实例_第2张图片

运行3

spring3.2+velocity 实例_第3张图片



  • spring3.2+velocity 实例_第4张图片
  • 大小: 33.4 KB
  • spring3.2+velocity 实例_第5张图片
  • 大小: 23.5 KB
  • spring3.2+velocity 实例_第6张图片
  • 大小: 20.3 KB
  • boce-all-velocityweb.zip (29.7 KB)
  • 下载次数: 0
  • 查看图片附件

你可能感兴趣的:(velocity,spring)