0
votes

Neither BindingResult nor plain target object for bean name 'user' available as request attribute

Apr 05, 2019 9:18:13 AM org.apache.catalina.core.StandardWrapperValve invoke SEVERE: Servlet.service() for servlet [SpringController] in context with path [/DataVisualization] threw exception [An exception occurred processing JSP page /WEB-INF/views/AddUserForm.jsp at line 23

20: 21:
22: Enter Name: 23: 24: 25:
26: Save

Stacktrace:] with root cause java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'user' available as request attribute at org.springframework.web.servlet.support.BindStatus.(BindStatus.java:153)

package com.datavisualization.controller;


import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.datavisualization.model.User;

@Controller
@RequestMapping("restApi/dataVizualization")
public class GetUserData {
private static Logger logger=Logger.getLogger(GetUserData.class);
@RequestMapping(value="adduser")
public String addUserForm()
{
    logger.info("===addUserForm ");
    return "AddUserForm";
}
@RequestMapping(value="save", method=RequestMethod.POST)
public void saveUser(@ModelAttribute("user") User user,BindingResult                                          result,Model model)
{
    System.out.println("=== save user method");
}
}

model class: package com.datavisualization.model;

import java.io.Serializable;

public class User implements Serializable {

private static final long serialVersionUID = 1L;

private String name;

public User() {
super();
// TODO Auto-generated constructor stub
}
public User( String name) {
super();

this.name = name;
}


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

}

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"   %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
</head>
<body>
<div class="container">
        <h3 id="form_header" class="text-warning" align="center">User Form</h3>
        <div> </div>

        <!-- User input form to add a new user or update the existing user-->
        <c:url var="saveUrl" value="/restApi/dataVizualization/save" />
        <form:form id="user_form" modelAttribute="user" method="POST" action="${saveUrl}">

            <label for="user_name">Enter Name: </label>
            <form:input id="user_name" cssClass="form-control" path="name" />
            <div> </div>

            <button id="saveBtn" type="submit" class="btn btn-primary">Save</button>
        </form:form>
    </div>

web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
<display-name>Archetype Created Web Application</display-name>

<servlet>
    <servlet-name>SpringController</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>SpringController</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<!-- Welcome File List -->
<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

</web-app>

spring-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:component-scan base-package="com.datavisualization" />


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

2

2 Answers

0
votes

The below method calls the user form, but here you did not mention the model attribute which is used in JSP, that's why you get an error. because in your JSP page do not know what is the user. so you need to mention the user attribute in the controller

@RequestMapping(value="adduser")
public String addUserForm()
{
    logger.info("===addUserForm ");
    return "AddUserForm";
}

The solution is, change the method to this

@RequestMapping(value="adduser")
public String addUserForm(Model model)
{
    logger.info("===addUserForm ");
    model.addAttribute("user", new User());
    return "AddUserForm";
}

and Change @RequestMapping("restApi/dataVizualization") to @RequestMapping("/restApi/dataVizualization") you missed / in the mapping. it is a best practice.

best of luck

0
votes

Why you use BindingResult? I only kown BindingResult used with @Valid together. Try drop BindingResult , it will works i think!

@RequestMapping(value="save", method=RequestMethod.POST)
public void saveUser(@ModelAttribute("user") User user,Model model)
{
    System.out.println("=== save user method");
}