如何使用VERTX处理程序获取POST表单数据?

我能够使用缓冲区处理程序获取表单数据,但它是一个void函数,我无法返回表单数据值。 总共有大约4-7个表单,我不想一遍又一遍地编写相同的处理程序,因为默认函数是void。

HTML:

  Login Page  activate user 
log in
Login

Java的:

 import org.jboss.netty.handler.codec.http.QueryStringDecoder; import org.vertx.java.core.Handler; import org.vertx.java.core.Vertx; import org.vertx.java.core.buffer.Buffer; import org.vertx.java.core.http.HttpServer; import org.vertx.java.core.http.HttpServerRequest; import org.vertx.java.core.http.RouteMatcher; import org.vertx.java.deploy.Verticle; import java.util.Collection; import java.util.List; import java.util.Map; import java.util.Set; /** * Created by IntelliJ IDEA. * User: yao * Date: 1/17/13 * Time: 2:22 PM */ public class Main extends Verticle { @Override public void start() throws Exception { System.out.println("starting the vertx stuff"); final String host = "localhost"; final String port = "8181"; Vertx vertx = Vertx.newVertx(); HttpServer httpServer = vertx.createHttpServer(); ... httpServer.requestHandler(new Handler() { public void handle(HttpServerRequest req) { String path = req.path; /* start mapping of page urls*/ // redirect user to the login page if (path.equals("/")) { req.response.sendFile(dir + "htmlhttps://stackoverflow.com/loginPage.html"); } ... /* end mapping of page urls*/ /* start mapping of form urls */ // login else if (path.equals(login)) { mainLogin(); getFormData(req); } ... /* end mapping of form urls */ /* all other pages */ else { req.response.end("404 - page no exist"); } } }); System.out.println("vertx listening to: " + host + " " + port); httpServer.listen(Integer.valueOf(port), host); } ... private void getFormData(final HttpServerRequest req) { req.bodyHandler(new Handler() { @Override public void handle(Buffer buff) { String contentType = req.headers().get("Content-Type"); if ("application/x-www-form-urlencoded".equals(contentType)) { QueryStringDecoder qsd = new QueryStringDecoder(buff.toString(), false); Map<String, List> params = qsd.getParameters(); System.out.println(params); } } }); } } 

对于java,这完美地工作:

 request.expectMultiPart(true); request.endHandler(req->{ String text = request.formAttributes().get("bigtext"); //got it here //do something ... }); 

我到底做了什么基本上是这样的:

使用post执行ajax调用,需要序列化表单中的数据。

 $.ajax ({ type: "POST", url: "/login", data: $('#frm_login').serialize(), success: function(data) ... 

在后端,vertx将此数据作为缓冲区接收。 rest是通过“&”和“=”拆分来解析缓冲区。

 Map params = new HashMap(); String[] paramSplits = buffer.toString().split("&"); String[] valueSplits; if (paramSplits.length > 1) { for (String param : paramSplits) { valueSplits = param.split("="); if (valueSplits.length > 1) { // add this check to handle empty phone number fields params.put(decode(valueSplits[0]), decode(valueSplits[1])); } } } 

希望这会有助于他人!

现在BodyHandler由vertx提供,使用它将isExpectMultipart设置为true,您将能够读取表单属性

 request.getFormAttribute("username");//to read input named username. 

只需在实际处理程序之前添加此行:

 router.route().handler(BodyHandler.create()); 

这可以使用http请求上的formAttributes来完成。 这是scala中的一个例子

  req.expectMultiPart(true) //Will expect a form req.endHandler({ req.formAttributes() //This is used to access form attributes //some code with attributes }) 

参考: http : //vertx.io/core_manual_java.html#handling-multipart-form-attributes

Interesting Posts