如何处理java.lang.IllegalStateException

时间:2018-05-25 10:31:59

标签: java spring-mvc

以下是我的User.controller

package com.niit.wedSchooling_fc.Controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.niit.Dao.MemberDao;
import com.niit.Dao.UserDao;
import com.niit.model.Membership;
import com.niit.model.UserInfo;

@Controller

public class UserController 
{
    @Autowired
    UserInfo userInfo;

    @Autowired
    UserDao userDao;

    @Autowired
    Membership membership;

    @Autowired
    MemberDao memberDao;

    @RequestMapping("/User")
    public ModelAndView userform()
    {
        List<UserInfo> users=userDao.list();
        ModelAndView obj=new ModelAndView("User");
        obj.addObject("user",new UserInfo());
        obj.addObject("Users",users);
        return obj;
    }
 @RequestMapping("/addusr")
 public ModelAndView addusr(@ModelAttribute("user") UserInfo usr)
 {
     ModelAndView obj =new ModelAndView("redirect:/User");
     if(userDao.saveorupdate(usr)==true)
     {
         obj.addObject("Msg1","User added Successfully");
     }
     else
     {
         obj.addObject("Msg2","User not added");
     }
     return obj;
 }

 public ModelAndView memberform()
 {
     List<Membership> members=memberDao.list();
     ModelAndView obj1= new ModelAndView("User");
     obj1.addObject("member",new Membership());
     obj1.addObject("Members",members);
     return obj1;
 }
 @RequestMapping("/addmember")
 public ModelAndView addmember(@ModelAttribute("member") Membership member)
 {
     ModelAndView obj1 =new ModelAndView("redirect:/User");
     if(memberDao.saveorupdate(member)==true)
     {
         obj1.addObject("Msg1","Membership User added Successfully");
     }
     else
     {
         obj1.addObject("Msg2","Membership User not added");
     }
     return obj1;
 }

}

我的User.jsp页面也可以在下面找到:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="c" uri="http://www.springframework.org/tags" %>
<!DOCTYPE html>
<html lang="en" >

<head>
<c:url value="/resources/image" var="img"/>
<c:url value="/resources/css" var="css"/>
<c:url value="/resources/js" var="js"/>
  <meta charset="UTF-8">
  <title>Login & Sign Up Form Concept</title>


  <link rel='stylesheet prefetch' href='https://fonts.googleapis.com/css?family=Open+Sans:400,300'>
<link rel='stylesheet prefetch' href='https://fonts.googleapis.com/icon?family=Material+Icons'>

      <link rel="stylesheet" href="${css}/user.css">
<style>
*{
    margin: 10;
    padding: 0;
}
body {
  text-align: center;
}
#main{
    width: 100%;
    height: 175vh;
    background-image: url(${img}/user.jpg);
    background-size: cover;
    opacity: 0.7;
}
</style> 
</head>
<body>
<meta name="viewport" content="width=device-width,initial scale=1">
<div id="main">

  <div class="cotn_principal">
<div class="cont_centrar">

  <div class="cont_login">
<div class="cont_info_log_sign_up">
      <div class="col_md_login">
<div class="cont_ba_opcitiy">

        <h2>USER</h2>  

  <button class="btn_login" onclick="cambiar_login()">SIGNUP</button>
  </div>
  </div>
<div class="col_md_sign_up">
<div class="cont_ba_opcitiy">
  <h2>MEMBER</h2>




  <button class="btn_sign_up" onclick="cambiar_sign_up()">SIGNUP</button>
</div>
  </div>
       </div>


    <div class="cont_back_info">
       <div class="cont_img_back_grey">
       <img src="${img}/user.jpg" alt="" />
       </div>

    </div>
<div class="cont_forms" >
    <div class="cont_img_back_">
       <img src="${img}/user.jpg" alt="" />
       </div>
 <div class="cont_form_login">
<a href="#" onclick="ocultar_login_sign_up()" ><i class="material-icons">&#xE5C4;</i></a>
   <h2>USER</h2>
   <c:url value="/addusr" var="usr"/>
   <form:form  action="${usr}" method="post" commandName="user">
 <form:input type="text" path="First_name" placeholder="First Name" name="fname"/>
<form:input type="text"  path="Last_name" placeholder="Last Name" name="lname"/> 
<form:input type="text" path="Email" placeholder="Email" name="id" />
<form:input type="text" path="Location" placeholder="Location" name="city"/>
<form:input type="password" path="password" placeholder="Password" name="pwd" />
<form:input type="password" path="Confirmpassword" placeholder="Confirm Password" name="pwd" />
<p><font face="'Merienda', cursive" align="left">Gender</font></p>
<div style="font-weight: normal;font-style:'Open Sans', sans-serif; font-size:17px"><form:radiobutton path="gender" name="Gender" value="Male"/>Male
<form:radiobutton path="gender" name="Gender" value="female"/>Female<br></div>
<button class="btn_sign_up" onclick="cambiar_sign_up()">SIGN UP</button>
</form:form>
  </div>

   <div class="cont_form_sign_up">
<a href="#" onclick="ocultar_login_sign_up()"><i class="material-icons">&#xE5C4;</i></a>
     <h2>MEMBER</h2>
     <c:url value="/addmember" var="member"/>
   <form:form  action="${member}" method="post" commandName="member">
<form:input type="text" path="mem_First_name" placeholder="First Name" name="fsname"/>
<form:input type="text" path="mem_Last_name" placeholder="Last Name" name="lsname"/> 
<form:input type="text" path="mem_mblno" placeholder="Mobile No" name="mblno"/>
<form:input type="text" path="mem_Email" placeholder="Email" name="id"/>
<form:input type="text" path="Nationality" placeholder="Nationality" name="nation"/>
<form:input type="text" path="mem_city" placeholder="City" name="cty"/>
<form:input type="text" path="Profession" placeholder="Profession" name="prfsn"/>
<form:input type="text" path="mem_User_Name" placeholder="UserName" name="usrname"/>
<form:input type="password" path="mem_password"  placeholder="Password" name="pwd"/>
<form:input type="password" path="Confirm_password" placeholder="Confirm Password" name="pwd"/>
<form:input type="file" path="path" id="profile_pic" name="profile_pic" accept=".jpg, .jpeg, .png"/>
<p style="font-weight: normal;font-style: italic; font-size:17px">Gender</p>
<div style="font-weight: normal;font-style: italic; font-size:17px"><form:radiobutton path="mem_gender" name="Gender" value="Male"/>Male
<form:radiobutton path="mem_gender" name="mem_Gender" value="female"/>Female<br></div>
<button class="btn_sign_up" onclick="cambiar_sign_up()">SIGN UP</button>
</form:form>
  </div>

    </div>

  </div>
 </div>
</div>
  </div>


    <script  src="${js}/user.js"></script>




</body>

</html>

运行此代码时,我得到:

  

java.lang.IllegalStateException:无法将BindingResult和bean名称“member”的普通目标对象用作请求属性。

下面我还提供了localhost中的错误:

HTTP Status 500 – Internal Server Error
Type Exception Report

Message An exception occurred processing [/WEB-INF/views/User.jsp] at line [100]

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

org.apache.jasper.JasperException: An exception occurred processing [/WEB-INF/views/User.jsp] at line [100]

97:      <h2>MEMBER</h2>
98:      <c:url value="/" var="member"/>
99:    <form:form  action="${member}" method="post" commandName="member">
100: <form:input type="text" path="mem_First_name" placeholder="First Name" name="fsname"/>
101: <form:input type="text" path="mem_Last_name" placeholder="Last Name" name="lsname"/> 
102: <form:input type="text" path="mem_mblno" placeholder="Mobile No" name="mblno"/>
103: <form:input type="text" path="mem_Email" placeholder="Email" name="id"/>


Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:593)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:477)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    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:1286)
    org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1041)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:984)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
    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:634)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Root Cause

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'member' available as request attribute
    org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:144)
    org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:168)
    org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:188)
    org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getName(AbstractDataBoundFormElementTag.java:154)
    org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.autogenerateId(AbstractDataBoundFormElementTag.java:141)
    org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.resolveId(AbstractDataBoundFormElementTag.java:132)
    org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.writeDefaultAttributes(AbstractDataBoundFormElementTag.java:116)
    org.springframework.web.servlet.tags.form.AbstractHtmlElementTag.writeDefaultAttributes(AbstractHtmlElementTag.java:422)
    org.springframework.web.servlet.tags.form.InputTag.writeTagContent(InputTag.java:142)
    org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:84)
    org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:80)
    org.apache.jsp.WEB_002dINF.views.User_jsp._jspx_meth_form_005finput_005f6(User_jsp.java:968)
    org.apache.jsp.WEB_002dINF.views.User_jsp._jspx_meth_form_005fform_005f1(User_jsp.java:872)
    org.apache.jsp.WEB_002dINF.views.User_jsp._jspService(User_jsp.java:244)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:444)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    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:1286)
    org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1041)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:984)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
    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:634)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Note The full stack trace of the root cause is available in the server logs.

我自己无法解决这个问题。

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

我认为默认@RequestMapping注释使用GET,并且在您的jsp中调用POST,在控制器中添加方法POST,我也会添加{ {1}}方法的参数

BindingResult bindingResult

同样作为建议,您要将第一个actionName @RequestMapping(value = "/addmember", method = RequestMethod.POST) public ModelAndView addmember(@ModelAttribute("member") Membership member, BindingResult bindingResult) 分配给/addmember变量,并且您还使用member变量作为模型名称,这可能会让其他开发人员感到困惑,所以改为

member

<c:url value="/addmember" var="member"/>
<form:form  action="${member}" method="post" commandName="member">