无法显示jsp页面
Posted
技术标签:
【中文标题】无法显示jsp页面【英文标题】:could not able to display the jsp page 【发布时间】:2019-04-05 01:56:22 【问题描述】:Bean 名称“用户”的 BindingResult 和普通目标对象都不能用作请求属性
2019 年 4 月 5 日上午 9:18:13 org.apache.catalina.core.StandardWrapperValve 调用 严重:servlet [SpringController] 的 Servlet.service() 在路径 [/DataVisualization] 的上下文中抛出异常 [在第 23 行处理 JSP 页面 /WEB-INF/views/AddUserForm.jsp 时发生异常
20: 21: 22:输入姓名: 23: 24: 25: 26:保存
Stacktrace:] 有根本原因 java.lang.IllegalStateException:Bean 名称“用户”的 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>
【问题讨论】:
【参考方案1】:下面的方法调用了用户表单,但是这里你没有提到JSP中使用的模型属性,这就是你得到错误的原因。因为在你的 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")
您在映射中错过了 /。这是一个最佳实践。
祝你好运
【讨论】:
【参考方案2】:为什么要使用 BindingResult?我只知道 BindingResult 与 @Valid 一起使用。 尝试 drop BindingResult ,我认为它会起作用!
@RequestMapping(value="save", method=RequestMethod.POST)
public void saveUser(@ModelAttribute("user") User user,Model model)
System.out.println("=== save user method");
【讨论】:
以上是关于无法显示jsp页面的主要内容,如果未能解决你的问题,请参考以下文章
关于springmvc 只能在index.jsp页面显示图片的处理办法jsp页面无法显示图片
AJAX提交表单,上传出错的国际化信息无法显示在jsp页面上
shiro+springmvc集成时,配置loginUrl='login.jsp',但是login.jsp页面在WEB-INF 目录下,导致无法显示