简体   繁体   English

如何使用Restlet从多重选择中获取值

[英]How to get values from multi select with restlet

I have some html forms which have select inputs that can have multiple values selected. 我有一些具有选择输入的html表单,这些输入可以选择多个值。 When I post them to my Restlet service I only the currently selected value. 当我将它们发布到我的Restlet服务时,我只有当前选定的值。 I know that if this were a plain servlet I could use request.getParameterValues(... to get to the array of selected values, but I can't seem to find the equivalent in Restlet. From what I can tell the service maps the request to a JsonRepresentation but I don't see an equivalent method for accessing the parameter values. 我知道,如果这是一个普通的servlet,我可以使用request.getParameterValues(...来获取所选值的数组,但是我似乎无法在Restlet中找到等效的值。据我所知,该服务映射了请求到JsonRepresentation,但没有看到用于访问参数值的等效方法。

Does anyone know a way to do this with Restlet2.x? 有人知道使用Restlet2.x做到这一点的方法吗?

In fact, it depends on the way you post the form from the client. 实际上,这取决于您从客户端发布表单的方式。 Your question lets me think that you use URL encoded form ( Content-Type: application/x-www-form-urlencoded ). 您的问题让我认为您使用的是URL编码形式( Content-Type: application/x-www-form-urlencoded )。

In this case, you can extract the submitted data using the class Form of Restlet, as described below: 在这种情况下,您可以使用Restlet类Form提取提交的数据,如下所述:

public class MyServerResource extends ServerResource {
    @Post
    public void handleForm(Form myForm) {
        // Equivalent from request#getParameterValues for Servlet
        String[] values = myForm.getValuesArray("mykey");
        (...)
    }
}

If you want to get a query parameter, simply use the method getQuery to get the associated form object: 如果要获取查询参数,只需使用方法getQuery即可获取关联的表单对象:

public class MyServerResource extends ServerResource {
    @Post
    public void handleForm(Form myForm) {
        // Equivalent from request#getParameterValues for Servlet
        String[] values = getQuery().getValuesArray("mykey");
        (...)
    }
}

Hope it helps you, Thierry 希望对您有帮助,蒂埃里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM