Hi all,
I'm new to Spring MVC, but not new to web development in Java. I'm attempting to create a simple form->controller example.
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?
Form controller spring configuration:
<?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>
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;
}
}
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>
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).
That's to say, when I submit, the logging call on line 1 of 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 doSubmitAction
in the form controller is never executed.formBackingObject
always occurs however. The request attribute of my form (the form input 'url') is always null.
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.
Please help?