我收到异常 绑定结果和 bean 名称的普通目标对象'studentRegistration'在春季都不能用作请求属性



我是春天的新手。 我正在创建一个项目,其中注册是像任何其他项目一样的基本要求。但是我得到了下面给出的异常

Neither BindingResult nor plain target object for bean name 'studentRegistration' available as request attribute
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'studentRegistration' available as request attribute
    at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:144)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:168)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:188)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getName(AbstractDataBoundFormElementTag.java:154)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.autogenerateId(AbstractDataBoundFormElementTag.java:141)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.resolveId(AbstractDataBoundFormElementTag.java:132)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.writeDefaultAttributes(AbstractDataBoundFormElementTag.java:116)
    at org.springframework.web.servlet.tags.form.AbstractHtmlElementTag.writeDefaultAttributes(AbstractHtmlElementTag.java:422)
    at org.springframework.web.servlet.tags.form.InputTag.writeTagContent(InputTag.java:142)
    at org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:84)
    at org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:80)
    at org.apache.jsp.studentRegistration_jsp._jspx_meth_form_005finput_005f0(studentRegistration_jsp.java:565)
    at org.apache.jsp.studentRegistration_jsp._jspx_meth_form_005fform_005f0(studentRegistration_jsp.java:419)
    at org.apache.jsp.studentRegistration_jsp._jspService(studentRegistration_jsp.java:189)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:521)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1096)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:674)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)
Jul 12, 2016 5:07:58 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [jsp] in context with path [/projectMonitoring] threw exception [java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'studentRegistration' available as request attribute] with root cause
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'studentRegistration' available as request attribute
    at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:144)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:168)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:188)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getName(AbstractDataBoundFormElementTag.java:154)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.autogenerateId(AbstractDataBoundFormElementTag.java:141)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.resolveId(AbstractDataBoundFormElementTag.java:132)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.writeDefaultAttributes(AbstractDataBoundFormElementTag.java:116)
    at org.springframework.web.servlet.tags.form.AbstractHtmlElementTag.writeDefaultAttributes(AbstractHtmlElementTag.java:422)
    at org.springframework.web.servlet.tags.form.InputTag.writeTagContent(InputTag.java:142)
    at org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:84)
    at org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:80)
    at org.apache.jsp.studentRegistration_jsp._jspx_meth_form_005finput_005f0(studentRegistration_jsp.java:565)
    at org.apache.jsp.studentRegistration_jsp._jspx_meth_form_005fform_005f0(studentRegistration_jsp.java:419)
    at org.apache.jsp.studentRegistration_jsp._jspService(studentRegistration_jsp.java:189)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:521)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1096)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:674)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)

我知道这个例外很常见,很多人都回复了这个,但任何解决方案都对我不起作用。我还尝试运行以前创建的相同类别项目的".war"文件,但它们也显示相同的异常。最后,我在这里发布这个问题。这是我的模型

package edu.projectMonitoring.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="studentdetail")
public class Student {
    @Id
    @Column(name="emailId")
    String email;
    @Column(name="name")
    String name;
    @Column(name="Class")
    String clas;
    @Column(name="enrollementNo")
    String enrollment;
    @Column(name="password")
    String password;
    @Column(name="course")
    String course;
    @Column(name="contactNo")
    String contactNo;
    @Column(name="status")
    String status;

