PDA

View Full Version : why I never reach successView?


vincent
Oct 10th, 2005, 08:44 AM
Hi, all

I am doing a test on SimpleFormController but never reach defined successView. After submit the form, I was directed to formView immediately.
Here is what I do:

--- dispatcher-servlet.xml ---

<bean name="/signonForm.form" class="signonFormController">
<property name="sessionForm" value="true"/>
<property name="commandClass" value="hashCommand"/>
<property name="formView" value="signonForm"/>
<property name="successView" value="welcome"/>
</bean>

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

--- signonForm.jsp ---


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

<html>
<head>
<title>Sign on</title>
</head>
<body>
<center>
<h2>Spring Framework (SimpleFormController)</h2>
<form method=get>
<table>
<spring:bind path="command.formData['username']">
<tr><td>ID:</td><td><input type=text name=username size=15 value="<c:out value="${status.value}"/>"</td></tr>
</spring:bind>
<tr><td>Password:</td><td><input type=password name=password size=15></td></tr>
<tr><td colspan=2 align=center><input type=submit value=SingOn></td></tr>
</table>
</form>
<spring:hasBindErrors name="command">
<b>Binding error!</b>
</spring:hasBindErrors>
</center>
</body>
</html>

--- signonFormController.java ---


import org.springframework.validation.*;
import org.springframework.web.servlet.*;
import org.springframework.web.servlet.mvc.*;

import javax.servlet.*;
import javax.servlet.http.*;

import org.apache.log4j.*;

import java.util.*;

public class signonFormController extends SimpleFormController {

Logger log;
String logfile="logs/myspring.log";

public signonFormController() throws Exception {

log=Logger.getLogger("myspring");
log.addAppender(new FileAppender(new PatternLayout("%d %p: %m"),logfile));

}

protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception {

log.debug("signonFormController.java onSubmit()");

return new ModelAndView("signonForm");

}

protected Object formBackingObject(HttpServletRequest request) {

HashMap prevInput=new HashMap();

if ( request.getParameter("username") != "" ) {
prevInput.put("username",request.getParameter("username"));
}
if ( request.getParameter("password") != "" ) {
prevInput.put("password",request.getParameter("password"));
}

hashCommand prevCommand=new hashCommand();
prevCommand.setFormData(prevInput);

return prevCommand;

}

}


Any idea? Do I need a validator class? It takes me several days and still no progress.


Thanks,

Vincent Chen

ghooghe
Oct 10th, 2005, 09:30 AM
You forgot to tell your HTML page where to submit :
your HTML code :
<form method=get action=>
should look like this :
<form method=get action=signonForm.form>

regards

vincent
Oct 10th, 2005, 10:45 AM
Hi,

Thanks for your reply. Is 'action=xxx' in form tag necessary? I saw several examples including 'Developing a Spring Framework MVC application step-by-step', they don't have action attribute in form tag. I thought SimpleFormController will take care this.

vincent
Oct 10th, 2005, 11:30 AM
Finally solved!

Quote from isFormSubmission method:

Determine if the given request represents a form submission.

Default implementation treats a POST request as form submission. Note: If the form session attribute doesn't exist when using session form mode, the request is always treated as new form by handleRequestInternal.


I am using get method in html form and not override this method. My bad, still a lot to learn.


Thanks,

Vincent Chen