Spring使用Netbeans既不是BindingResult,也不是bean名称Netbeans的普通目标对象

Spring使用Netbeans既不是BindingResult,也不是bean名称Netbeans的普通目标对象,spring,jsp,spring-mvc,Spring,Jsp,Spring Mvc,我对使用Spring框架开发java应用程序非常陌生。我以前曾使用MVC架构开发过应用程序,但今年春天真的让我心烦意乱 我的意思是我不能在一个小时内把一件新东西做好。无论如何,我在处理表单时遇到了一个非常常见的问题。我已经试了几个小时,在网上搜索我的问题,但都没有成功。这与Netbeans有关吗?我不知道 index.jsp <%@page contentType="text/html" pageEncoding="UTF-8"%> <%@ taglib prefix="for

我对使用Spring框架开发java应用程序非常陌生。我以前曾使用MVC架构开发过应用程序,但今年春天真的让我心烦意乱

我的意思是我不能在一个小时内把一件新东西做好。无论如何,我在处理表单时遇到了一个非常常见的问题。我已经试了几个小时,在网上搜索我的问题,但都没有成功。这与Netbeans有关吗?我不知道

index.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <form:form action="apptest.htm" commandName="userForm" method="POST">
            Name:<form:input path="name" />
            <br>
            Age:<form:input path="age" />
            <br>
            City:<form:input path="city" />
            <br>
            <input type="submit" value="Submit">
            <br> 
        </form:form>

    </body>
</html>
dispatcher-servlet.xml

<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?xml version="1.0" encoding="UTF-8"?> -->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
    <context:annotation-config />
    <context:component-scan base-package="SpringApp" />
    <mvc:annotation-driven />

    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" />

</beans>
test.jsp

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>

        Name: ${userForm.name} <br>
        City: ${userForm.city}
    </body>
</html>

JSP页面
名称:${userForm.Name}
城市:${userForm.City}
看起来您的
模型属性
没有绑定到
请求
,请在
index.jsp中进行以下更改

 <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
    <!DOCTYPE html>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>JSP Page</title>
        </head>
        <body>
            <form:form action="apptest.htm" modelAttribute="userForm" method="POST">
                Name:<form:input path="name" />
                <br>
                Age:<form:input path="age" />
                <br>
                City:<form:input path="city" />
                <br>
                <input type="submit" value="Submit">
                <br> 
            </form:form>

        </body>
    </html>

JSP页面
姓名:

年龄:
城市:

现在确保您有一个
test.jsp
具有
People
bean
modeldattribute
,否则它将再次导致错误

java.lang.IllegalStateException:bean名称“userForm”的BindingResult和普通目标对象都不能作为请求属性使用

从stacktrace
index.jsp
中可以看到,无法将模型属性映射到表单参数

这是因为在
中有
index.jsp
。因此,
index.jsp
在控制器之前加载,不能访问模型值

要解决此问题,您必须:

  • index.jsp
    文件移动到
    WEB-INF/jsp
    文件夹

  • 更改为:

    <welcome-file-list>
        <welcome-file></welcome-file>
    </welcome-file-list>
    

    web.xml

    <web-app id="WebApp_ID" version="2.4"
             xmlns="http://java.sun.com/xml/ns/j2ee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
        http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    
      <display-name>Spring MVC</display-name>
      <description>Spring MVC</description>
    
      <!-- For web context -->
      <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
      </servlet>
    
      <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
      </servlet-mapping>
    
      <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
          WEB-INF/application-context.xml
        </param-value>
      </context-param>
    
      <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
    
      <!--<welcome-file-list>-->
        <!--<welcome-file></welcome-file>-->
      <!--</welcome-file-list>-->
    </web-app>
    
    
    春季MVC
    春季MVC
    调度员
    org.springframework.web.servlet.DispatcherServlet
    1.
    调度员
    /
    上下文配置位置
    WEB-INF/application-context.xml
    org.springframework.web.context.ContextLoaderListener
    
    可能的副本我已经尝试了我在这里找到的所有东西,但都没有用。我觉得这与NetBeans有关。我做了您要求的更改,但问题仍然存在。我已经在我的问题中添加了test.jsp的代码,供您参考。先生,您刚刚度过了我的一天!!!我有一个问题,假设我根本没有任何索引文件,它的等价物是“demo.jsp”。那么,当我运行项目时,将打开哪个文件?只是想一想,先生。谢谢:)这取决于您从控制器返回的视图
    <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>JSP Page</title>
        </head>
        <body>
    
            Name: ${userForm.name} <br>
            City: ${userForm.city}
        </body>
    </html>
    
     <%@page contentType="text/html" pageEncoding="UTF-8"%>
        <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
        <!DOCTYPE html>
        <html>
            <head>
                <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
                <title>JSP Page</title>
            </head>
            <body>
                <form:form action="apptest.htm" modelAttribute="userForm" method="POST">
                    Name:<form:input path="name" />
                    <br>
                    Age:<form:input path="age" />
                    <br>
                    City:<form:input path="city" />
                    <br>
                    <input type="submit" value="Submit">
                    <br> 
                </form:form>
    
            </body>
        </html>
    
    <welcome-file-list>
        <welcome-file></welcome-file>
    </welcome-file-list>
    
    import SpringApp.DAO.People;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.ModelAttribute;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    
    import java.util.Map;
    @Controller
    @RequestMapping({"/", "/apptest.htm"})
    /**
     *
     * @author ROHAN
     */
    public class InsertController {
        @RequestMapping(method = RequestMethod.GET)
        public String viewRegistration(Map<String, Object> model) {
            People userForm = new People();
            model.put("userForm", userForm);
            return "index";
        }
    
        @RequestMapping(method = RequestMethod.POST)
        public String processRegistration(@ModelAttribute(value="userForm") People people,
                                          Map<String, Object> model) {
            model.put("name",people.getName());
            model.put("city",people.getCity());
            return "test";
        }
    }
    
    <web-app id="WebApp_ID" version="2.4"
             xmlns="http://java.sun.com/xml/ns/j2ee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
        http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    
      <display-name>Spring MVC</display-name>
      <description>Spring MVC</description>
    
      <!-- For web context -->
      <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
      </servlet>
    
      <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
      </servlet-mapping>
    
      <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
          WEB-INF/application-context.xml
        </param-value>
      </context-param>
    
      <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
    
      <!--<welcome-file-list>-->
        <!--<welcome-file></welcome-file>-->
      <!--</welcome-file-list>-->
    </web-app>