在Spring MVC中,我可以通过以下方式获取查询字符串上items的值:
public void sendMessage(HttpServletResponse response,
@RequestParam("Session Id") String sessionId,
但是我如何将整个查询字符串作为一个长字符串获取呢?即我不想从中获取各个参数,我需要整个查询字符串。
非常感谢!
在Spring MVC中,我可以通过以下方式获取查询字符串上items的值:
public void sendMessage(HttpServletResponse response,
@RequestParam("Session Id") String sessionId,
但是我如何将整个查询字符串作为一个长字符串获取呢?即我不想从中获取各个参数,我需要整个查询字符串。
非常感谢!
将HttpServletRequest
作为方法的参数添加,并从请求中获取查询字符串:
public void sendMessage(HttpServletRequest request,
HttpServletResponse response {
String queryString = request.getQueryString();
}
如果您不想在控制器中使用HttpServletRequest,可以创建HandlerMethodArgumentResolver来解析查询字符串。
例如:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
public @interface QueryString {
}
public class QueryStringResolver implements HandlerMethodArgumentResolver {
@Override
public boolean supportsParameter(MethodParameter parameter) {
Annotation[] parameterAnnotations = parameter.getParameterAnnotations();
for (Annotation parameterAnnotation : parameterAnnotations) {
if (QueryString.class.isInstance(parameterAnnotation)) {
return true;
}
}
return false;
}
@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class);
return request.getQueryString();
}
}
<mvc:annotation-driven>
<mvc:argument-resolvers>
<bean class="mypackage.QueryStringResolver"/>
</mvc:argument-resolvers>
</mvc:annotation-driven>
public class MyController {
@RequestMapping(...)
public String someMethod(@QueryString String queryString) {
...
}
}
public void sendMessage(HttpServletResponse response,
@RequestParam("Session Id") String sessionId, HttpServletRequest request,..
{
String qString= request.getQueryString();
控制器本身知道整个查询字符串的内容。
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception
然后,您可以从中获取完整的查询字符串:
request.getQueryString();