Note that there are some explanatory texts on larger screens.

plurals
  1. POSpring MVC form input value is always null
    primarykey
    data
    text
    <p>I'm new to Spring MVC, but not new to web development in Java. I'm attempting to create a simple form->controller example. </p> <p>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?</p> <p>Form controller spring configuration:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;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"&gt; &lt;!-- controllers --&gt; &lt;bean name="/home.html" class="atc.web.view.controller.HomeController" /&gt; &lt;!--bean name="/mirror.html" class="atc.web.view.controller.MirrorController" --&gt; &lt;bean name="/url-cache.html" class="atc.web.view.controller.URLCacheFormController"&gt; &lt;property name="synchronizeOnSession" value="true" /&gt; &lt;!--property name="sessionForm" value="false"--&gt; &lt;property name="commandName" value="urlForm"/&gt; &lt;property name="commandClass" value="atc.web.view.model.URLBean"/&gt; &lt;property name="validator"&gt; &lt;bean class="atc.web.view.validators.URLValidator"/&gt; &lt;/property&gt; &lt;property name="formView" value="mirror"/&gt; &lt;property name="successView" value="url-cache.html"/&gt; &lt;property name="URLCachingService" ref="urlCachingService" /&gt; &lt;/bean&gt; &lt;!-- end controllers --&gt; &lt;bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt; &lt;property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /&gt; &lt;property name="prefix" value="/WEB-INF/jsp/" /&gt; &lt;property name="suffix" value=".jsp" /&gt; &lt;/bean&gt; &lt;!-- custom beans --&gt; &lt;bean id="breakcrumbInjectionFilter" class="atc.web.view.filter.BreadcrumbInjectionFilter"&gt; &lt;property name="contextPrefix" value="/home-web" /&gt; &lt;/bean&gt; &lt;!-- end custom beans --&gt; &lt;/beans&gt; </code></pre> <p>The JSP that contains my form is as follows:</p> <pre><code>&lt;%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %&gt; &lt;%@ page contentType="text/html;charset=UTF-8" language="java" %&gt; &lt;%@ include file="/WEB-INF/jspf/core/taglibs.jspf" %&gt; &lt;html&gt; &lt;head&gt;&lt;title&gt;Simple tools&lt;/title&gt;&lt;/head&gt; &lt;style&gt; .error s { color:#FF0000; } &lt;/style&gt; &lt;body&gt; &lt;%@ include file="/WEB-INF/jspf/nav/nav.jspf" %&gt; Errors: &lt;form:errors path="url" cssClass="error"/&gt; &lt;form:form method="post" commandName="urlForm"&gt; &lt;form:input path="url" /&gt; &lt;input type="submit" align="center" value="Execute" /&gt; &lt;/form:form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Here's the full source of my controller:</p> <pre><code>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; } } </code></pre> <p>All of the above produces the following HTML:</p> <pre><code>&lt;html&gt; &lt;head&gt;&lt;/head&gt; &lt;body&gt; &lt;div&gt;&lt;span&gt;&lt;a href="home.html"&gt;Home &lt;/a&gt;&lt;/span&gt;&lt;span&gt; | &lt;a href="url-cache.html"&gt;Page Mirror&lt;/a&gt;&lt;/span&gt;&lt;/div&gt; &lt;div id="breadcrumb"&gt;&lt;span&gt;Your trail:&lt;a href=""/&gt;&lt;/span&gt;&lt;/div&gt;Attrs:&lt;div/&gt; &lt;form id="urlForm" method="post" action="/home-web/url-cache.html"&gt; &lt;input id="url" type="text" value="" name="url"/&gt; &lt;input type="submit" align="center" value="Execute"/&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p><strike>I'm now overriding <code>doSubmitAction(Object command)</code> 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 <code>formBackingObject(HttpServletRequest request)</code> is called).</strike></p> <p><strike>That's to say, when I submit, the logging call on line 1 of <code>doSubmitAction</code> in the form controller is never executed.</strike> <strong>The validator executes, and fails (adds error messages correctly) because the value it's checking is always null <em>(or put correctly, it's never set)</em></strong>. The call to <code>formBackingObject</code> always occurs however. The request attribute of my form (the form input 'url') is always null. </p> <p><strong>Update:</strong> 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.</p> <p>Please help?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload