gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:48:34 25 4
gpt4 key购买 nike

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

html:

<!DOCTYPE html>
<html>
<head><title>Login Page</title></head>
<body>
<a href="/activateUserPage">activate user</a>
<br/>
<a href="/login">log in</a>
<br/>

<form id='login' action='/login' method='post'>
<fieldset >
<legend>Login</legend>
<input type='hidden' name='submitted' id='submitted' value='1'/>

<label for='username' >UserName: </label>
<input type='text' name='username' id='username' maxlength="50"/>

<label for='password' >Password: </label>
<input type='password' name='password' id='password' maxlength="50"/>

<input type='submit' name='Submit' value='Submit' />
</fieldset>
</form>
</body>
</html>

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<HttpServerRequest>()
{
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 + "html/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<Buffer>()
{
@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<String>> params = qsd.getParameters();
System.out.println(params);
}
}
});
}
}

最佳答案

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

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

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

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

关于java - 如何使用 VERTX 处理程序获取 POST 表单数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14390322/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com