在相同的html页面中处理百万美元两种形式会导致错误

时间:2016-06-14 11:51:02

标签: spring-mvc spring-boot thymeleaf

我正在尝试创建登录和注册页面,但问题是同一页面上的两个表单返回以下错误:

  

2016-06-14 13:17:57.970 ERROR 6108 --- [nio-8080-exec-4]   org.thymeleaf.TemplateEngine:   [THYMELEAF] [http-nio-8080-exec-4]异常处理模板   " index":执行处理器时出错   ' org.thymeleaf.spring4.processor.attr.SpringInputGeneralFieldAttrProcessor'   (指数:55)2016-06-14 13:17:57.972 ERROR 6108 --- [nio-8080-exec-4]   o.a.c.c.C。[。[。[/]。[dispatcherServlet]:Servlet.service()for   在path []的上下文中的servlet [dispatcherServlet]引发了异常   [请求处理失败;嵌套异常是   org.thymeleaf.exceptions.TemplateProcessingException:期间出错   执行处理器   ' org.thymeleaf.spring4.processor.attr.SpringInputGeneralFieldAttrProcessor'   (指数:55)]有根本原因   java.lang.IllegalStateException:既不是BindingResult也不是普通的   bean名称的目标对象' register'可用作请求属性     在   org.springframework.web.servlet.support.BindStatus。(BindStatus.java:144)   〜[spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE] at   org.thymeleaf.spring4.util.FieldUtils.getBindStatusFromParsedExpression(FieldUtils.java:396)   〜[thymeleaf-spring4-2.1.4.RELEASE.jar:2.1.4.RELEASE] at   org.thymeleaf.spring4.util.FieldUtils.getBindStatus(FieldUtils.java:323)   〜[thymeleaf-spring4-2.1.4.RELEASE.jar:2.1.4.RELEASE] at   org.thymeleaf.spring4.util.FieldUtils.getBindStatus(FieldUtils.java:289)   〜[thymeleaf-spring4-2.1.4.RELEASE.jar:2.1.4.RELEASE] at   org.thymeleaf.spring4.processor.attr.AbstractSpringFieldAttrProcessor.processAttribute(AbstractSpringFieldAttrProcessor.java:98)   〜[thymeleaf-spring4-2.1.4.RELEASE.jar:2.1.4.RELEASE] at   org.thymeleaf.processor.attr.AbstractAttrProcessor.doProcess(AbstractAttrProcessor.java:87)   〜[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE] at   org.thymeleaf.processor.AbstractProcessor.process(AbstractProcessor.java:212)   〜[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE] at   org.thymeleaf.dom.Node.applyNextProcessor(Node.java:1017)   〜[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE] at   org.thymeleaf.dom.Node.processNode(Node.java:972)   〜[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE] at   org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:695)   〜[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE] at   org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:668)   〜[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE] at   org.thymeleaf.dom.Node.processNode(Node.java:990)   〜[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE] at   org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:695)   〜[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE] at   org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:668)   〜[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE] at   org.thymeleaf.dom.Node.processNode(Node.java:990)   〜[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE] at   org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:695)   〜[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE] at   org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:668)   〜[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE] at   org.thymeleaf.dom.Node.processNode(Node.java:990)   〜[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE] at   org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:695)   〜[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE] at   org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:668)   〜[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE] at   org.thymeleaf.dom.Node.processNode(Node.java:990)   〜[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE] at   org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:695)   〜[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE] at   org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:668)   〜[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE] at   org.thymeleaf.dom.Node.processNode(Node.java:990)   〜[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE] at   org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:695)   〜[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE] at   org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:668)   〜[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE] at   org.thymeleaf.dom.Node.processNode(Node.java:990)   〜[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE] at   org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:695)   〜[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE] at   org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:668)   〜[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE] at   org.thymeleaf.dom.Node.processNode(Node.java:990)   〜[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE] at   org.thymeleaf.dom.Document.process(Document.java:93)   〜[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE] at   org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1155)   〜[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE] at   org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1060)   〜[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE] at   org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1011)   〜[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE] at   org.thymeleaf.spring4.view.ThymeleafView.renderFragment(ThymeleafView.java:335)   〜[thymeleaf-spring4-2.1.4.RELEASE.jar:2.1.4.RELEASE] at   org.thymeleaf.spring4.view.ThymeleafView.render(ThymeleafView.java:190)   〜[thymeleaf-spring4-2.1.4.RELEASE.jar:2.1.4.RELEASE] at   org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1246)   〜[spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE] at   org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1029)   〜[spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE] at   org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:973)   〜[spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE] at   org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:895)   〜[spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE] at   org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:967)   〜[spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE] at   org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:858)   〜[spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE] at   javax.servlet.http.HttpServlet.service(HttpServlet.java:622)   〜[tomcat-embed-core-8.0.33.jar:8.0.33] at   org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843)   〜[spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE] at   javax.servlet.http.HttpServlet.service(HttpServlet.java:729)   〜[tomcat-embed-core-8.0.33.jar:8.0.33] at   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)   〜[tomcat-embed-core-8.0.33.jar:8.0.33] at   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)   〜[tomcat-embed-core-8.0.33.jar:8.0.33] at   org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)   〜[tomcat-embed-websocket-8.0.33.jar:8.0.33] at   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)   〜[tomcat-embed-core-8.0.33.jar:8.0.33] at   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)   〜[tomcat-embed-core-8.0.33.jar:8.0.33] at   org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)   〜[spring-web-4.2.6.RELEASE.jar:4.2.6.RELEASE] at   org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)   〜[spring-web-4.2.6.RELEASE.jar:4.2.6.RELEASE] at   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)   〜[tomcat-embed-core-8.0.33.jar:8.0.33] at   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)   〜[tomcat-embed-core-8.0.33.jar:8.0.33] at   org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:87)   〜[spring-web-4.2.6.RELEASE.jar:4.2.6.RELEASE] at   org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)   〜[spring-web-4.2.6.RELEASE.jar:4.2.6.RELEASE] at   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)   〜[tomcat-embed-core-8.0.33.jar:8.0.33] at   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)   〜[tomcat-embed-core-8.0.33.jar:8.0.33] at   org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)   〜[spring-web-4.2.6.RELEASE.jar:4.2.6.RELEASE] at   org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)   〜[spring-web-4.2.6.RELEASE.jar:4.2.6.RELEASE] at   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)   〜[tomcat-embed-core-8.0.33.jar:8.0.33] at   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)   〜[tomcat-embed-core-8.0.33.jar:8.0.33] at   org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:121)   〜[spring-web-4.2.6.RELEASE.jar:4.2.6.RELEASE] at   org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)   〜[spring-web-4.2.6.RELEASE.jar:4.2.6.RELEASE] at   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)   〜[tomcat-embed-core-8.0.33.jar:8.0.33] at   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)   〜[tomcat-embed-core-8.0.33.jar:8.0.33] at   org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)   〜[tomcat-embed-core-8.0.33.jar:8.0.33] at   org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)   [tomcat-embed-core-8.0.33.jar:8.0.33] at   org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)   [tomcat-embed-core-8.0.33.jar:8.0.33] at   org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)   [tomcat-embed-core-8.0.33.jar:8.0.33] at   org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)   [tomcat-embed-core-8.0.33.jar:8.0.33] at   org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)   [tomcat-embed-core-8.0.33.jar:8.0.33] at   org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:522)   [tomcat-embed-core-8.0.33.jar:8.0.33] at   org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1095)   [tomcat-embed-core-8.0.33.jar:8.0.33] at   org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:672)   [tomcat-embed-core-8.0.33.jar:8.0.33] at   org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1502)   [tomcat-embed-core-8.0.33.jar:8.0.33] at   org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.run(NioEndpoint.java:1458)   [tomcat-embed-core-8.0.33.jar:8.0.33] at   java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)   [na:1.8.0_65] at   java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:617)   [na:1.8.0_65] at   org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)   [tomcat-embed-core-8.0.33.jar:8.0.33] at   java.lang.Thread.run(Thread.java:745)[na:1.8.0_65]

