PDA

View Full Version : SimpleFormController


rlynn
Jul 27th, 2005, 11:12 AM
I have a form and a SimpleFormController, and the values I enter are not getting to the controller.


The controller:


public DocumentFormController() {

setCommandName("doc");
setSessionForm(true);
}
/**
*
*/
public ModelAndView onSubmit(Object command, BindException errors)
throws ServletException
{
NewDocument doc = (NewDocument) command;

[b] //The following line has empty string for the title and not the title entered[/b]:
logger.info("doc TITLE is " + doc.getTitle() );

...

return new ModelAndView(new RedirectView(getSuccessView()));
} ...


The xml:


<!-- Validator and Form Controller for the "New Document" page -->
<bean id="createDocForm" class="com.myApp.web.DocumentFormController">
<property name="sessionForm"><value>true</value></property>
<property name="commandName"><value>doc</value></property>
<property name="commandClass"><value>com.myApp.business.NewDocument</value></property>
<property name="formView"><value>createdoc</value></property>
<property name="successView"><value>inbox.htm</value></property>
<property name="documentManager">
<ref bean="docMan"/>
</property>
<property name="userManager">
<ref bean="userMan"/>
</property>
</bean>


The jsp :

<%@ page session="false"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="spring" uri="/spring" %>

...

<form method="post" enctype="multipart/form-data" name="newdocument">
<table>

<tr>
<th>
Title
</th>
<td><spring:bind path="doc.title"><input type="text" name="title" size="40" maxlength="255" value="<c:out value="${status.value}"/>" /></spring:bind></td>
</tr>
<tr>
<th>
Purpose
</th>
<td><spring:bind path="doc.purpose"><textarea name="purpose" rows="6" cols="40"><c:out value="${status.value}"/></textarea></spring:bind></td>
</tr>
<tr>
<th>

</th>
<td><input type="submit" name="submit" value="Save" />
<input type="button" name="cancel" value="Cancel" onclick="history.back()" /></td>
</tr>
</table>

</form>


And the command class:


public class NewDocument
{
private String title;
private int statusId;
private int id;
private String purpose;
private int ownerId;
private User owner;

/**
*
* @param i
*/
public void setId(int i) {
id = i;
}

/**
*
* @return int
*/
public int getId() {
return id;
}

/**
*
* @param s
*/
public void setTitle(String s)
{
title = s;
}

/**
*
* @return String
*/
public String getTitle()
{
return title;
}

/**
*
* @param s
*/
public void setStatusId(int id)
{
statusId = id;
}

/**
*
* @return int
*/
public int getStatusId()
{
return statusId;
}

/**
*
* @param s
*/
public void setPurpose(String s)
{
purpose = s;
}

/**
*
* @return String
*/
public String getPurpose()
{
return purpose;
}


/**
*
* @param User
*/
public void setOwner(User u)
{
owner = u;
}

/**
*
* @return User
*/
public User getOwner()
{
return owner;
}

/**
* (delete??)
* @param s
*/
public void setOwnerId(int id)
{
ownerId = id;
}

/**
* (delete??)
* @return int
*/
public int getOwnerId()
{
return ownerId;
}
}


What is wrong with the above? I have implemented one other form and it works fine.

Thanks!

rlynn
Jul 27th, 2005, 03:45 PM
Nevermind, it's working now. I think changing the enctype for the form is what did it (I haven't worked with forms enough to even know what that field is for!)

katentim
Jul 27th, 2005, 06:13 PM
I haven't worked with forms enough to even know what that field (enctype) is for!
You don't need to specify it unless you are doing a file upload.

rlynn
Jul 28th, 2005, 07:31 AM
Actually there is a file upload on the page. I haven't tried uploading anything yet though.

Colin Yates
Jul 28th, 2005, 07:48 AM
You will need to tell spring to use the multi-part upload hander.

I do it by defining a filter in web.xml: <filter>
<filter-name>multipartFilter</filter-name>
<filter-class>org.springframework.web.multipart.support.Multipar tFilter</filter-class>
</filter>
but there are lots of other ways ;)

rlynn
Jul 28th, 2005, 09:54 AM
Thanks! I'm going to need that.

katentim
Jul 28th, 2005, 10:09 PM
There's an example of file uploads in the imagedb sample.