当前位置: 代码迷 >> Web前端 >> GWT2.0请求形式
  详细解决方案

GWT2.0请求形式

热度:493   发布时间:2012-10-21 09:00:07.0
GWT2.0请求方式

?????? GWT默认请求方式采用RPC (remote procedure call).在GWT当中有三种请求方式RPC、Form、RequestBuilder.

Form表单形式 [可参考google代码]:

?? 客户端调用:

                //设置请求地址(url action)
		formPanel.setAction(GWT.getModuleBaseURL() + "myFormHandler");
		//form模式(文件上传、普通模式)
		formPanel.setEncoding(FormPanel.ENCODING_URLENCODED);
		//请求方式(POST、GET)
		formPanel.setMethod(FormPanel.METHOD_GET);
		//验证:请求之前判断参数是否为空等操作
		formPanel.addSubmitHandler(new FormPanel.SubmitHandler() {
			
			@Override
			public void onSubmit(SubmitEvent event) {
				if (box.getText().trim().equals("")) {
					Window.alert("请输入");
					//撤销form.submit()
					event.cancel();
				}
			}
		});
		//完成:与服务器交互完成后的提示
		formPanel.addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler() {
			public void onSubmitComplete(SubmitCompleteEvent event) {
				Window.alert(event.getResults());
			}
		});

?? 提交:

final Button button = new Button("form test");
		button.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
                                //form提交
				formPanel.submit();
			}
		});

?? 简单servlet后台处理:

package org.server;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyServlet extends HttpServlet {  
    private static final long serialVersionUID = 1L;  
  
    @Override  
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)  
            throws ServletException, IOException {  
        doPost(req, resp);  
    }  
  
    @Override  
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)  
            throws ServletException, IOException {  
        resp.setContentType("text/plain;charset=UTF-8");  
        resp.getWriter().write(req.getParameter("textbox"));  
    }  
}  

?? GWT配置:

<?xml version="1.0" encoding="UTF-8"?>
<module rename-to='formpanel'>
	<inherits name='com.google.gwt.user.User' />
	<inherits name='com.google.gwt.user.theme.standard.Standard' />
	<entry-point class='org.client.Formpanel' />
	<source path='client' />
	<source path='shared' />
	<servlet class="org.server.MyServlet" path="/myFormHandler" />
</module>

?? web.xml配置

<servlet>
		<servlet-name>myServlet</servlet-name>
		<servlet-class>org.server.MyServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>myServlet</servlet-name>
		<url-pattern>/formpanel/myFormHandler</url-pattern>
	</servlet-mapping>
?

???? RPC方式 [参考google例子]:需要两个接口一个实现类。

@RemoteServiceRelativePath("greet")//访问路径
public interface GreetingService extends RemoteService {
	String greetServer(String name) throws IllegalArgumentException;
}

?

public interface GreetingServiceAsync {
	void greetServer(String input, AsyncCallback<String> callback)
			throws IllegalArgumentException;
}

?

@SuppressWarnings("serial")
public class GreetingServiceImpl extends RemoteServiceServlet implements
		GreetingService {

	public String greetServer(String input) throws IllegalArgumentException {
		return input;
	}
}
?
//创建形式:
EmployeeServiceAsync employeeService = GWT.create(EmployeeService.class);

//调用形式
employeeService.saveOrUpdateEmployee(employeeId, employeeName, employeeSurname, employeeJobTitle, 
						new AsyncCallback<Void>() {}
?

???? RequestBuilder方式后面再介绍.

?

?