示例URL:
../search/?attr1=value1&attr2=value2&attr4=value4
我不知道attr1、att2和attr4的名称。
我希望能够做这样的事情(或类似的,不关心,只要我可以访问请求参数名称->值的映射:
@RequestMapping(value = "/search/{parameters}", method = RequestMethod.GET)
public void search(HttpServletRequest request,
@PathVariable Map<String,String> allRequestParams, ModelMap model)
throws Exception {//TODO: implement}
我如何实现这与Spring MVC?
有两个接口
org.springframework.web.context.request.WebRequest
org.springframework.web.context.request.NativeWebRequest
允许通用请求参数访问以及请求/会话属性访问,而不需要绑定到本机Servlet/Portlet API。
Ex.:
@RequestMapping(value = "/", method = GET)
public List<T> getAll(WebRequest webRequest){
Map<String, String[]> params = webRequest.getParameterMap();
//...
}
附注:有一些文档是关于可以用作控制器参数的参数的。
下面是一个在Map中获取requestParams的简单例子:
@RequestMapping(value="submitForm.html", method=RequestMethod.POST)
public ModelAndView submitForm(@RequestParam Map<String, String> reqParam) {
String name = reqParam.get("studentName");
String email = reqParam.get("studentEmail");
ModelAndView model = new ModelAndView("AdmissionSuccess");
model.addObject("msg", "Details submitted by you::Name: " + name
+ ", Email: " + email );
}
在这种情况下,它将绑定值:
带name的studentName
studentEmail与email