应用程序类' org.springframework.web.servlet.support.BindStatus。抛出的异常。:141'

时间:2015-10-09 07:59:29

标签: java spring

我收到此错误,应用程序类抛出异常

org.springframework.web.servlet.support.BindStatus.:141 

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

at org.springframework.web.servlet.support.BindStatus.(BindStatus.java:141)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:174)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:194)
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 com.ibm._jsp._luhnCheck._jspx_meth_form_label_0(_luhnCheck.java:129)
at com.ibm._jsp._luhnCheck._jspx_meth_form_form_0(_luhnCheck.java:217)
at com.ibm._jsp._luhnCheck._jspService(_luhnCheck.java:98)
at com.ibm.ws.jsp.runtime.HttpJspBase.service(HttpJspBase.java:101)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:668)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1240)
at [internal classes]
at com.ibm.ws.jsp.webcontainerext.AbstractJSPExtensionServletWrapper.handleRequest(AbstractJSPExtensionServletWrapper.java:215)
at com.ibm.ws.webcontainer.filter.WebAppFilterManager.invokeFilters(WebAppFilterManager.java:1033)
at [internal classes]

请帮忙

代码放在下面:

@Controller

public class LuhnController {


private ILuhnCheckService luhnCheckService;

public void setLuhnCheckService(ILuhnCheckService luhnCheckService){
this.luhnCheckService = luhnCheckService;
}

@ModelAttribute("luhnFormBean")
@RequestMapping(value = "/validateLuhn",method = RequestMethod.POST)
public String validateLuhn(@ModelAttribute("luhnFormBean") LuhnFormBean luhnFormBean,Model model){
//  LuhnFormBean luhnFormBean= new LuhnFormBean();;
    System.out.println(luhnCheckService.validateLuhn(luhnFormBean.getCcNumber()));
     return "luhnFormBean";
}

@ModelAttribute("luhnFormBean")
@RequestMapping(value = "/validateLuhn",method = RequestMethod.GET)
public ModelAndView validateLuhn() {
    return new ModelAndView("/luhnCheck", "luhnFormBean", new LuhnFormBean());
 }

}

的web.xml

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

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/Example-servlet.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
      <servlet-name>Example</servlet-name>
      <servlet-class>
         org.springframework.web.servlet.DispatcherServlet
      </servlet-class>
      <load-on-startup>1</load-on-startup>
   </servlet>

   <servlet-mapping>
      <servlet-name>Example</servlet-name>
      <url-pattern>/web/*</url-pattern>
   </servlet-mapping>

    <welcome-file-list>
        <welcome-file>/jsp/home.jsp</welcome-file>
    </welcome-file-list>


</web-app>

实施例-servlet.xml中

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

    <context:component-scan base-package="example" />
      <mvc:annotation-driven />

   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/jsp/" />
      <property name="suffix" value=".jsp" />
   </bean>



</beans>

JSP

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Luhn Validation</title>
</head>
<body>
    <div align="center">
        <form:form method="" action="validate" modelAttribute="luhnFormBean">
            <table border="0">
                        <tr>
                <td><form:label path="ccNumber"><b> Card Number</b></form:label></td>
                <td><form:input path="ccNumber" /></td>
            </tr>

            </table>
            <input type="submit" value="Submit" />
        </form:form>
    </div>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

你的Spring控制器出了很多问题。

首先,您在方法级别指定了title。在方法级别,注释的目的是为控制器中的所有@ModelAttribute方法共享表单bean的构造逻辑。注释的正确返回类型的方法是表单类型@RequestMapping。不是这里的情况。

其次,LuhnFormBean bean的返回类型是视图名称(@RequestMapping)或模型和视图(String)。为什么ModelAndView方法返回String validateLuhn?你有这样的观点吗?

第三,对于相同的(!)方法"luhnFormBean",您在方法级别和参数级别定义了validateLuhn。参数的注释是指定将绑定到表单参数的表单bean,该方法的注释是告诉Spring如何创建表单bean。同样的方法不能两者兼顾。这没有意义!