首页 \ 问答 \ 模型属性在JSP页面中不可用作请求属性(Model attribute is not available as request attribute in JSP page)

模型属性在JSP页面中不可用作请求属性(Model attribute is not available as request attribute in JSP page)

我正在尝试使用Spring MVC和JSP页面创建一个示例注册页面。

在tomcat服务器上打开url时,我收到以下错误

root cause
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'register' 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(

我有一个JSP register.jsp

    <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Registration</title>
    </head>
    <body>
    <form:form action="/register/process" method="POST" modelAttribute="register">
        <table style="text-align: center;">

            <tr>
                <td><form:label path="fname">First Name</form:label></td>
                <td><form:input path="fname" name="fname"
                        id="fname" /></td>
            </tr>
            <tr>
                <td><form:label path="lname">Last Name</form:label></td>
                <td><form:input path="lname" name="lname" id="lname" />
                </td>
            </tr>
            <tr>
                <td></td>
                <td>
                <input type="submit" value="CREATE AN ACCOUNT"/>
                </td>
            </tr>
        </table>
    </form:form>
   </body>
  </html>

我有一个控制器类UserController.java

package vnfhub.supplier.controller;

@Controller
public class UserController {
    @RequestMapping(value = "/register", method = RequestMethod.GET)
    public String getRegisterForm(Model model) {
        model.addAttribute("register", new Register());
        return "register";
    }

   @RequestMapping(value = "/register/process", method = RequestMethod.POST)
   public String processRegistration(@ModelAttribute("register") Register register, BindingResult result) {
       return "success";
   }
}

和一个success.jsp页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Success Form</title>
</head>
<body>
<font color="green"><h1>Hello</h1></font>

<h1>You have successfully registered</h1>
<font color="green"><h1>Welcome to Spring world !</h1></font>
</body>
</html>

我已经尝试了很多解决方案在stackoverflow ....但他们都没有工作。


I am trying to create a sample registration page with Spring MVC and JSP pages.

While opening the url on tomcat server, I am getting following error

root cause
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'register' 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(

I have a JSP register.jsp

    <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Registration</title>
    </head>
    <body>
    <form:form action="/register/process" method="POST" modelAttribute="register">
        <table style="text-align: center;">

            <tr>
                <td><form:label path="fname">First Name</form:label></td>
                <td><form:input path="fname" name="fname"
                        id="fname" /></td>
            </tr>
            <tr>
                <td><form:label path="lname">Last Name</form:label></td>
                <td><form:input path="lname" name="lname" id="lname" />
                </td>
            </tr>
            <tr>
                <td></td>
                <td>
                <input type="submit" value="CREATE AN ACCOUNT"/>
                </td>
            </tr>
        </table>
    </form:form>
   </body>
  </html>

I have a controller class UserController.java

package vnfhub.supplier.controller;

@Controller
public class UserController {
    @RequestMapping(value = "/register", method = RequestMethod.GET)
    public String getRegisterForm(Model model) {
        model.addAttribute("register", new Register());
        return "register";
    }

   @RequestMapping(value = "/register/process", method = RequestMethod.POST)
   public String processRegistration(@ModelAttribute("register") Register register, BindingResult result) {
       return "success";
   }
}

and a success.jsp page

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Success Form</title>
</head>
<body>
<font color="green"><h1>Hello</h1></font>

<h1>You have successfully registered</h1>
<font color="green"><h1>Welcome to Spring world !</h1></font>
</body>
</html>

I have tried many solution on stackoverflow.... but none of them worked.


原文:https://stackoverflow.com/questions/47467274
更新时间:2020-01-10 11:01

最满意答案

我发现你的代码没问题,只要你在这里给出。 我用你的代码模仿了这种情况,但不幸地发现没有异常

在这里输入图像描述

你可能做错了的事情是你正在tomcat中运行一些旧的构建代码。 尝试清理构建重新部署到您的容器中。

注意:一个友好的建议。 你做错了一件事就是将你的表单操作/register/process发送给容器根目录(例如localhost:8080/register/process )。 你会得到404 。 你不可能想要那样。 register/process应该是你的URL,并且这会将请求发送给你的应用程序上下文。 如果您的应用程序上下文是localhost:8080/test ,则会将请求发送到localhost:8080/test/register/process


I find your code okay so far as you given here. I mimic the situation with your code but unfortuantely found No Exception.

enter image description here

Things that you might have doing wrong is you are running some old build code in your tomcat. try to clean build and re-deploy in your container.

NB: one friendly suggestion. You are doing one thing wrong that is having action of your form to /register/process that will send the request to the container root (e.g. localhost:8080/register/process). And you will get 404 for that. You are not probably want that. register/process should be your URL and this will POST the request relative to your application-context. If your application context is something localhost:8080/test, this will send the request to localhost:8080/test/register/process

相关问答

更多

如何迭代我的JSP页面上的所有模型属性?(How do I iterate over all my model attributes on my JSP page?)

模型属性是“请求范围”对象,您可以执行以下操作(我使用JSTL): <c:forEach items="${requestScope}" var="par"> <c:if test="${par.key.indexOf('attrName_') > -1}"> <li>${par.key} - ${par.value}</li> </c:if> </c:forEach> 由于没有过滤器,您将拥有所有的请求范围对象,我通过我们 ...

BindingResult和bean名称的普通目标对象都不可用作请求属性[duplicate](Neither BindingResult nor plain target object for bean name available as request attribute [duplicate])

在控制器中,您需要添加登录对象作为模型的属性: model.addAttribute("login", new Login()); 喜欢这个: @RequestMapping(value = "/", method = RequestMethod.GET) public String displayLogin(Model model) { model.addAttribute("login", new Login()); return "login"; } In the ...

了解JSP errorPage属性(Understanding JSP errorPage attribute)

JSP错误页面仅用于运行时错误(加载页面时),当servlet容器尝试编译JSP时,tomcat错误是编译错误。 The JSP error page is only for run-time errors (when the page is loaded), the tomcat errors are compile errors when the servlet container tries to compile your JSP.

请求属性未从Action传递到JSP(Request attribute not passed from Action to JSP)

JSON就是答案。 我创建了一个JSON对象并将其设置为在我的Action文件中设置响应对象。 String myJsonObj = createJSONObject(myList); setReponseObj(response, myJsonObj, "text/html"); 在我的JSP中访问了这个JSON对象。 函数showHistoryLog现在更改为: function showHistoryLog() { var questionId = document.getEle ...

Servlet不向jsp页面发送属性(Servlet not sending attribute towards jsp page)

尝试另一种方式 通过将这些值保持在会话中 语法session.setAttribute("errmsg",errmsg); 并检索session.getAttribute("errmsg"); 在servlet中这样做 HttpSession session =request.getSession(); session.setAttribute("errmsg",errmsg); 在jsp页面中只需执行session.getAttribute("errmsg"); try in another ...

jsp包含在请求属性中使用HashMap的页面参数(jsp include page parameter using a HashMap in a request attribute)

您必须升级到至少JSP 2.0才能使<jsp:param> EL工作。 JSP 1.2语法参考没有提到<jsp:param>的任何EL支持,而JSP 2.0语法参考确实如此。 You've to upgrade to at least JSP 2.0 to get EL in <jsp:param> to work. JSP 1.2 syntax reference doesn't mention of any EL support in <jsp:param>, while JSP 2.0 sy ...

在JSP中检查请求属性(Checking Request Attribute in JSP)

试试这个: request.setAttribute("submitted", true); 在你的JSP中: <c:if test="${submitted}"> // some html </c:if> Try this instead: request.setAttribute("submitted", true); and in your JSP: <c:if test="${submitted}"> // some html </c:if>

JSP中的request属性和pagecontext属性(request attribute and pagecontext attribute in JSP)

规则 : 这取决于您的要求,Session在整个会话中保持数据(即变量值)(即直到用户关闭浏览器或会话超时),而Request会在单个请求中保存数据,这包括请求时的情况被重定向或发送。 在您的示例中,pageContext.getSession()。getAttribute(“itemList”)返回null,因为您在请求中设置属性而不是会话,但为什么EL代码返回null,我无法弄明白为什么....你可能在你的jsp中定义了一个名为'itemList'的局部变量,它是null,所以它先读取它, 在 ...

JSP获取属性定义(JSP getting attribute definition)

在控制器函数中使用ModelMap map而不是Model模型,然后在该映射中添加属性。 还要检查jsp顶部的页面目录。 该标签中是否有任何属性,如isELIgnored='true' ,然后将其删除,然后尝试使用此属性。 我认为它应该可以解决你的问题。 干杯。 Instead of Model model in your controller function use ModelMap map and then add your attribute in that map. Also check ...

模型属性在JSP页面中不可用作请求属性(Model attribute is not available as request attribute in JSP page)

我发现你的代码没问题,只要你在这里给出。 我用你的代码模仿了这种情况,但不幸地发现没有异常 。 你可能做错了的事情是你正在tomcat中运行一些旧的构建代码。 尝试清理构建并重新部署到您的容器中。 注意:一个友好的建议。 你做错了一件事就是将你的表单操作/register/process发送给容器根目录(例如localhost:8080/register/process )。 你会得到404 。 你不可能想要那样。 register/process应该是你的URL,并且这会将请求发送给你的应用程序 ...

相关文章

更多

最新问答

更多
  • 未捕获的不变违规:addComponentAsRefTo(...):只有ReactOwner可以有refs(Uncaught Invariant Violation: addComponentAsRefTo(…): Only a ReactOwner can have refs)
  • 通过嵌入式YouTube / Flash视频避免滚轮劫持(Avoid scroll-wheel hijack by embedded youtube / flash video)
  • 如何在多用户环境中处理表单编辑?(how to handle form editing in a Multi-user environment?)
  • PHP关闭MySQL连接(PHP close MySQL connection)
  • 我要微信下载
  • Solr dataimport处理程序查询(Solr dataimport handler query)
  • GIT是什么车.
  • Powershell错误处理和空结果(Powershell Error Handling and Null results)
  • Laravel按第二级值排序第一级集合(Laravel sorting 1st level collection by 2nd level value)
  • logback.xml到logback属性文件(logback.xml to logback property file)
  • 使用fgets和strcat时出现问题(Problems when using fgets and strcat)
  • 绘制熊猫数据框两栏(plot pandas dataframe two columns from)
  • 超全球$ _GET中是否保证键值对的顺序?(Is the order of key-value pairs guaranteed in the superglobal $_GET?)
  • C# - 检查两组索引值(C# - Checking Index values of two sets)
  • 模板中的URL输出为空(URL output in template is empty)
  • 关于redhat linux 9.0的三个镜像文件
  • 为什么要在go中分配对struct的引用?(Why assign a reference to a struct in go?)
  • Datagrid点击事件(Datagrid click event)
  • window.location.href在输入press时返回undefined(window.location.href on enter press returns undefined)
  • Javascript Array.remove()作者:John Resig - 为什么要在for-in语句中枚举?(Javascript Array.remove() by John Resig - why does it enumerate in for-in statement?)
  • sudo gem安装cocoapods --pre错误(sudo gem install cocoapods --pre error)
  • 加载完成后,在所有已加载的HTML文件中执行某些操作(After Load Complete, Do Something in All Loaded HTML Files)
  • 如何为wpf datagrid行应用样式(How to have style applied for wpf datagrid row)
  • 在debian上升级内核[关闭](Upgrade kernel on debian [closed])
  • 无法使用JavaScript FileReader API上传二进制文件(Trouble uploading binary files using JavaScript FileReader API)
  • 工作简历怎样写啊?电脑方面的
  • 查找通过gmail API检索的环聊和聊天消息的时间戳(Find timestamp for hangout and chat messages retrieved with gmail api)
  • 如果在汇编程序中没有退出系统调用,会发生什么情况?(What happens if there is no exit system call in an assembly program?)
  • 无法从Intranet访问Wordpress门户(Wordpress portal not accessible from intranet)
  • PDFBox:禁用字体缓存或更改其位置(PDFBox: Disable Font Cache or change its location)