【问题标题】:Post html form to restlet将html表单发布到restlet
【发布时间】:2015-09-27 11:07:54
【问题描述】:

我有一个html form,需要提交给restlet。看起来很简单,但表格总是空着。

这是表格:

<form action="/myrestlet" method="post"> 
    <input type="text" size=50 value=5/>
    <input type="text" size=50 value=C:\Temp/>
    (and a few other input type texts)
</form>

restlet:

@Post
public Representation post(Representation representation) {
    Form form = getRequest().getResourceRef().getQueryAsForm();
    System.out.println("form " + form);
    System.out.println("form size " + form.size());
}

我也尝试过这样的形式:

Form form = new Form(representation);

但它总是以 [] 的形式出现,大小为 0。

我错过了什么?

编辑:这是我正在使用的解决方法:

String query = getRequest().getEntity().getText();

这包含来自form 的所有值。我必须解析它们,这很烦人,但它确实有效。

【问题讨论】:

  • 请求参数丢失。
  • @RomanC 你能详细说明一下吗?
  • 不,我对上面的代码不熟悉,我只是看到HTML代码中有一些错别字。
  • HTML 中有哪些拼写错误?
  • 错字只是没有引号的值。

标签: java html restlet


【解决方案1】:

这是从 Restlet 服务器资源中提交的 HTML 表单(内容类型为 application/x-www-form-urlencoded)获取值的正确方法。事实上,这就是你所做的。

public class MyServerResource extends ServerResource {
    @Post
    public Representation handleForm(Representation entity) {
        Form form = new Form(entity);

        // The form contains input with names "user" and "password"
        String user = form.getFirstValue("user");
        String password = form.getFirstValue("password");

        (...)
    }
}

在您的情况下,实际上并未发送 HTML 表单,因为您没有为表单定义任何属性 name。我使用了您的 HTML 代码,发送的数据为空。您可以使用 Chrome 开发者工具 (Chrome) 或 Firebug (Firefox) 进行检查。

POST /myrestlet HTTP/1.2
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip, deflate
Accept-Language: fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3
Connection: keep-alive
Host: localhost:8182
Referer: http://localhost:8182/static/test.html
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:41.0) Gecko/20100101 Firefox/41.0
Content-Length: 0
Content-Type: application/x-www-form-urlencoded

你应该为你的 HTML 表单使用类似的东西:

<form action="/test" method="post">
  <input type="text" name="val1" size="50" value="5"/>
  <input type="text" name="val2" size="50" value="C:\Temp"/>
  (and a few other input type texts)
  <input type="submit" value="send">
</form>

在这种情况下,请求将是:

POST /myrestlet HTTP/1.2
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip, deflate
Accept-Language: fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3
Connection: keep-alive
Host: localhost:8182
Referer: http://localhost:8182/static/test.html
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:41.0) Gecko/20100101 Firefox/41.0
Content-Length: 23
Content-Type: application/x-www-form-urlencoded

val1=5&val2=C%3A%5CTemp

希望对你有帮助 蒂埃里

【讨论】:

  • 谢谢,现在可以使用了。澄清一下,我需要为输入字段而不是表单本身命名。
【解决方案2】:

这里有一个更简单的方法来实现这一点,它直接将Form声明为Java方法的参数:

public class MyServerResource extends ServerResource {
    @Post
    public Representation handleForm(Form form) {

        // The form contains input with names "user" and "password"
        String user = form.getFirstValue("user");
        String password = form.getFirstValue("password");

    (...)
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 2015-06-04
    • 2014-07-20
    • 1970-01-01
    相关资源
    最近更新 更多