/** * Get an array of int parameters, return an empty array if not found. * @param request current HTTP request * @param name the name of the parameter with multiple possible values */ public static int[] getIntParameters(ServletRequest request, String name) { try { return getRequiredIntParameters(request, name); } catch (ServletRequestBindingException ex) { return new int[0]; } }
/** * Get an array of int parameters, return an empty array if not found. * @param request current HTTP request * @param name the name of the parameter with multiple possible values */ public static int[] getIntParameters(ServletRequest request, String name) { try { return getRequiredIntParameters(request, name); } catch (ServletRequestBindingException ex) { return new int[0]; } }
@Test public void testIntParameters() throws ServletRequestBindingException { MockHttpServletRequest request = new MockHttpServletRequest(); request.addParameter("param", new String[] {"1", "2", "3"}); request.addParameter("param2", "1"); request.addParameter("param2", "2"); request.addParameter("param2", "bogus"); int[] array = new int[] {1, 2, 3}; int[] values = ServletRequestUtils.getRequiredIntParameters(request, "param"); assertEquals(3, values.length); for (int i = 0; i < array.length; i++) { assertEquals(array[i], values[i]); } try { ServletRequestUtils.getRequiredIntParameters(request, "param2"); fail("Should have thrown ServletRequestBindingException"); } catch (ServletRequestBindingException ex) { // expected } }
/** * Get an array of int parameters, throwing an exception if not found or one is not a number.. * @param request current HTTP request * @param name the name of the parameter with multiple possible values * @throws ServletRequestBindingException a subclass of ServletException, * so it doesn't need to be caught */ public static int[] getRequiredIntParameters(ServletRequest request, String name) throws ServletRequestBindingException { return org.springframework.web.bind.ServletRequestUtils.getRequiredIntParameters(request, name); }
/** * Get an array of int parameters, return an empty array if not found. * @param request current HTTP request * @param name the name of the parameter with multiple possible values */ public static int[] getIntParameters(ServletRequest request, String name) { try { return getRequiredIntParameters(request, name); } catch (ServletRequestBindingException ex) { return new int[0]; } }
/** * Get an array of int parameters, return an empty array if not found. * @param request current HTTP request * @param name the name of the parameter with multiple possible values */ public static int[] getIntParameters(ServletRequest request, String name) { try { return getRequiredIntParameters(request, name); } catch (ServletRequestBindingException ex) { return new int[0]; } }