Bean名称'user'的BindingResult和普通目标对象都不能用作请求属性
2019年4月5日上午9:18:13 org.apache.catalina.core.StandardWrapperValve调用 严重:Servlet [SpringController]在路径为[/ DataVisualization]的上下文中的Servlet.service()引发了异常[在第23行处理JSP页面/WEB-INF/views/AddUserForm.jsp时发生了异常
20:
21:
22:输入名称:
23:
24:
25:
26:保存
Stacktrace:]具有根本原因 java.lang.IllegalStateException:BeanResult'user'的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>
答案 0 :(得分:0)
为什么使用BindingResult?我只知道将BindingResult与@Valid一起使用。 尝试删除BindingResult,我认为它将起作用!
@RequestMapping(value="save", method=RequestMethod.POST)
public void saveUser(@ModelAttribute("user") User user,Model model)
{
System.out.println("=== save user method");
}
答案 1 :(得分:0)
下面的方法调用用户表单,但是在这里您没有提及JSP中使用的model属性,这就是为什么会出错的原因。因为在您的JSP页面中不知道用户是什么。所以您需要在控制器中提及用户属性
@RequestMapping(value="adduser")
public String addUserForm()
{
logger.info("===addUserForm ");
return "AddUserForm";
}
解决方法是,将方法更改为此
@RequestMapping(value="adduser")
public String addUserForm(Model model)
{
logger.info("===addUserForm ");
model.addAttribute("user", new User());
return "AddUserForm";
}
和更改
@RequestMapping("restApi/dataVizualization")
至@RequestMapping("/restApi/dataVizualization")
您错过了映射中的 / 。这是最佳做法。
好运