SpringMVC
eclipse
在之前的程序里面使用了String进行了参数的传递处理,那么除了使用String之外也可以使用其他的各种类型。接收int类型: @RequestMapping('remove') public ModelAndView remove(int id) { ModelAndView md = new ModelAndView('/pages/show.jsp'); logger.info(id * 2 + '--------'); return null; }
如果此时传递的内容不是一个数字,那么就会出现如下的错误信息:Failed to bind request element: org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'int'; nested exception is java.lang.NumberFormatException: For input string: 'llll'
实际上在SpringMVC里面对于参数的接收也都是按照字符串接收,而后帮助用户自动进行转型控制。在进行分页处理的时候往往需要手工的判断:cp,ls,col,kw等参数的内容,如果该参数接收的内容是null,怎必须进行判断才可以转型,但是如果有了这样的机制,对于整个分页的处理就变得异常简单。@RequestMapping('list') public ModelAndView list(@RequestParam(value='cp',defaultValue='1') int currentPage, @RequestParam(value='ls',defaultValue='10') int lineSize, @RequestParam(value='col',defaultValue='ename') String colum, @RequestParam(value='kw',defaultValue='') String keyword) { ModelAndView md = new ModelAndView(); System.out.println('currentPage=' + currentPage); System.out.println('lineSize=' + lineSize); System.out.println('colum=' + colum); System.out.println('keyw ord=' + keyword); return md; }
这样处理之后当方法参数没有赋值时将去我们程序设置的默认值。
内置对象配置。在正常编写servlet程序的时候doGet()或者是doPost()方法上都会提供有HttpServletRequest,HttpServletResponse参数,这个时候可以根据用户的需求自己随意来设置request,response内容。@RequestMapping('get') public ModelAndView doGet(int id,HttpServletRequest request, HttpServletResponse response) { ModelAndView md = new ModelAndView(); return md; }
需要注意的时候,这个参数随意自己来进行安排,顺序也没有固定限制,所有的参数可以有用户自己发挥。