0

很抱歉问我在许多答案中看到的那个错误,但我无法解决我的错误,所以请帮助我。我的要求是根据id no我使用注释在我的控制器中获取参数来获取员工,但我不知道为什么会出现这个错误

    SEVERE: Servlet.service() for servlet [jsp] in context with path [/EmployeeWebSpring] threw exception [java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'empDetails' available as request attribute] with root cause
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'empDetails' available as request attribute
    at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:141)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:178)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:198)
    at org.springframework.web.servlet.tags.form.LabelTag.autogenerateFor(LabelTag.java:129)
    at org.springframework.web.servlet.tags.form.LabelTag.resolveFor(LabelTag.java:119)
    at org.springframework.web.servlet.tags.form.LabelTag.writeTagContent(LabelTag.java:89)
    at org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:102)
    at org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:79)
    at org.apache.jsp.Search_jsp._jspx_meth_form_005flabel_005f0(Search_jsp.java:229)
    at org.apache.jsp.Search_jsp._jspx_meth_form_005fform_005f0(Search_jsp.java:157)
    at org.apache.jsp.Search_jsp._jspService(Search_jsp.java:95)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:307)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
3 Jan, 2013 2:23:33 PM org.apache.jasper.compiler.TldLocationsCache tldScanJar
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
3 Jan, 2013 2:23:33 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [jsp] in context with path [/EmployeeWebSpring] threw exception [An exception occurred processing JSP page /Search.jsp at line 26

23: 
24: 
25:             <tr>
26:                 <td><form:label path="employeeNo">Employee_ID</form:label> <form:input
27:                         path="employeeNo" /></td>
28:                 <td><input type="submit" name="method" value="FindById" /></td>
29:             </tr>


Stacktrace:] with root cause

这是我的控制器

包 com.nousinfo.tutorial.controllers;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.dao.EmptyResultDataAccessException;
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.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;


import com.nousinfo.tutorial.service.impl.EmployeeServiceImpl;
import com.nousinfo.tutorial.service.model.EmployeeBO;
@Controller

public class SearchEmployeeController  {
    private EmployeeServiceImpl employeeServiceImpl;

    public void setEmployeeServiceImpl(EmployeeServiceImpl employeeServiceImpl) {
        this.employeeServiceImpl = employeeServiceImpl;
    }
    @ModelAttribute("empDetails")
    @RequestMapping(value="/search.spring" ,method=RequestMethod.POST)
    public ModelAndView searchByEmpNo(@RequestParam("employeeNo") long employeeNo) throws Exception {
        EmployeeBO empDetails = null;
        try {
            if (employeeServiceImpl.getEmployee(employeeNo) != null) {
                empDetails = employeeServiceImpl.getEmployee(employeeNo);
            }
        } catch (EmptyResultDataAccessException e) {
            return new ModelAndView("successSingleEmp", "empDetails",
                    empDetails);
        }
        return new ModelAndView("successSingleEmp", "empDetails", empDetails);
    }
}

这是我的jsp文件

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>

<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<fmt:setBundle basename="ApplicationResources" />
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Employee Search Page</title>
</head>
<body>
<form:form action="/search.spring" method="post" modelAttribute="empDetails">
        <table border="0">



            <tr>
                <td><form:label path="employeeNo">Employee_ID</form:label> <form:input
                        path="employeeNo" /></td>
                <td><input type="submit" name="method" value="FindById" /></td>
            </tr>
            <tr>
                <td><form:label path="empName">Employee_Name</form:label>
                <form:input path="empName"/></td>
                <td><input type="submit" name="method" value="FindByName" /></td>
                </tr>


            <tr>
                <td><form:label path="deptNo">Employee_Name</form:label>
                <form:input path="deptNo"/></td>
                <td><input type="submit" name="method" value="FindByDeptNO" /></td>
                </tr>
            <tr>
                <td colspan="2" align="center"><font size=3>For
                        Searching the employees by<b>Employee Name</b><br />you can use %
                        match all the records with the given pattern
                </font><br /> <font size="2"> <i>e.g <b> for search by</b>EmployeeName<br />
                            matches alL the employees whose name starts with character <b>S</b></i></font></td>
            </tr>
        </table>
    </form:form>
</body>
</html>

这是我的 webConfig-context.xml

  <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
                           http://www.springframework.org/schema/context 
                           http://www.springframework.org/schema/context/spring-context-3.2.xsd
                           http://www.springframework.org/schema/mvc
                           http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

<context:annotation-config />
  <context:component-scan base-package="com.nousinfo.tutorial." />
   <mvc:annotation-driven />


<bean id="viewResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver"
      p:prefix="/WEB-INF/"
      p:suffix=".jsp" />

    <!-- configuring BeanNameViewResolver -->
    <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"></bean>

    <!-- configuring exception Resolver -->
    <bean
        class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
        <property name="exceptionMappings">
            <props>
                <prop key="org.springframework.dao.DAOException">dberrorView</prop>
                <prop key="java.lang.NumberFormatException">notANumber</prop>
            </props>
        </property>
    </bean>
    <!-- configuring views required to render the view for /search.spring -->
    <bean name="successSingleEmp" class="org.springframework.web.servlet.view.JstlView">
        <property name="url" value="/WEB-INF/EmployeeDetail.jsp" />
    </bean>
    <bean name="successMultipleEmps" class="org.springframework.web.servlet.view.JstlView">
        <property name="url" value="/WEB-INF/EmployeeList.jsp" />
    </bean>
    <bean name="dberrorView" class="org.springframework.web.servlet.view.JstlView">
        <property name="url" value="/WEB-INF/DBError.jsp" />
    </bean>
    <bean name="notANumber" class="org.springframework.web.servlet.view.JstlView">
        <property name="url" value="/WEB-INF/NotANumberError.jsp" />
    </bean>
    <!-- CONFIGURING THE MEASSAGE RESOURCES FOR THIS APPLICTION CONTEXT -->
    <bean id="messageSource"
        class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename" value="/com/nousinfo/resources/ApplicationResources" />
    </bean>
</beans>

这是 web.xml

 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>EmployeeWebSpring</display-name>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/webConfig-context.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- The front controller of this Spring Web application, responsible for 
        handling all application requests -->
    <servlet>
        <servlet-name>ds</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/services-context.xml, /WEB-INF/webConfig-context.xml</param-value>
        </init-param>

        <load-on-startup>1</load-on-startup>
    </servlet>
    <!-- Map all requests to the DispatcherServlet for handling -->
    <servlet-mapping>
        <servlet-name>ds</servlet-name>
        <url-pattern>*.spring</url-pattern>
    </servlet-mapping>
</web-app>
4

1 回答 1

0

我的猜测是您没有将 empDetails 添加到初始视图中

错误告诉你

@ModelAttribute("empDetails")

不作为 POST 请求的一部分出现。如果您始终将其设置为 null,为什么这是该方法的模型属性?

于 2013-01-03T09:13:08.013 回答