Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 弹簧3.6.4+表单绑定错误_Java_Spring_Spring Mvc - Fatal编程技术网

Java 弹簧3.6.4+表单绑定错误

Java 弹簧3.6.4+表单绑定错误,java,spring,spring-mvc,Java,Spring,Spring Mvc,//类型异常报告 消息java.lang.IllegalStateException:bean名称“test”的BindingResult和普通目标对象都不能作为请求属性使用 说明服务器遇到内部错误,无法满足此请求 HomeController.java import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springfra

//类型异常报告

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

说明服务器遇到内部错误,无法满足此请求

HomeController.java


import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/** Handles requests for the application home page. */
@Controller
public class HomeController {

    private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

    /** Simply selects the home view to render by returning its name. */
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String home(Model model, Locale locale) {
        logger.info("Welcome home! The client locale is {}.", locale);

        Date date = new Date();
        DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);

        String formattedDate = dateFormat.format(date);

        model.addAttribute("serverTime", formattedDate);

        return "home";
    }

}

HomeModel.java
package com.test.app;

public class HomeModel {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

HometController.java
package com.test.app;

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

@Controller
public class HometController {

    @RequestMapping(value = "/try", method = RequestMethod.POST)
    public String trynew(@ModelAttribute("test") HomeModel hm, BindingResult br, Model model) {
        model.addAttribute("test", new HomeModel());
        System.out.println("in try");
        return "tst";
    }

}

home.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@ page session="false" %>
<html>
<head>
    <title>Home</title>
</head>
<body>
<h1>
    Hello world!  
</h1>

<P>  The time on the server is ${serverTime}. </P>
</body>
<form:form method="POST" modelAttribute="test" name="test"  action="try">

<form:input path="name"/>

<input type="submit" value="test"/>
</form:form>
<!-- onClick="document.test.submit()" -->
</html>

tst.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Hello World with Spring 3 MVC</title>
        <meta http-equiv="Content-Type" content="text/html; charset=windows-1251">
    </head>
    <body>
        <h1>Registration Form</h1><br />
        <form:form commandName="USER">
        <table>
        <form:errors path="*" cssStyle="color : red;"/>

            <tr><td>Name : </td><td><form:input path="name" /></td></tr> 

            <tr><td colspan="2"><input type="submit" value="Save Changes" /></td></tr>
        </table>
        </form:form>
    </body>
</html>


java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'test' available as request attribute
删除此参数@ModelAttributetest HomeModel hm以消除异常

  @Controller
    public class HometController {

        @RequestMapping(value = "/try", method = RequestMethod.POST)
        public String trynew( BindingResult br, Model model) {
            model.addAttribute("test", new HomeModel());
            System.out.println("in try");
            return "tst";
        }

    }
请阅读以了解如何编写关于不起作用的代码的明确问题。