如何通过HttpServletRequest读取控制器内的数值数组值

时间:2013-09-26 09:57:24

标签: java spring jsp servlets

这是我的代码 -

@controller
public ModelAndView ServiceContract(HttpServletRequest httpServletRequest,
            HttpServletResponse httpServletResponse) {      
        int sortId = Integer.parseInt(httpServletRequest.getParameter("sortId"));
                ModelMap map = new ModelMap();
        map.addAttribute("jsonString",jsonStringX); 
        return new ModelAndView(jsonView,map);
    }

在我的jsp文件中,我们正在使用这种控制器方法.-

var sortId= [1,2, 3, 4 , 5];

如何在控制器中获取它的值?

1 个答案:

答案 0 :(得分:0)

String sortId = httpServletRequest.getParameter("sortId");
int sortIds[] = null;
if(sortId !=null){
       String array[] = sortId.split(",");
       sortIds = new int[array.length];
       for(int i=0;i<array.length;i++){
           try{
            sortIds[i] = Integer.parseInt(array[i]); 
           }catch(Exception e){
           }
       }
}

现在sortIds数组有这些值[1,2,3,4,5]