Mryqu's Notes


  • 首页

  • 搜索
close

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

时间: 2014-02-04   |   分类: Service+JavaEE   Spring     |   阅读: 100 字 ~1分钟

最近玩一下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 时发生如下问题:处理注解@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);            
        }
    }
}

标题:处理注解@RequestParam的"Required String parameter is not present"
作者:mryqu
声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 3.0 CN 许可协议。转载请注明出处!

#springmvc# #requestparam# #required属性#
MinGW安装和使用
免费私有代码托管平台
  • 文章目录
  • 站点概览

Programmer & Architect

662 日志
27 分类
1472 标签
GitHub Twitter FB Page
© 2009 - 2023 Mryqu's Notes
Powered by - Hugo v0.120.4
Theme by - NexT
0%