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中的列表。
和 。 最后⼀个是⽤于⽇期的特殊适配器,因为应该转换为毫秒长,然后再转换回。 到⽬前为⽌,⼀切都很好。 但是,如何在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
} catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } parametrizedUrl = (); } public String getParametrizedUrl() { return parametrizedUrl; }}在XHTML中使⽤– h:outputLink⽰例
参考:来⾃ Oleg Varaksin的 博客上。翻译⾃:
发布者:admin,转转请注明出处:http://www.yc00.com/web/1687679762a30970.html
评论列表(0条)