    public String getStatus() {
        return status;
    }
    public void setStatus(String status) {
        this.status = status;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getClas() {
        return clas;
    }
    public String getEnrollment() {
        return enrollment;
    }
    public void setEnrollment(String enrollment) {
        this.enrollment = enrollment;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getCourse() {
        return course;
    }
    public void setCourse() {
        this.course = course;
    }
    public String getContactNo() {
        return contactNo;
    }
    public void setContactNo(String contactNo) {
        this.contactNo = contactNo;
    }
    public void setClas(String clas) {
        this.clas = clas;
    }
    public void setCourse(String course) {
        this.course = course;
    }

}

这是我的控制器

package edu.projectMonitoring.controller;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

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 edu.projectMonitoring.model.Student;
@Controller
@RequestMapping(value = "/studentRegistration")
public class StudentRegistration {
    @RequestMapping(method = RequestMethod.GET)
    public String viewRegistration(Map<String, Object> model) {
    Student registerForm = new Student();
    model.put("studentRegistration", registerForm);
    List<String> courseList = new ArrayList<String>();
    courseList.add("Developer");
    courseList.add("Designer");
    courseList.add("IT Manager");
    model.put("courseList", courseList);
    return "studentRegistration";
    }
    @RequestMapping(method = RequestMethod.POST)
    public String processRegistration(@ModelAttribute("studentRegistration") Student registerForm,
    Map<String, Object> model) {
    // implement your own registration logic here...
    // for testing purpose:
    /*System.out.println("username: " + user.getUsername());
    System.out.println("password: " + user.getPassword());
    System.out.println("email: " + user.getEmail());
    System.out.println("birth date: " + user.getBirthDate());
    System.out.println("profession: " + user.getProfession());
*/  return "index";
    }
}

这是我的观点

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
    <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html >
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
    <%--  <link href="<c:url value="/resources/css/bootstrap.min.css" />" rel="stylesheet"> --%>
    <spring:url value="/resources/css/bootstrap.min.css" var="mainCss" /> 
    <link href="${mainCss}" rel="stylesheet" />
   <!--  <link href="https://fonts.googleapis.com/css?family=Lobster" rel="stylesheet"> -->
    <spring:url value="/resources/css/bootstrap-cerulean.min.css" var="cerulonCss" /> 
    <link href="${cerulonCss}" rel="stylesheet" />
    <spring:url value="/resources/css/charisma-app.css" var="charismaCss" /> 
    <link href="${charismaCss}" rel="stylesheet" />
    <spring:url value="/resources/css/animate.min.css" var="animateCss" /> 
    <link href="${animateCss}" rel="stylesheet" />
     <spring:url value="/resources/css/custom.css" var="customCss" /> 
    <link href="${customCss}" rel="stylesheet" />
</head>
<body>
<div>
<form:form action="studentRegistration" method="post" commandName="studentRegistration" class="form-horizontal">
  <div class="form-group">
    <label for="name" class="col-sm-2 control-label"><spring:message code="student.name"/></label>
    <div class="col-sm-10">
      <form:input path="name" class="form-control"  placeholder="Name"/>
    </div>
  </div>
  <div class="form-group">
    <label for="course" class="col-sm-2 control-label"><spring:message code="student.course"/></label>
    <div class="col-sm-10">
      <form:select path="course" items="${courseList}" />
    </div>
  </div>
  <div class="form-group">
    <label for="clas" class="col-sm-2 control-label"><spring:message code="student.class"/></label>
    <div class="col-sm-10">
      <form:select path="clas" items="${classList}" />
    </div>
  </div>
  <div class="form-group">
    <label for="enrollement" class="col-sm-2 control-label"><spring:message code="student.enrollement"/></label>
    <div class="col-sm-10">
      <form:input path="enrollement" class="form-control"  placeholder="Enrollement Number"/>
    </div>
  </div>
  <div class="form-group">
    <label for="contactNo" class="col-sm-2 control-label"><spring:message code="student.enrollement"/></label>
    <div class="col-sm-10">
      <form:input path="contactNo" class="form-control"  placeholder="Contact Number"/>
    </div>
  </div>
  <div class="form-group">
    <label for="email" class="col-sm-2 control-label"><spring:message code="student.enrollement"/></label>
    <div class="col-sm-10">
      <form:input path="email" class="form-control"  placeholder="Email"/>
    </div>
  </div>
  <div class="form-group">
    <label for="password" class="col-sm-2 control-label"><spring:message code="student.enrollement"/></label>
    <div class="col-sm-10">
      <form:password path="password" class="form-control"  placeholder="Password"/>
    </div>
  </div>
   <div class="form-group">
    <div class="col-sm-offset-2 col-sm-10">
      <button type="submit" class="btn btn-default">Sign in</button>
    </div>
  </div>

</form:form>
</div>
<!-- JavaScript Files Starts-->
<spring:url value="/resources/js/jquery-1.12.2.min.js" var="jqueryJs" />
<spring:url value="/resources/js/bootstrap.min.js" var="bootstrapJs" />
<spring:url value="/resources/js/jquery.cookie.js" var="cookieJs" />
<spring:url value="/resources/js/charisma.js" var="charJs" />
<script src="${jqueryJs}"></script>
<script src="${bootstrapJs}"></script>
<script src="${cookieJs}"></script>
<script src="${charJs}"></script>
<!-- JavaScript Files Ends-->
</body>
</html>

最后这是我的pom.xml文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.hari</groupId>
  <artifactId>springBasics</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>springBasics Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

   <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
   <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>4.3.0.RELEASE</version>
   </dependency>
   <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
   <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>4.3.0.RELEASE</version>
   </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
   <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>4.3.0.RELEASE</version>
   </dependency>
   <!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
   <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
   </dependency>
   <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-annotations -->
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-annotations</artifactId>
    <version>3.5.6-Final</version>
</dependency>
  <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-commons-annotations -->
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-commons-annotations</artifactId>
    <version>3.2.0.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.21</version>
</dependency>

  </dependencies>
  <build>
    <finalName>projectMonitoring</finalName>
  </build>
</project>

请帮助摆脱这种情况。提前谢谢。

尝试以下方法查看是否仍然遇到错误

我的假设:您正在使用带有html扩展名的浏览器上显示实际的jsp。

  1. 稍微修改您的学生注册.java控制器,例如

    @Controller
    public class StudentRegistration {
    @RequestMapping(value = "/studentRegistration", method = RequestMethod.GET)
    public String viewRegistration(Map<String, Object> model) {
        Student registerForm = new Student();
        model.put("studentRegistration", registerForm);
        List<String> courseList = new ArrayList<String>();
        courseList.add("Developer");
        courseList.add("Designer");
        courseList.add("IT Manager");
        model.put("courseList", courseList);
        return "studentRegistration";
    }
    @RequestMapping((value = "/index", method = RequestMethod.POST)
    public String processRegistration(@Valid Student registerForm, Map<String, Object> model, BindingResult result) {   
        if (result.hasErrors()) {
            return "error";
        } 
        else {
             //Do your student registeration process
            ...
            return "index";
        }
    }}
    
    1. 修改表单标记上的 JSP

<form:form action="index.html" method="post" commandName="studentRegistration" class="form-horizontal">

这里的问题是我正在请求页面http://localhost:8080/projectMonitoring/studentRegistration.jsp 以这种方式。也就是说,我正在请求学生注册.jsp但由控制器创建以处理映射名称"学生注册"

@RequestMapping(value = "/studentRegistration")

因此,它显示了异常。但是当我请求带有名称的页面时

http://localhost:8080/projectMonitoring/studentRegistration

然后它成功执行。

  1. 建议的做法是不要公开您的底层技术因此,当您有Jsp实现时,最好掩饰成像你帮助这样简单的东西.jsp作为帮助.html。您可以使用 Bean 配置实现此目的,例如

    '

    @Bean public ViewResolver viewResolver() { InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/views/"); resolver.setSuffix(".jsp"); resolver.setExposeContextBeansAsAttributes(true); return resolver; }

'

或 XML 配置

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix">
        <value>/WEB-INF/views/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean> 
  1. 例如,当您有带有以下链接的索引.jsp文件并单击它时,下面的控制器将拾取。

<a href="help.html" class="space"><spring:message code="top.reviews" />

  1. 控制器如下。注意:请求映射中的值"/help"应与帮助匹配.html不带扩展.html

    @RequestMapping(value = "/help", method = RequestMethod.GET) public String getHelpPage(Map model, Locale loc) {
    model.put("singleReport", new YourUserForm()); return "help"; }

相关内容

  • 没有找到相关文章

最新更新