加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

处理注解@RequestParam的"Required String parameter is not present"

(2014-02-04 10:04:20)
标签:

springmvc

requestparam

required属性

分类: Web.JavaEE
最近玩一下SpringMVC,代码如下:

@Controller
@RequestMapping("/test.do")
public class TestController {
    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView sync(Model m, 
            @RequestParam("fid") String fid,
            @RequestParam("sid") String sid) throws Exception {        
        if(fid==null || sid==null) {
            m.addAttribute("file", new FileMetaDAO());
            return new ModelAndView(ViewProvider.UPLOAD);
        } else {
            m.addAttribute("sync", new SyncDAO());
            return new ModelAndView(ViewProvider.HR_SYNC);            
        }
    }
}

访问http://localhost:8080/hellorest/test.do时发生如下问题:
http://s13/mw690/0026uWfMgy6MJkj4sBC5c&690String parameter is not present"" TITLE="处理注解@RequestParam的"Required String parameter is not present"" />
查了一下Annotation Type RequestParam的javadoc,加上可选参数required解决战斗。

@Controller
@RequestMapping("/test.do")
public class TestController {
    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView sync(Model m, 
            @RequestParam(value="fid", required = false) String fid,
            @RequestParam(value="sid", required = false) String sid) 
    throws Exception {        
        if(fid==null || sid==null) {
            m.addAttribute("file", new FileMetaDAO());
            return new ModelAndView(ViewProvider.UPLOAD);
        } else {
            m.addAttribute("sync", new SyncDAO());
            return new ModelAndView(ViewProvider.HR_SYNC);            
        }
    }
}

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有