侯体宗的博客
  • 首页
  • Hyperf2.1版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

【Velocity四】Velocity与Java互操作

Java  /  管理员 发布于 2年前   139

Velocity出现的目的用于简化基于MVC的web应用开发,用于替代JSP标签技术,那么Velocity如何访问Java代码.本篇继续以Velocity三http://bit1129.iteye.com/blog/2106142中的例子为基础,

 

   POJO

  

package com.tom.servlets;public class User {    private String name;    private String passwd;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getPasswd() {        return passwd;    }    public void setPasswd(String passwd) {        this.passwd = passwd;    }}

 

 

   Service

  

package com.tom.servlets;public class UserService {    public User get(String userId) {        User user = new User();        user.setName("tom");        user.setPasswd("tom_pwd");        return user;    }    public User get(Long userId) {        User user = new User();        user.setName("Jack");        user.setPasswd("Jack");        return user;    }        public void save() {//用于测试,vm是否可以调用无参数的方法        System.out.println("save is called");    }}

  

   Servlet代码

   

package com.tom.servlets;import org.apache.velocity.Template;import org.apache.velocity.context.Context;import org.apache.velocity.tools.view.VelocityViewServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class TestVelocityViewServlet extends VelocityViewServlet {    @Override    protected Template handleRequest(HttpServletRequest request, HttpServletResponse response, Context ctx) {        UserService userService = new UserService();        ctx.put("userService" , userService);//把userService对象设置到Context中        return getTemplate("obj.vm");    }}

 

 

   vm代码

 

  

<!--obj.vm--><html>    <body>        <p>$userService</p> ##call userService's toString method        #set($user = $userService.get(0)) ##When call the java method on its object, only string typed parameters can be accepted        <p>$user.name</p> ##print #$user.name as it is, because $user can't be resolved        <p>$user.getName()</p> ##Equivalent with $user.name        #set($user = $userService.get("0"))        <p>$user.name</p> ##print $$user.name as it is, because $user can't be resolved        <p>$user.getName()</p> ##Equivalent with $user.name        <p>$user.save()</p> ##$user.save can't be resolved,so vm can't resolve method that takes no parameter    </body></html>

 

   运行结果

com.tom.servlets.UserService@48b4721b

$user.name

$user.getName()

tom

tom

$user.save()

 

    总结

1.可以通过Velocity将Java的对象注入到Context中,这样在vm中可以获得这个注入的Java对象,例子中调用了这个Java对象的toString方法

2.在vm中,仅仅能调用Java对象的带有String类型参数的方法,这是非性常巨大的局限,因为在vm中,没法像在JSP中通过<%%>创建Java对象,然后把它作为参数调用Java的方法

3.对于setter和getter,可以直接使用属性进行方法,例如$user.getName和$user.name是等价的,不管User类是否定义了name属性(比如把User类中的属性name改名为xname,getName和setName方法名不变)

4.$user.save()不能正确解析,也就是说,vm只能调用Java对象带String类型参数的方法(POJO的get方法除外)

 

 

 

 


  • 上一条:
    java枚举序列化问题
    下一条:
    【Java范型一】Java范型详解之范型集合和自定义范型类
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • TP(3/5)
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • java 正则表达式基础,实例学习资料收集大全 原创(0个评论)
    • java正则表达式彻底研究(0个评论)
    • java正则表达式验证函数(0个评论)
    • MVC、MVP和MVVM分别是什么(0个评论)
    • java 单例模式(饿汉模式与懒汉模式)(0个评论)
    • 近期文章
    • redis安全配置之修改端口、添加密码流程步骤及启动使用(0个评论)
    • PHP + Memcache实现简单的统计当前在线人数功能(0个评论)
    • Thinkphp5.1框架中实现Session+Redis会话共享流程步骤(0个评论)
    • go语言中使用Signbit()函数判断一个整数是正数或负数(0个评论)
    • 删库跑路之一链家程序员删除公司9TB数据被判7年,望各大码农警之!(0个评论)
    • Laravel角色和权限:拦截器Gates和策略Policies的解释(0个评论)
    • Laravel 9.12版本发布(0个评论)
    • go语言中实现把数据写入文件函数WriteFile()编写(0个评论)
    • go语言之如何加入字符串?(0个评论)
    • go语言之以x因子的倍数重复一个字符(0个评论)
    • 近期评论
    • 博主 在

      hyperf框架常用命令-在centos7中退出命令及在docker容器中退出命令中评论 @路过的靓仔:cdn静态资源被墙,已修复..
    • GGGGGGGGG 在

      layui框架常用输入框介绍中评论 写的很好解决问题..
    • 路过的靓仔 在

      hyperf框架常用命令-在centos7中退出命令及在docker容器中退出命令中评论 剩下好多 wait 状态的..
    • 激光豆芽 在

      为什么你不能安逸?国内996为什么没有国外955香?中评论 国内现在无意义的内卷太多了..
    • 激光豆芽 在

      阿里云香港服务器搭建自用vpn:Shadowsocks使用流程步骤中评论 厉害了..
    • 2016-11
    • 2018-03
    • 2020-03
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客