原文地址:http://hi.baidu.com/xia_mingjian/item/07ff040c1826a004addc70b5
方法一
当你开发一个项目,如果你选择的是spring MVC 框架,而你在前台使用spring的标签时,那么你有可能出现在这个情况。
javax.servlet.jsp.JspTagException: Neither BindingResult nor plain target object for bean name 'command' available as request attribute
方法:1为抛出异常原因,2为异常解决方法。
1. 原因: 进入spring:bind标签源码你可以看到
Object target = requestContext.getModelObject(beanName);
if (target == null) {
throw new IllegalStateException("Neither BindingResult nor plain target object for bean name '" +
beanName + "' available as request attribute");
}
beanName= <spring:bind path="command.spjg">的绿色部分
如果你是直接对某个页面进行请求,那么request中还没command这个对象
2.
在页面上加上
<jsp:useBean id="command" class="com.ztenc.proj.bean.AFRFOAP" scope="request" ></jsp:useBean>
红色部分填上你的绑定类
-----------------------------------------------------------------------
方法二
下面是lkj同学教我的方法...
这个错误的原因是在进入表单页面的时候没有对应的Vo对象,所以在controller中进入get方法的时候传入一个Vo对象即可
如下:
@RequestMapping(value = CUSTOM_ADD,method=RequestMethod.GET)
@BusinessDesc(ModuleDesc = MODULE_DES, MethodDesc = "增加客户的表单")
public String add(@ModelAttribute("customInfoVo") CustomInfoVo customInfoVo) {
return AutoGetURL();
}
@RequestMapping(value = CUSTOM_ADD,method=RequestMethod.POST)
@BusinessDesc(ModuleDesc = MODULE_DES, MethodDesc = "增加客户")
public ModelAndView add(HttpServletRequest request, @ModelAttribute("customInfoVo") @Valid CustomInfoVo customInfoVo, BindingResult result)
throws ServiceException {
try {
request.getSession().removeAttribute("errors");
if (result.hasErrors()) {
//后台验证不通过
return new ModelAndView(AutoGetURL());
}
int key = iCustomService.save(customInfoVo);
if (key < 0) {
throw new ServiceException("插入数据失败");
}
} catch (Exception e) {
e.printStackTrace();
throw new ServiceException(e);
}
return new ModelAndView(new RedirectView(CUSTOM_SELECTBYPAGE));
}