Spring MVC既不是BindingResult也不是普通的目标 [英] Spring MVC Neither BindingResult nor plain target

查看:97
本文介绍了Spring MVC既不是BindingResult也不是普通的目标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

java.lang.IllegalStateException:BindingResult和bean名称'user'的普通目标对象都不可用作请求属性
所以当我试图运行这个网页时我得到了这个错误,我不知道知道为什么。
我看到并阅读了很多关于这个错误的内容,但我无法找出问题所在。

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'user' available as request attribute So i got this error when i'm trying to run this web page, and i don't know why. I saw and read a lot about this error but i can't find out what is the problem.

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" 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>onallo_labor</display-name>
  <welcome-file-list>
<welcome-file>welcome.jsp</welcome-file>
  </welcome-file-list>

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

<servlet-mapping>
    <servlet-name>servlet_1</servlet-name>
    <url-pattern>*.html</url-pattern>
</servlet-mapping>

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

      <listener>
       <listener-class>
          org.springframework.web.context.ContextLoaderListener
       </listener-class>
  </listener> 

</web-app>

servlet_1-servlet.xml:

servlet_1-servlet.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"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="org.springframework.samples.petclinic.web"/>

</beans>

CreateController:

CreateController:

package onallo_labor;

package onallo_labor;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.portlet.ModelAndView;

@Controller
public class CreateController {
    @RequestMapping(value="create",method=RequestMethod.GET)
    public ModelAndView create(){
        return new ModelAndView("create","user",new User());
}
@RequestMapping(value="",method=RequestMethod.GET)
public ModelAndView welcome(){
    return new ModelAndView("welcome");
}
@RequestMapping(value="create_ready",method=RequestMethod.POST)
public String addContact(@ModelAttribute("user")
        User user,Model model) {

    model.addAttribute("username",user.getUsername());
    model.addAttribute("birthday",user.getBirthday());

    return "create_ready";
}
}

create.jsp:

create.jsp:

<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>User Account Creation</title>
</head>
<body>

<div align="center">
<form:form method="POST" modelAttribute="user">
    <table border="0">
        <tr>
            <td colspan="2" align="center"><h2>Registration Form</h2></td>
        </tr>
        <tr>
            <td>Username: </td>
            <td><form:input path="username" id="djb"/></td>
        </tr>
        <tr>
            <td>Password: </td>
            <td><form:password path="password"/></td>
        </tr>
        <tr>
            <td>Birthday: </td>
            <td><form:input path="birthday"/></td>
        </tr>
        <tr>
                <td colspan="2" align="center"><input type="submit" value="Submit" /></td>
        </tr>
    </table>
</form:form>
</div>




</body>
</html>

create_ready.jsp:

The create_ready.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <h2>Submitted User Information</h2>
   <table>
<tr>
    <td>Name</td>
    <td>${username}</td>
</tr>
<tr>
    <td>Birthday</td>
    <td>${birthday}</td>
</tr>
</table> 
</body>
</html>

用户类:

package onallo_labor;

public class User {
public User(){}
public String   username;
public String   password;
public String   birthday;
public String getUsername(){return this.username;}
public void setUsername(String un){this.username=un;}
public String getPassword(){return this.password;}
public void setPassword(String pw){this.password=pw;}
public String getBirthday(){return this.birthday;}
public void setBirthday(String bd){this.birthday=bd;}
}

有一张关于文件结构的图片。

There is a pic about the structure of the files.

http://kepkezelo.com/images/ lm418k9xegg2aukio3.png

UPDATE1:
所以我更改了User类中的方法名称,现在这里是错误:

UPDATE1: So i changed the method name's in the User class, now here is the error:

org.apache.jasper.JasperException: An exception occurred processing JSP page /create.jsp at line 20

17:             </tr>
18:             <tr>
19:                 <td>Username: </td>
20:                 <td><form:input path="username" id="djb"/></td>
21:             </tr>
22:             <tr>
23:                 <td>Password: </td>


Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:510)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:413)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)


root cause 

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'user' available as request attribute
org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:144)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:168)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:188)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getName(AbstractDataBoundFormElementTag.java:154)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.writeDefaultAttributes(AbstractDataBoundFormElementTag.java:117)
org.springframework.web.servlet.tags.form.AbstractHtmlElementTag.writeDefaultAttributes(AbstractHtmlElementTag.java:422)
org.springframework.web.servlet.tags.form.InputTag.writeTagContent(InputTag.java:142)
org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:84)
org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:80)
org.apache.jsp.create_jsp._jspx_meth_form_005finput_005f0(create_jsp.java:181)
org.apache.jsp.create_jsp._jspx_meth_form_005fform_005f0(create_jsp.java:125)
org.apache.jsp.create_jsp._jspService(create_jsp.java:78)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

感谢您的回答。

推荐答案

问题是各种各样的事情。你有

The problem is a combination of things. You have

import org.springframework.web.portlet.ModelAndView;

@RequestMapping(value="create",method=RequestMethod.GET)
public ModelAndView create(){
    return new ModelAndView("create","user",new User());
}

ModelAndView 来自 portlet 库不是默认识别 DispatcherServlet 堆栈的返回类型。因此,它将被视为普通的 Object 并添加到模型本身。因为您的URI是 ... / create ,所以Spring将使用该最后一个路径元素映射到视图。因此,最终会将 create.jsp 作为视图解析,但原因错误。

The ModelAndView from the portlet library is not a default recognized return type for the DispatcherServlet stack. As such, it will be considered as a plain Object and added to the model itself. Because your URI is .../create, Spring will use that last path element to map to a view. So it will end up resolving your create.jsp as a view but for the wrong reasons.

ModelAndView 更改为

import org.springframework.web.servlet.ModelAndView;

我不知道为什么你有一个portlet依赖,但如果你有一个你不需要它。

你也不见了

<mvc:annotation-driven />

你're < servlet> 映射到

<servlet-mapping>
    <servlet-name>servlet_1</servlet-name>
    <url-pattern>*.html</url-pattern>
</servlet-mapping>

因此我不清楚您发送请求的URL是什么, ../ create.html

so it's unclear to me what URL you're sending your request to, ../create.html?

这篇关于Spring MVC既不是BindingResult也不是普通的目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