HTML表单代码

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8"/>
    <title>Material Login Form</title>

    <link rel="stylesheet" th:href="@{/css/bootstrap.min.css}"
          href="../static/css/reset.css"/>

    <link rel='stylesheet prefetch'
          href='http://fonts.googleapis.com/css?family=Roboto:400,100,300,500,700,900|RobotoDraft:400,100,300,500,700,900'/>
    <link rel='stylesheet prefetch' href='http://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css'/>

    <!--<link rel="stylesheet" href="../static/css/style.css"/>-->
    <link rel="stylesheet" th:href="@{/css/style.css}"
          href="../static/css/style.css"/>


</head>

<body>


<!-- Mixins-->
<!-- Pen Title-->

<div class="container">
    <div class="card"></div>
    <div class="card">
        <h1 class="title">Login</h1>
        <form action="login" th:action="@{/register}" th:object="${login}" method="post" >
            <div class="input-container">
                <input type="text" id="email" th:field="*{email}"  required="required"/>
                <label for="email">email</label>
                <div class="bar"></div>
            </div>
            <div class="input-container">
                <input type="password" id="Password" th:field="*{password}"  required="required"/>
                <label for="Password">Password</label>
                <div class="bar"></div>
            </div>
            <div class="button-container">
                <button ><span>Go</span></button>
            </div>
            <div class="footer"><a href="#">Forgot your password?</a></div>
        </form>
    </div>
    <div class="card alt">
        <div class="toggle"></div>
        <h1 class="title">Register
            <div class="close"></div>
        </h1>
        <form action="register" th:action="@{/register}" th:object="${register}" method="post">
            <div class="input-container">
                <input type="text" id="firstName" th:field="*{firstName}" required="required"/>
                <label for="firstName">First Name</label>
                <div class="bar"></div>
            </div>

            <div class="input-container">
                <input type="text" id="lastName" th:field="*{lastName}" required="required"/>
                <label for="lastName">Last Name</label>
                <div class="bar"></div>
            </div>

            <div class="input-container">
                <input type="text" id="email_" th:field="*{email}" required="required"/>
                <label for="email">Email</label>
                <div class="bar"></div>
            </div>
            <div class="input-container">
                <input type="password" id="Password_" th:field="*{password}" required="required"/>
                <label for="Password">Password</label>
                <div class="bar"></div>
            </div>
            <div class="button-container">
                <button><span>Next</span></button>
            </div>
        </form>
    </div>
