gwt 中FormPanel的使用

package com.google.gwt.sample.hello.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.FormHandler;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.FormSubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormSubmitEvent;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

/**
 * FormPanel用法
 * @author crane.ding
 * @version 1.0 2008-9-26
 */
public class Hello implements EntryPoint {

	public void onModuleLoad() {
		HorizontalPanel outer = new HorizontalPanel();
		final FormPanel formPanel = new FormPanel();
		final TextBox box = new TextBox();
		final Button button = new Button("ok", new ClickListener() {
			public void onClick(Widget sender) {
				formPanel.submit();
			}
		});

		box.setName("textbox");

		formPanel.setAction(GWT.getModuleBaseURL() + "/myFormHandler");
		formPanel.setEncoding(FormPanel.ENCODING_URLENCODED);
		formPanel.setMethod(FormPanel.METHOD_GET);
		formPanel.addFormHandler(new FormHandler() {
			public void onSubmit(FormSubmitEvent event) {
				if (box.getText().trim().equals("")) {
					Window.alert("请输入");
					event.setCancelled(true);
				}
			}

			public void onSubmitComplete(FormSubmitCompleteEvent event) {
				Window.alert(event.getResults());
			}
		});

		outer.add(box);
		outer.add(button);

		formPanel.setWidget(outer);

		RootPanel.get().add(formPanel);
	}
}

package com.google.gwt.sample.hello;

import java.io.IOException;

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

/**
 * @author crane.ding
 * @version 1.0 2008-9-26
 */
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"));
	}
}

<module>
	<inherits name="com.google.gwt.user.User"/>
	<entry-point class="com.google.gwt.sample.hello.client.Hello"/>
	<servlet class="com.google.gwt.sample.hello.MyServlet" path="/myFormHandler"/>
</module>

你可能感兴趣的:(UI,xml,servlet,Google,gwt)