六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 50|回复: 0

Buffalo.BOCLASS与Buffalo.Form.formToBean,session

[复制链接]

升级  11%

65

主题

65

主题

65

主题

举人

Rank: 3Rank: 3

积分
233
 楼主| 发表于 2013-1-27 04:44:43 | 显示全部楼层 |阅读模式
1.客户端 js 的数组来映射服务器端的 Map 对象
(1).service
public  Map object(Map arg)   {               return  arg;    }
(2).js
< script language = " javascript " > var buffalo  =   new  Buffalo(endPoint)function sendObject()   {    var a  =    {} ;    a[Buffalo.BOCLASS]  =   " java.util.Map " ;    a[ " a " ]  =   " A " ;    a[ " b " ]  =   true ;    a[ " c " ]  =   123.4 ;    var u  =    {} ;    u[Buffalo.BOCLASS]  =   " net.buffalo.demo.simple.User " ;    u.id  =   234 ;    u.name  =   " <xml here>& " ;    u.age  =   17 ;    u.sex  =   false ;    u.memo  =   " very beautiful " ;    a[ " u " ]  =  u;    buffalo.remoteCall( " simpleService.object " ,[a], function(reply)   {        alert( " return user memo: "   +  reply.getResult()[ " u " ].memo);    } );}  </ script >

2.将表单序列化为一个dto(Data Transfer Object)对象

(1).dto对象
public class User {    private int id;    private String name;    private int age;    private boolean sex;    private String memo;        public User() {    }}

(2).js
<script language="javascript">var buffalo = new Buffalo(endPoint);function doAnotherSubmit() {    var userObj = Buffalo.Form.formToBean("form1", "net.buffalo.demo.form.User");    buffalo.remoteCall("userService.createUser", [userObj], function(reply){        $("form_infomsg").innerHTML="Form has been submited, username is: " + reply.getResult().username;    })}</script>

3.buffalo中使用session

(1).Buffalo为那些需要使用session, request,servletContext对象的服务提供了BuffaloService类,任何服务只需要继承这个类就可以获得容器对象,如request,session, context等。
例:
import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpSession;import net.buffalo.server.BuffaloService;class LoginService extends BuffaloService{   public boolean login(String username, String password) {    HttpServletRequest request = (HttpServletRequest)getRequest();    HttpSession session = request.getSession();   if (username == "foo" && password="bar") {       session.setAttribute("username", username);       return true;     }      else        {return false;}  }}

(2). 从Buffalo2.0开始,你方便地可以get/set session/cookie/context的值。通过使用RequestContext类,你能以更加轻松的方式全权控制它们的生命周期值。
// Get a thread-safe request contextcontext = net.buffalo.request.RequestContext.getContext();// Get session valueMap session = context.getSession();String username = (String)session.get("username");// Update the session value, will refresh the session immediatelysession.put("username", "newUsername");// cookieMap cookieMap = context.getCookie();Cookie cookie = cookieMap.get("cookieName");// update cookieCookie c = new Cookie("name", "value");cookieMap.put(c.getName(), c);// ServletContextMap application = context.getApplication();Object value = application.get("key");...
注:
1.2.4之前需要继承一个BuffaloService,从1.2.4开始就不需要继承了,引入了线程安全的BuffaloContext对象,只需要通过BuffaloContext.getContext()即可获得一个线程安全的引用,并且对Request的各种属性进行操作。
Map BuffaloContext.getContext().getSession()Map BuffaloContext.getContext().getApplication()Map BuffaloContext.getContext().getCookie()
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表