</div>
<script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>

<script th:src="@{/js/index.js}"></script>


</body>
</html>

控制器代码:

@Controller
public class UserController {
    private UserServices userServices;

    @Autowired
    private UserRepository repository;


    @Autowired
    public UserController(UserServices userServices) {
    this.userServices=userServices;
    }

    // Retrive Registration form
    @RequestMapping(value = "/register", method = RequestMethod.GET)
    public String getRegisterForm(Model model) {
        model.addAttribute("register", new User());
        return "index";
    }

    //Submitting registration form and adding user
    @RequestMapping(value = "/register", method = RequestMethod.POST,params = "action=register")
    public String registerSubmit(@ModelAttribute User user, Model model) {
        model.addAttribute("register", user);
        if (repository.findByEmail(user.getEmail()) == null) {
            userServices.creat(user);
        } else {
            return "index";
        }
        return "index";
    }

    //retrive login form
    @RequestMapping(value = "/login",method = RequestMethod.GET)
    public String getLoginForm(Model model){

        model.addAttribute("login" , new User());
        return "index";
    }

}

1 个答案:

答案 0 :(得分:1)

您的错误消息显示Neither BindingResult nor plain target object for bean name 'register' available as request attribute。如果查看getLoginForm方法,则只填充“登录”对象,而不是模型中的“注册”对象。如果你想在一个页面上有两个表单,我想你想要一个method = RequestMethod.GET方法来填充两个表单的默认值。无论何时显示此页面,都需要确保填充两个表单支持对象。