`
log_cd
  • 浏览: 1089242 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Buffalo.BOCLASS与Buffalo.Form.formToBean,session

阅读更多
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 context
context = net.buffalo.request.RequestContext.getContext();

// Get session value
Map session = context.getSession();
String username = (String)session.get("username");
// Update the session value, will refresh the session immediately
session.put("username", "newUsername");

// cookie
Map cookieMap = context.getCookie();
Cookie cookie = cookieMap.get("cookieName");
// update cookie
Cookie c = new Cookie("name", "value");
cookieMap.put(c.getName(), c);

// ServletContext
Map 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()
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics