在URL参数中传递复杂对象

在URL参数中传递复杂对象

2023年6月25日发(作者:)

在URL参数中传递复杂对象假设您要传递原始数据类型,例如复杂的Java对象

,,泛型类,数组以及通过URL参数所需的所有内容,以便在页⾯加载后在任何⽹页上预设默认值。 共同的任务? 是的,但是可⽤的解决⽅案主要限于的编码/解码。 我将展⽰的⽅法对数据类型没有任何限制。 URL⼤⼩的限制只有⼀个限制。 长度超过2083个字符的URL在旧的IE版本中可能⽆法正常⼯作。 现代Firefox,Opera和Safari可以处理URL中的⾄少80000个字符。如果我们将对象序列化为JSON并在服务器端反序列化它们,则可以通过URL参数传递任何类型的对象。 编码的JSON字符串具有有效的

格式,这是⼀种⽅法! 好吧,但是有⼀个问题。 如果对象是⾮通⽤类型,则从JSON格式序列化/反序列化可以正常⼯作。 但是,如果对象属于通⽤类型,则通⽤类型信息会因为Java Type Erasure⽽丢失。 在这种情况下该怎么办? 我要演⽰的解决⽅案不仅仅限于JSF,⽽是在开发Java / Web前端时,我正在这个圈⼦中旋转……所以,让我们开始吧。 ⾸先,我们需要⼀个适当的转换器来接收JSON格式的URL参数,并将其转换回Java。 提供了⼀个– 。 怎么运⾏的? 以下⽰例显⽰如何将JsonConverter应⽤于f:viewParam,以将JSON格式的字符串列表转换为Java中的列表。 JsonConverter具有⼀种可选的属性类型。 我们不需要为诸如boolean或int之类的原语提供数据类型信息。 但是通常,类型信息是必要的属性。 它指定值对象的数据类型。 ⽀持任何原始类型,数组,⾮通⽤或通⽤类型。 该类型由完全限定的类名(原始类型除外)组成。 例⼦:'long[]''''''tion''<, r>''GenericClass''ericClass<, r>''ericClass>'该类型的字符串在运⾏时进⾏解析。 JsonConverter的代码在 (供有兴趣的读者使⽤)。 JsonConverter基于其他三个类: ,

和 。 最后⼀个是⽤于⽇期的特殊适配器,因为应该转换为毫秒长,然后再转换回。 到⽬前为⽌,⼀切都很好。 但是,如何在Java端准备这些值作为URL参数呢? 我将展⽰⼀个可⽤于该⽬的的实⽤程序类。 请阅读评论,它们是不⾔⾃明的。import ;import nverter;import ortedEncodingException;import oder;import ontext;import rvletRequest;/** * Builder for request parameters. */public class RequestParameterBuilder { private Logger LOG = ger(); private StringBuilder buffer; private StringBuilder buffer; private String originalUrl; private JsonConverter jsonConverter; private String encoding; private boolean added; /** * Creates a builder instance by the current request URL. */ public RequestParameterBuilder() { this(((HttpServletRequest) rentInstance().getExternalContext().getRequest()).getRequestURL() .toString()); } /** * Creates a builder instance by the given URL. * * @param url URL */ public RequestParameterBuilder(String url) { buffer = new StringBuilder(url); originalUrl = url; jsonConverter = new JsonConverter(); encoding = rentInstance().getExternalContext().getRequestCharacterEncoding(); if (encoding == null) { encoding = 'UTF-8'; } } /** * Adds a request parameter to the URL without specifying a data type of the given parameter value. * Parameter's value is converted to JSON notation when adding. Furthermore, it will be encoded * according to the acquired encoding. * * @param name name of the request parameter * @param value value of the request parameter * @return RequestParameterBuilder updated this instance which can be reused */ public RequestParameterBuilder paramJson(String name, Object value) throws UnsupportedEncodingException { return paramJson(name, value, null); } /** * Adds a request parameter to the URL with specifying a data type of the given parameter value. Data type is sometimes * required, especially for Java generic types, because type information is erased at runtime and the conversion to JSON * will not work properly. Parameter's value is converted to JSON notation when adding. Furthermore, it will be encoded * according to the acquired encoding. * * @param name name of the request parameter * @param value value of the request parameter * @param type data type of the value object. Any primitive type, array, non generic or generic type is supported. * Data type is sometimes required to convert a value to a JSON representation. All data types should be * fully qualified. * @return RequestParameterBuilder updated this instance which can be reused */ public RequestParameterBuilder paramJson(String name, Object value, String type) throws UnsupportedEncodingException { e(type); String jsonValue; if (value == null) { jsonValue = 'null'; } else { jsonValue = tring(null, null, value); } } if (added || ns('?')) { ('&'); } else { ('?'); } (name); ('='); ((jsonValue, encoding)); // set a flag that at least one request parameter was added added = true; return this; } /** * Adds a request parameter to the URL. This is a convenient method for primitive, plain data types. * Parameter's value will not be converted to JSON notation when adding. It will be only encoded * according to the acquired encoding. Note: null values will not be added. * * @param name name of the request parameter * @param value value of the request parameter * @return RequestParameterBuilder updated this instance which can be reused */ public RequestParameterBuilder param(String name, Object value) throws UnsupportedEncodingException { if (value == null) { return this; } if (added || ns('?')) { ('&'); } else { ('?'); } (name); ('='); ((ng(), encoding)); // set a flag that at least one request parameter was added added = true; return this; } /** * Builds the end result. * * @return String end result */ public String build() { String url = ng(); if (() > 2083) { ('URL ' + url + ' is longer than 2083 chars (' + () + '). It may not work properly in old IE versions.'); } return url; } /** * Resets the internal state in order to be reused. * Resets the internal state in order to be reused. * * @return RequestParameterBuilder reseted builder */ public RequestParameterBuilder reset() { buffer = new StringBuilder(originalUrl); e(null); added = false; return this; }}通常,使⽤RequestParameterBuilder的bean通过调⽤paramJson(…)或param(…)提供参数化的URL。import izable;import ortedEncodingException;import ist;import ;import nstruct;import dBean;import nScoped;/** * UrlParameterProvider bean. */@ManagedBean@SessionScopedpublic class UrlParameterProvider implements Serializable { private String parametrizedUrl; @PostConstruct protected void initialize() { RequestParameterBuilder rpBuilder = new RequestParameterBuilder('/views/examples/'); try { List subscriptions = new ArrayList(); ('2'); ('3'); // add the list to URL parameters with conversion to JSON son('subscriptions', subscriptions, '<>'); // add int values to URL parameters without conversion to JSON (just for example) ('min', 20); ('max', 80);

} catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } parametrizedUrl = (); } public String getParametrizedUrl() { return parametrizedUrl; }}在XHTML中使⽤– h:outputLink⽰例 Parametrized URL⼀旦⽤户单击链接并以相对路径/视图/⽰例/登陆⽬标页⾯,他/她将看到⼀个预先检查的h:selectManyCheckbox。 现实世界更加复杂。 实际上,我已经写了很多内置JsonConverter的⾃定义转换器。 因此,附加了⾃定义转换器,⽽不是。 这个主题超出了这篇⽂章。

参考:来⾃ Oleg Varaksin的 博客上。翻译⾃:

发布者:admin,转转请注明出处:http://www.yc00.com/web/1687679762a30970.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信