[springMVC]javax.servlet.jsp.JspTagException: Neither BindingResult nor plain target object for bean

本文介绍了解决Spring MVC框架中使用bind标签时出现的异常问题,提供了两种方法:一是通过jsp:useBean标签显式创建请求域中的对象;二是通过Controller层预先传递VO对象。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文地址: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));
    }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值