javax.servlet.jsp.JspTagException:Bean名称“用户”的BindingResult或普通目标对象都不能用作请求属性

时间:2019-04-25 02:29:22

标签: spring-mvc

我只是讲讲Spring MVC用户界面和后端处理,但是我无法将参数传递到后端进程中。

这是表单jsp代码。

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>



<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
<link href="src/main/webapp/recources/css/login.css" rel="stylesheet">
<link href="src/main/webapp/recources/css/boostrap.min.css"
    rel="stylesheet">
<script
    src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="src/main/webapp/recources/js/boostrap.min.js"></script>
</head>
<body>
    <spring:url value="register" var="registerUrl" />
    <div class="login-page">

        <div class="form">

            <form:form class="register-form" action="${registerUrl}"
                method="post" modelAttribute="user">
                <spring:bind path="username">
                    <div class="form-group ${status.error ? 'has-error': ''}">
                        <form:input type="text" path="username" placeholder="name" />
                        <form:errors path="username" />
                    </div>
                </spring:bind>
                <spring:bind path="password">
                    <div class="form-group ${status.error ? 'has-error':''}">
                        <form:input type="password" path="password" placeholder="password" />
                        <form:errors path="password"></form:errors>
                    </div>
                </spring:bind>

                <button>create</button>
                <p class="message">
                    Already registered? <a href="#">Sign In</a>
                </p>
            </form:form>

这里是Java控制器类。

package com.myApps.controller.userservice;

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

import com.myApps.vo.Register;

@Controller
@RequestMapping("/")
public class LoginController {
    @RequestMapping("/")
    public String loginPage()
    {
        return "login";
    }
    @RequestMapping(value="login", method=RequestMethod.POST)
    public String login()
    {

        System.out.println("login page:");
        return null;
    }

    @RequestMapping(value="register", method=RequestMethod.POST)
    public String register(@ModelAttribute("user") Register user, BindingResult result)
    {
        System.out.println("register:"+user.getUsername());

        return "login";
    }
}

这里是模型类。

    package com.myApps.vo;

public class Register {
private String username;
private String password;

public String getUsername() {
    return username;
}
public void setUsername(String username) {
    this.username = username;
}
public String getPassword() {
    return password;
}
public void setPassword(String password) {
    this.password = password;
}

}

当我尝试运行该项目时,我遇到了一些异常,我束缚了很多方式,但我无法解决此问题

message An exception occurred processing JSP page /WEB-INF/views/login.jsp at line 30

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: An exception occurred processing JSP page /WEB-INF/views/login.jsp at line 30

27: 
28:             <form:form class="register-form" action="${registerUrl}"
29:                 method="post" modelAttribute="user">
30:                 <spring:bind path="username">
31:                     <div class="form-group ${status.error ? 'has-error': ''}">
32:                         <form:input type="text" path="username" placeholder="name" />
33:                         <form:errors path="username" />


Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:574)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:461)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:168)
    org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303)
    org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1244)
    org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1027)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:971)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause

javax.servlet.ServletException: javax.servlet.jsp.JspTagException: Neither BindingResult nor plain target object for bean name 'user' available as request attribute
    org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:909)
    org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:838)
    org.apache.jsp.WEB_002dINF.views.login_jsp._jspService(login_jsp.java:483)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:168)
    org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303)
    org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1244)
    org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1027)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:971)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

1 个答案:

答案 0 :(得分:0)

加载登录页面时,请求范围内必须有一个User。这样做的方法有多种:一种选择是如下更改

@RequestMapping("/")
public ModelAndView loginPage()
{
   ModelAndView mav = new ModelAndView("login");
   mav.addObject("user", new User());
   return mav;
}