Ни BindingResult, ни простой целевой объект для имени компонента 'user' не доступны в качестве атрибута запроса
05 апреля 2019 9:18:13 org.apache.catalina.core.StandardWrapperValve вызывают SEVERE: Servlet.service() для сервлета [SpringController] в контексте с путем [/ DataVisualization] выдало исключение [Произошло исключение при обработке страницы JSP /WEB-INF/views/AddUserForm.jsp в строке 23
20: 21:
22: введите имя: 23: 24: 25:
26: сохраните
Stacktrace:] с первопричиной java.lang.IllegalStateException: ни BindingResult, ни простой целевой объект для имени компонента «пользователь» не доступны в качестве запросаатрибут в 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");
}
}
класс модели: пакет 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>