public Map<String, Object> getReturnOrderListLF(WebRequest request, Pageable pageable) {
PageTemplate pageTemplate = new PageTemplate();
pageTemplate.setSize(pageable.getPageSize());
pageTemplate.setNumber(pageable.getPageNumber());
Map<String, Object> searchParams = Servlets.getParametersStartingWith(request, null);
Map<String, Object> page = returnOrderService.getReturnOrderListLF(searchParams, pageTemplate);
return page;
}
当时看见这个代码就懵逼了,因为不理解究竟是为啥要传入参数request以及servlets这个的调用究竟是为了啥,看了很多百度,回答模棱两可,零零散散,我自己总结下
**
下面这个是相对应的vue代码,承接上下文,一会解释为什么要看这个 重点是 params这个参数
重点还有http.get这个参数 后面的路径是相对应我们平时查询所对应的访问路径 **
http.get("api/returnOrder/getReturnOrderListLF", {params: params}).then(response => {
this.isLoading = false;
if(response.code == 200){
if(response.content != undefined){
for (let i = 0; i < response.content.length; i++) {
let obj = response.content[i];
let imgUrlPath = obj.imgUrlPath;
if(imgUrlPath == undefined){
obj['imgUrlPath'] = '';
}
}
}
response.page = response.number + 1;
this.pagination = response;
}else{
this.$message.error(this.$t('pages.lostAndFoundList.tips.queryFailed'));
console.error("error:"+response.err)
}
});
上面这段前端代码的params的值是什么 就是通过webrequest进行的赋值 得到的数据,咋搞 咋搞 通过servlet代码进入查看 params
public static Map<String, Object> getParametersStartingWith(WebRequest request, String prefix) {
logger.debug("prefix = " + prefix);
Validate.notNull(request, "Request must not be null");
Iterator<String> paramNames = request.getParameterNames();
Map<String, Object> params = Maps.newTreeMap();
if (prefix == null) {
prefix = "";
}
while ((paramNames != null) && paramNames.hasNext()) {
String paramName = paramNames.next();
logger.debug("paramName = " + paramName);
if ("".equals(prefix) || paramName.startsWith(prefix)) {
String unprefixed = paramName.substring(prefix.length());
String[] values = request.getParameterValues(paramName);
if (values != null && values.length == 1) {
Boolean boolValue = StringUtils.convert(values[0]);
if(boolValue == null) {
params.put(unprefixed, values[0]);
} else if(boolValue) {
params.put(unprefixed, true);
} else {
params.put(unprefixed, false);
}
} else {
params.put(unprefixed, values);
}
}
}
return params;
}
通过对接刚才的前端代码 就可以完成访问
webrequest说白了就是请求 可能又有人想问 那么request请求的数据是什么 下面就是源码 自己慢慢欣赏
public interface WebRequest extends RequestAttributes {
/**
* Return the request header of the given name, or {@code null} if none.
* <p>Retrieves the first header value in case of a multi-value header.
* @since 3.0
* @see javax.servlet.http.HttpServletRequest#getHeader(String)
*/
String getHeader(String headerName);
/**
* Return the request header values for the given header name,
* or {@code null} if none.
* <p>A single-value header will be exposed as an array with a single element.
* @since 3.0
* @see javax.servlet.http.HttpServletRequest#getHeaders(String)
*/
String[] getHeaderValues(String headerName);
/**
* Return a Iterator over request header names.
* @since 3.0
* @see javax.servlet.http.HttpServletRequest#getHeaderNames()
*/
Iterator<String> getHeaderNames();
/**
* Return the request parameter of the given name, or {@code null} if none.
* <p>Retrieves the first parameter value in case of a multi-value parameter.
* @see javax.servlet.http.HttpServletRequest#getParameter(String)
*/
String getParameter(String paramName);
/**
* Return the request parameter values for the given parameter name,
* or {@code null} if none.
* <p>A single-value parameter will be exposed as an array with a single element.
* @see javax.servlet.http.HttpServletRequest#getParameterValues(String)
*/
String[] getParameterValues(String paramName);
/**
* Return a Iterator over request parameter names.
* @see javax.servlet.http.HttpServletRequest#getParameterNames()
* @since 3.0
*/
Iterator<String> getParameterNames();
/**
* Return a immutable Map of the request parameters, with parameter names as map keys
* and parameter values as map values. The map values will be of type String array.
* <p>A single-value parameter will be exposed as an array with a single element.
* @see javax.servlet.http.HttpServletRequest#getParameterMap()
*/
Map<String, String[]> getParameterMap();
/**
* Return the primary Locale for this request.
* @see javax.servlet.http.HttpServletRequest#getLocale()
*/
Locale getLocale();
/**
* Return the context path for this request
* (usually the base path that the current web application is mapped to).
* @see javax.servlet.http.HttpServletRequest#getContextPath()
*/
String getContextPath();
/**
* Return the remote user for this request, if any.
* @see javax.servlet.http.HttpServletRequest#getRemoteUser()
*/
String getRemoteUser();
/**
* Return the user principal for this request, if any.
* @see javax.servlet.http.HttpServletRequest#getUserPrincipal()
*/
Principal getUserPrincipal();
/**
* Determine whether the user is in the given role for this request.
* @see javax.servlet.http.HttpServletRequest#isUserInRole(String)
*/
boolean isUserInRole(String role);
/**
* Return whether this request has been sent over a secure transport
* mechanism (such as SSL).
* @see javax.servlet.http.HttpServletRequest#isSecure()
*/
boolean isSecure();
/**
* Check whether the requested resource has been modified given the
* supplied last-modified timestamp (as determined by the application).
* <p>This will also transparently set the "Last-Modified" response header
* and HTTP status when applicable.
* <p>Typical usage:
* <pre class="code">
* public String myHandleMethod(WebRequest webRequest, Model model) {
* long lastModified = // application-specific calculation
* if (request.checkNotModified(lastModified)) {
* // shortcut exit - no further processing necessary
* return null;
* }
* // further request processing, actually building content
* model.addAttribute(...);
* return "myViewName";
* }</pre>
* <p>This method works with conditional GET/HEAD requests, but
* also with conditional POST/PUT/DELETE requests.
* <p><strong>Note:</strong> you can use either
* this {@code #checkNotModified(long)} method; or
* {@link #checkNotModified(String)}. If you want enforce both
* a strong entity tag and a Last-Modified value,
* as recommended by the HTTP specification,
* then you should use {@link #checkNotModified(String, long)}.
* <p>If the "If-Modified-Since" header is set but cannot be parsed
* to a date value, this method will ignore the header and proceed
* with setting the last-modified timestamp on the response.
* @param lastModifiedTimestamp the last-modified timestamp in
* milliseconds that the application determined for the underlying
* resource
* @return whether the request qualifies as not modified,
* allowing to abort request processing and relying on the response
* telling the client that the content has not been modified
*/
boolean checkNotModified(long lastModifiedTimestamp);
/**
* Check whether the requested resource has been modified given the
* supplied {@code ETag} (entity tag), as determined by the application.
* <p>This will also transparently set the "ETag" response header
* and HTTP status when applicable.
* <p>Typical usage:
* <pre class="code">
* public String myHandleMethod(WebRequest webRequest, Model model) {
* String eTag = // application-specific calculation
* if (request.checkNotModified(eTag)) {
* // shortcut exit - no further processing necessary
* return null;
* }
* // further request processing, actually building content
* model.addAttribute(...);
* return "myViewName";
* }</pre>
* <p><strong>Note:</strong> you can use either
* this {@code #checkNotModified(String)} method; or
* {@link #checkNotModified(long)}. If you want enforce both
* a strong entity tag and a Last-Modified value,
* as recommended by the HTTP specification,
* then you should use {@link #checkNotModified(String, long)}.
* @param etag the entity tag that the application determined
* for the underlying resource. This parameter will be padded
* with quotes (") if necessary.
* @return true if the request does not require further processing.
*/
boolean checkNotModified(String etag);
/**
* Check whether the requested resource has been modified given the
* supplied {@code ETag} (entity tag) and last-modified timestamp,
* as determined by the application.
* <p>This will also transparently set the "ETag" and "Last-Modified"
* response headers, and HTTP status when applicable.
* <p>Typical usage:
* <pre class="code">
* public String myHandleMethod(WebRequest webRequest, Model model) {
* String eTag = // application-specific calculation
* long lastModified = // application-specific calculation
* if (request.checkNotModified(eTag, lastModified)) {
* // shortcut exit - no further processing necessary
* return null;
* }
* // further request processing, actually building content
* model.addAttribute(...);
* return "myViewName";
* }</pre>
* <p>This method works with conditional GET/HEAD requests, but
* also with conditional POST/PUT/DELETE requests.
* <p><strong>Note:</strong> The HTTP specification recommends
* setting both ETag and Last-Modified values, but you can also
* use {@code #checkNotModified(String)} or
* {@link #checkNotModified(long)}.
* @param etag the entity tag that the application determined
* for the underlying resource. This parameter will be padded
* with quotes (") if necessary.
* @param lastModifiedTimestamp the last-modified timestamp in
* milliseconds that the application determined for the underlying
* resource
* @return true if the request does not require further processing.
* @since 4.2
*/
boolean checkNotModified(String etag, long lastModifiedTimestamp);
/**
* Get a short description of this request,
* typically containing request URI and session id.
* @param includeClientInfo whether to include client-specific
* information such as session id and user name
* @return the requested description as String
*/
String getDescription(boolean includeClientInfo);
}