Spring MVC表单输入值始终为null [英] Spring MVC form input value is always null

查看:193
本文介绍了Spring MVC表单输入值始终为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Spring MVC的新手,但对Java的Web开发并不陌生。我正在尝试创建一个简单的form-> controller示例。

I'm new to Spring MVC, but not new to web development in Java. I'm attempting to create a simple form->controller example.

我有一个表单,一个表单控制器(在下面粘贴的上下文XML中配置)和我的模型(一个简单的bean)。当我提交表单时,我的文本输入的值始终为null,无论如何。任何想法?

I have a form, a form controller (configured in a context XML pasted below) and my model (a simple bean). When I submit the form the value of my text input is always null, regardless. Any ideas?

表格控制器弹簧配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- controllers -->

    <bean name="/home.html" class="atc.web.view.controller.HomeController" />

    <!--bean name="/mirror.html" class="atc.web.view.controller.MirrorController" -->

    <bean name="/url-cache.html" class="atc.web.view.controller.URLCacheFormController">
        <property name="synchronizeOnSession" value="true" />
        <!--property name="sessionForm" value="false"-->
        <property name="commandName" value="urlForm"/>
        <property name="commandClass" value="atc.web.view.model.URLBean"/>
        <property name="validator">
            <bean class="atc.web.view.validators.URLValidator"/>
        </property>
        <property name="formView" value="mirror"/>
        <property name="successView" value="url-cache.html"/>
        <property name="URLCachingService" ref="urlCachingService" />
    </bean>

    <!-- end controllers -->

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <!-- custom beans -->

    <bean id="breakcrumbInjectionFilter" class="atc.web.view.filter.BreadcrumbInjectionFilter">
        <property name="contextPrefix" value="/home-web" />
    </bean>

    <!-- end custom beans -->
</beans>

包含我的表单的JSP如下:

The JSP that contains my form is as follows:

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="/WEB-INF/jspf/core/taglibs.jspf" %>
<html>
<head><title>Simple tools</title></head>
<style>
.error s {
    color:#FF0000;
}
</style>
<body>
<%@ include file="/WEB-INF/jspf/nav/nav.jspf" %>
    Errors: <form:errors path="url" cssClass="error"/>
    <form:form method="post" commandName="urlForm">
        <form:input path="url" />
        <input type="submit" align="center" value="Execute" />
    </form:form>
</body>
</html>

这是我的控制器的完整来源:

Here's the full source of my controller:

public class URLCacheFormController extends SimpleFormController {
    private URLCachingService cachingService;

    private static Logger log = Logger.getLogger(URLCacheFormController.class);

    public ModelAndView onSubmit(Object command) throws ServletException {
        log.debug(String.format("URLCachingFormController received request with object '%s'", command));
        URLBean urlBean = (URLBean) command;
        try {
            URL url = new URL(urlBean.getUrl());
            URLCache cache = cachingService.cacheURL(url);
            cache.cacheToTempFile();

        } catch (IOException e) {
            log.error("Invalid URL...", e);
        }
        return new ModelAndView(new RedirectView(getSuccessView()));
    }

    protected Object formBackingObject(HttpServletRequest request) throws ServletException {
        log.debug("formBackingObject() ");
        return new URLBean();
    }

    public void setURLCachingService(URLCachingService cachingService) {
        this.cachingService = cachingService;
    }
}

以上所有产生以下HTML:

All of the above produces the following HTML:

<html>
<head></head>
<body>
<div><span><a href="home.html">Home </a></span><span> | <a href="url-cache.html">Page Mirror</a></span></div>
<div id="breadcrumb"><span>Your trail:<a href=""/></span></div>Attrs:<div/>
<form id="urlForm" method="post" action="/home-web/url-cache.html">
<input id="url" type="text" value="" name="url"/>
<input type="submit" align="center" value="Execute"/>
</form>
</body>
</html>

我现在重写 doSubmitAction(对象命令)但我还是没有按方法。表单提交,但接下来我知道我提交了空白表单(在 formBackingObject(HttpServletRequest请求)之后)。

I'm now overriding doSubmitAction(Object command) but I still do not hit the method. The form submits but the next thing I know I'm presented with the blank form (after formBackingObject(HttpServletRequest request) is called).

这就是说,当我提交时,表单控制器中 doSubmitAction 第1行的日志记录调用永远不会执行。 验证器执行并失败(正确添加错误消息),因为它检查的值始终为null (或正确放置,它从未设置) 。但是,始终会发生对 formBackingObject 的调用。我的表单的请求属性(表单输入'url')始终为null。

That's to say, when I submit, the logging call on line 1 of doSubmitAction in the form controller is never executed. The validator executes, and fails (adds error messages correctly) because the value it's checking is always null (or put correctly, it's never set). The call to formBackingObject always occurs however. The request attribute of my form (the form input 'url') is always null.

更新:好的,所以经过serg555建议的一些严肃的调试,并删除验证后,我可以确认问题似乎与映射有关请求参数 - 例如表单中'url'的值 - 到我的命令/ bean;即从未在我的bean上设置URL,或者正在重新创建bean。

Update: OK, so after some serious debugging as suggested by serg555, and removing validation, I can confirm the issue seems to be with mapping the request parameters - such as the value of 'url' from the form - to my command/bean; i.e. the URL is never being set on my bean, or the bean is being recreated.

请帮助?

推荐答案

原因是我的表单bean没有遵守Java Bean契约 - setter返回而不是无效,因此无法与Spring匹配来调用。

The cause was the fact that my form bean was not adhering to the Java Bean contract - the setter returned instead of being void and thus couldn't be matched by Spring to invoke.

这篇关于Spring MVC表单输入值始终为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