java – BindingResult和bean名称的普通目标对象都不可用作请求属性 – Spring MVC

前端之家收集整理的这篇文章主要介绍了java – BindingResult和bean名称的普通目标对象都不可用作请求属性 – Spring MVC前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

参见英文答案 > What causes “java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name ‘command’ available as request attribute”?                                    5个
我收到了这个错误

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'command' available as request attribute

这是堆栈跟踪

org.apache.jasper.JasperException: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name ‘command’ available as request attribute
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:534)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:452)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

root cause

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name ‘command’ available as request attribute
org.springframework.web.servlet.support.BindStatus.(BindStatus.java:141)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:178)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:198)
org.springframework.web.servlet.tags.form.LabelTag.autogenerateFor(LabelTag.java:129)
org.springframework.web.servlet.tags.form.LabelTag.resolveFor(LabelTag.java:119)
org.springframework.web.servlet.tags.form.LabelTag.writeTagContent(LabelTag.java:89)
org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:102)
org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:79)
org.apache.jsp.student_jsp._jspx_meth_form_005flabel_005f0(student_jsp.java:182)
org.apache.jsp.student_jsp._jspx_meth_form_005fform_005f0(student_jsp.java:117)
org.apache.jsp.student_jsp._jspService(student_jsp.java:79)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

这是我的web.xml文件

这是我的applicationContext.xml文件

这是我的student.jsp文件

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

这是我的StudentController.java文件

package com.Prime;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.ui.ModelMap;

@Controller
public class StudentController {

   @RequestMapping(value = "/student",method = RequestMethod.GET)
   public ModelAndView student() {
      return new ModelAndView("/student.jsp","SpringWeb",new Student());
   }

   @RequestMapping(value = "/addStudent",method = RequestMethod.POST)
   public String addStudent(@modelattribute("SpringWeb")Student student,ModelMap model) {
      model.addAttribute("name",student.getName());
      model.addAttribute("age",student.getAge());
      model.addAttribute("id",student.getId());

      return "result";
   }
}

任何人都可以帮我解决这个错误….提前谢谢

最佳答案
我尝试了你的配置,但我没有得到你的错误.
但我不得不改变一些事情.

viewResolver正在将.jsp添加到您的视图名称中.
恕我直言,你应该改变

 return new ModelAndView("/student.jsp",new Student());

 return new ModelAndView("student",new Student());

所以在学生之前删除jsp和“/”.

然后它对我有用.但正如我所说,我没有得到你的错误

原文链接:https://www.f2er.com/spring/432746.html

猜你在找的Spring相关文章