新手:在Jersey框架中,在我的例子中如何获取HTML表单对象数据?

发布于 2024-11-07 01:34:14 字数 1123 浏览 1 评论 0原文

当客户端提交HTML表单时,它会发送一个表单对象到服务器,例如:

params={username: USER_INPUT_USERNAME,
       passworkd: USER_INPUT_PASSWORD}

然后,客户端ajax将params对象发送到我的Jersey > 服务器。

在服务器端,我如何获取并解析这个 HTML 表单对象数据 params:(

@GET
@Produces({MediaType.APPLICATION_JSON})
public FormResult getFormResult(@QueryParam("params") Object params) {
                 //How can I define the type of the "params" I received?
                 //Do I need to create a Java Bean which represent the HTML form, 
                 //and use the bean as the type of paprams? or any other way?
}

在上面的代码中,返回类型 FormResult 是一个 POJO bean,它将结果描述为响应客户端)

当我收到params HTML表单对象数据时,如何定义params类型?(上面我定义了它类型为“Object”,这是错误的)。

我是否必须定义一个 POJO bean 来表示 HTML 表单并使用该 bean 来描述 params 的类型?或者在泽西岛还有其他方式吗?

(如果为HTML表单创建POJO bean,如果HTML表单上有复选框,则params将是一个动态对象,仅checked 字段将被添加到 params 对象中,这也是一个问题)

有人可以帮忙吗?

When client side submit a HTML form, it will send a form object to server, like:

params={username: USER_INPUT_USERNAME,
       passworkd: USER_INPUT_PASSWORD}

Then, client ajax send the params object to my Jersey server.

On the server side, how can I get and parse this HTML form object data params:

@GET
@Produces({MediaType.APPLICATION_JSON})
public FormResult getFormResult(@QueryParam("params") Object params) {
                 //How can I define the type of the "params" I received?
                 //Do I need to create a Java Bean which represent the HTML form, 
                 //and use the bean as the type of paprams? or any other way?
}

(In above code, the return type FormResult is an POJO bean which describe the result to response to client)

When I receive the params HTML form object data, how can I define the type of the params ?(above I defined it with type "Object", which is wrong).

Do I must define a POJO bean to represent the HTML form and use that bean to describe the type of the params?? or any other way in Jersey?

(If create POJO bean for the HTML form, If there are check boxes on the HTML form, the params will be an dynamic object, only checked field will be added to the params object, which is also a problem)

Anybody can help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

停顿的约定 2024-11-14 01:34:14

@QueryParam 映射单个查询字符串参数,因此对于 GET 请求,您需要将所有参数枚举为方法参数:

@GET
getFormResult(@QueryParam("name") String name, @QueryParam("age") int age, ... )

如果您提到的 params 实际上是一个查询字符串参数 (http://your.rest.service?params=somethingHere),您可以将其映射到某个类,该类的构造函数采用 String 或静态 < code>valueOf(String) 方法,您可以在其中进行实际解析。

@GET
getFormResult(@QueryParam("params") FormData formData);

FormData 类可以如下所示:

public class FormData {
    public FormData(String s) {
        // populate fields based on the content of s
    }

    // getters / setters / whatever ...
}

编辑:
对于复选框,它并不像看起来那样动态:它们具有相同的名称,因此您将有一个、多个或无与该名称关联的值,具体取决于用户检查的内容。因此,@QueryParam("chk") String[] checkValues 应该足以处理这里的“动态”方面(实际上,“多值”会是一个更好的词)。

The @QueryParam maps an individual queryString parameter, so for a GET request you need to enumerate all your parameters as method arguments:

@GET
getFormResult(@QueryParam("name") String name, @QueryParam("age") int age, ... )

If the params that you mentioned is actually one query string parameter (http://your.rest.service?params=somethingHere), you can map that to some class that has a constructor taking a String, or a static valueOf(String) method where you do the actual parsing.

@GET
getFormResult(@QueryParam("params") FormData formData);

And the FormData class can look like this:

public class FormData {
    public FormData(String s) {
        // populate fields based on the content of s
    }

    // getters / setters / whatever ...
}

EDIT:
For checkboxes, it's not as dynamic as it looks: they have the same name, so you'll have one-, several- or no- value(s) associated with that name, depending on what the user checked. Therefore, a @QueryParam("chk") String[] checkedValues should be enough to handle the "dynamic" aspects here (actually, 'multi-valued' would be a better word).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文