【问题标题】:java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'userData' available as request attributejava.lang.IllegalStateException:Bean 名称“userData”的 BindingResult 和普通目标对象都不能用作请求属性
【发布时间】:2017-11-18 10:06:56
【问题描述】:

我是spring mvc的新手。我正在尝试使用 MySQL jdbc 在春季制作简单的杂物。对于数据映射,我使用了弹簧形式。每当我尝试使用弹簧形式时,它都会显示错误

Error is here

每当我删除弹簧形式并使用正常形式时,它看起来还可以,但在使用弹簧形式时会显示错误。错误说 java.lang.IllegalStateException: Bean name 'userData' 的 BindingResult 和普通目标对象都不能用作请求属性

header.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

    </head>
    <body>
        <nav class="navbar navbar-inverse">
            <div class="container-fluid">
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span> 
                    </button>
                    <a class="navbar-brand" href="#">SpringCRUDDemo</a>
                </div>
                <div class="collapse navbar-collapse" id="myNavbar">
                    <ul class="nav navbar-nav">
                        <li><a href="<c:url value='/'/>">Home</a></li>
                        <li><a href="<c:url value='/view/'/>">View</a></li>
                        <li><a href="#">Update</a></li> 
                        <li><a href="#">Delete</a></li> 
                    </ul>
                    <ul class="nav navbar-nav navbar-right">
                        <li><a href="#"><span class="glyphicon glyphicon-user"></span> Sign Up</a></li>
                        <li><a href="#"><span class="glyphicon glyphicon-log-in"></span> Login</a></li>
                    </ul>
                </div>
            </div>
        </nav>

index.jsp

<%@include file="./shared/header.jsp" %>

<div class="container">
    <form:form commandName="userData" action="#" method="post" enctype="multipart/form-data">
        <label for="firstname" class="label">Enter your first name</label> 
        <form:input path="firstName"/>
    </form:form>
</div>

servlet 类

package com.nishan.springcruddemo.servlet;

import com.nishan.springcruddemo.daoimp.CustomerDaoImp;
import com.nishan.springcruddemo.entity.Customer;
import java.sql.SQLException;
import org.springframework.beans.factory.annotation.Autowired;
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;

/**
 *
 * @author Dell
 */
@Controller

public class DefaultController {

    @Autowired
    CustomerDaoImp customerDaoImp;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String index() {
        return "index";
    }

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public String save(@ModelAttribute("userData") Customer customer) {
        try {
            customerDaoImp.insert(customer);
        } catch (ClassNotFoundException | SQLException ex) {
            System.out.println(ex.getMessage());
        }

        return "redirect:/index";
    }

    @RequestMapping(value = "/view", method = RequestMethod.GET)
    public String viewCustomer() {
        return "view-customer";
    }
}

【问题讨论】:

    标签: java spring jsp spring-form


    【解决方案1】:

    您必须使用@RequestParam 来获取单个值。

    要将表单数据与模型类绑定您需要创建模型(POJO)为 userData commandName="userData".CommandName 和 ModelClass Name 都相同。

    firstName 类的字段,两者相同。然后你的表单数据与 Pojo 绑定它会来到你的控制器类。

    我希望它会有所帮助。

    【讨论】:

      猜你喜欢
      • 2016-02-17
      • 2020-09-26
      • 2018-01-07
      • 2020-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多