java - 创建表单时出现 Spring 错误

标签 java spring jsp spring-mvc

我正在尝试在 Spring 制作一个注册页面,这是代码

index.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!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>Welcome to Spring Web MVC project</title>
    </head>

    <body>
        <h1>Spring 3 Register!</h1>
        <form:form action="register" method="POST" modelAttribute="userForm">
            <table>
                <tr>
                    <td colspan="2" align="center">Spring MVC Form Demo - Registration</td>
                </tr>
                <tr>
                    <td>User Name</td>
                    <td><form:input path="username" /></td>
                </tr>
                <tr>
                    <td>Password</td>
                    <td><form:password path="password" /></td>
                </tr>
                <tr>
                    <td>Email</td>
                    <td><form:input path="email" /></td>
                </tr>
                <tr>
                    <td>BirthDate (mm/dd/yyyy)</td>
                    <td><form:input path="birthDate" /></td>
                </tr>
                <tr>
                    <td>Profession</td>
                    <td><form:select path="profession" items="${professionList}" /></td>
                </tr>
                <tr>
                    <td colspan="2" align="center"><input type="submit" value="Register" /></td>
                </tr>

            </table>
        </form:form>
    </body>
</html>

RegistrationController.java

package SpringRegister.controller;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *
 * @author Harshit Shrivastava
 */
import SpringRegister.controller.User;

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;

@Controller
@RequestMapping(value = "/register")
public class RegistrationController {

    @RequestMapping(method = RequestMethod.GET)
    public String viewRegistration(Map<String, Object> model)
    {
        User userForm = new User();
                model.put("userForm", new User());
        return "Registration";
    }

    @RequestMapping(method = RequestMethod.POST)
    public String processRegistration(@ModelAttribute("userForm") User user, Map<String, Object> model)
    {       
        System.out.println("Username : " + user.getUserName());
        model.put("userForm", new User());
        return "RegistrationSuccess";
    }
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>redirect.jsp</welcome-file>
    </welcome-file-list>
</web-app>

调度程序-servlet.xml

<?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:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd ">

    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

    <context:component-scan base-package="SpringRegister" />
    <mvc:annotation-driven />
    <!--
    Most controllers will use the ControllerClassNameHandlerMapping above, but
    for the index controller we are using ParameterizableViewController, so we must
    define an explicit mapping for it.
    -->
    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="index.htm">indexController</prop>
            </props>
        </property>
    </bean>

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

    <!--
    The index controller.
    -->
    <bean name="indexController"
          class="org.springframework.web.servlet.mvc.ParameterizableViewController"
          p:viewName="index" />

</beans>

我在这里收到此错误

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'userForm' available as request attribute

root cause

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'userForm' available as request attribute

我努力寻找错误,但无法得到它,因为我是 Spring 新手。这里可能的原因是什么?

最佳答案

更改此行:

public String viewRegistration(Map<String, Object> model)

对此:

public String viewRegistration(Model model) {
    model.addAttribute("userForm", new User());
    ...

关于java - 创建表单时出现 Spring 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34713655/

相关文章:

java - 如何使用 OkHttpClient MockWebSever 返回图像/字节[]?

java - 当我使用正则表达式处理多项式时可以处理第一行空间

java - Spring MVC 应用程序中 jsp 文件中的粉碎表单

javascript - JSP:插入不起作用 (SQLSERVER)

java - 使图像文件夹在项目中可用

java - 在JAVA操作系统上添加监听器

java - Springs MVC JSON 响应并将其转换为 JS 对象

spring - 使用 Spring 4.x 的 Solace 连接问题

spring - 如何模拟 Spring 注入(inject)的服务代理?

css - 将其包含在 .jsp 页面中时发生冲突的 css 文件