在Spring MVC中,我可以这样做以获取查询字符串上的项值:
public void sendMessage(HttpServletResponse response,
@RequestParam("Session Id") String sessionId,
但是如何将完整的查询字符串作为一个长字符串?即我不想要它的个别参数,我想要整个事情吗?
非常感谢!
答案 0 :(得分:10)
将HttpServletRequest
作为参数添加到方法中,并从请求中获取查询字符串:
public void sendMessage(HttpServletRequest request,
HttpServletResponse response {
String queryString = request.getQueryString();
}
答案 1 :(得分:3)
你需要这样做:
public void sendMessage(HttpServletResponse response,
@RequestParam("Session Id") String sessionId, HttpServletRequest request,..
{
String qString= request.getQueryString();
答案 2 :(得分:3)
如果您不想在控制器中使用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) {
...
}
}
答案 3 :(得分:0)
Controller本身知道整个查询字符串的内容。
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception
然后,您可以从以下位置获取完整的查询字符串:
request.getQueryString();