buffalo入门

jar包

 

配置/buffaloSpring/WebRoot/WEB-INF/applicationContext.xml

<beans>
	
	<bean id="userService" class="com.java.service.UserManager"></bean>
	
	<bean name="buffaloConfigBean" class="net.buffalo.service.BuffaloServiceConfigurer">
		<property name="services">
            <map>
            	<entry key="userService"><ref bean="userService"/></entry>
            </map>
        </property>
	</bean>
	
</beans>

 

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
    <display-name>Buffalo Web Remoting Demostration Application</display-name>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			/WEB-INF/applicationContext.xml
		</param-value>
	</context-param>
	
	<servlet>
		<servlet-name>bfapp</servlet-name>
		<servlet-class>net.buffalo.web.servlet.ApplicationServlet</servlet-class>
	</servlet>
	
	<servlet>
		<servlet-name>context</servlet-name>
		<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	
	<servlet-mapping>
		<servlet-name>bfapp</servlet-name>
		<url-pattern>/bfapp/*</url-pattern>
	</servlet-mapping>
	
	<welcome-file-list>
    	<welcome-file>login.jsp</welcome-file>
  	</welcome-file-list>
	
</web-app>

 

   服务类UserManager.java

public class UserManager {
 Logger log = Logger.getLogger(UserManager.class);

 // id,name为页面提交来的字段
 public String login(String id, String name) {
  log.info(">>>UserManager.logins");
  return id + name + " Login success !";
 }

 // user为页面提交来的对象
 public User getCustomreObj(User user) {
  System.out.println(user.getId());
  System.out.println(user.getName());
  return user;
 }

 //arg为页面提交来的Map对象
 public Map object(Map arg) {
  return arg;
 }

}

 

写这文章主要是为了说明这个无参的构造器很重要,u[Buffalo.BOCLASS] = "com.java.bean.User"时要用到,

要不会报错 fail to initialize type......

 

public class User implements Serializable {

	private String id;
	private String name;

	public User() {
	}

	public User(String id, String name) {
		this.id = id;
		this.name = name;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}

  

    <script language="javascript">
		var endPoint="<%=request.getContextPath()%>/bfapp";
		var buffalo = new Buffalo(endPoint);
 
 		//提交字段
		function login() {
        	var id=$("id").value;
        	var name=$("name").value;
        	//alert(id+name);
 			buffalo.remoteCall("userService.login", [id,name], function(reply){
  				$("form_infomsg").innerHTML=reply.getResult();
 			})
		}
		
		//提交对象
		function submitObj(){
			var userObj = Buffalo.Form.formToBean("form1", "com.java.bean.User");//buffalo实现form表单元素绑定是一对一的,form里包含多余的元素会报错
		    buffalo.remoteCall("userService.getCustomreObj", [userObj], function(reply){
		        $("form_infomsg").innerHTML="obj:"+reply.getResult().id+"-"+reply.getResult().name;
		    })
		}
				
		function submitMap() {
			var a = {};
			a[Buffalo.BOCLASS] = "java.util.Map";
			a["a"] = "A";
			a["b"] = true;
			a["c"] = 123.4;
			var u = {};
			u[Buffalo.BOCLASS] = "com.java.bean.User";
			u.id = "234";
			u.name = "ooxx";
			a["u"] = u;
			buffalo.remoteCall("userService.object", [a], function(reply) {
					alert("return userName:" + reply.getResult()["u"].name);
				});
	}
		

</script>
    
	</head>
	<body>
<h1>Form demos</h1>
<hr/>
<div id="form_infomsg" style="color:blue"></div>
<form id="form1" name="form1" method="get" onsubmit="return false;">
  <label for="textfield">ID</label>
  <input type="text" id="id" name="id" value="001"/>
  <br />
  <label for="label">NAME</label>
  <input type="text" id="name" name="name" value="Whisky"/>
  <input type="text" id="age" name="age" value="11"/>
  <br />
 <input type="button" value="submit" onclick="login();"/>
 <input type="button" value="submitObj" onclick="submitObj();"/>
 <input type="button" value="submitMap" onclick="submitMap();"/>
 
</form>

  a["c"] = 1;//若c为数字类型则要使用parseInt(..)或Number(..),不然传到后台值可能变了

你可能感兴趣的:(JavaScript,bean,Web,xml,servlet)