wood burning stoves 2.0*
The moose likes Spring and the fly likes Regarding onSubmit method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Regarding onSubmit method" Watch "Regarding onSubmit method" New topic
Author

Regarding onSubmit method

Hakan Axheim
Greenhorn

Joined: Nov 24, 2008
Posts: 9
Hello!

I am using Spring 2.5.x to develop a small Application.
The application retreives a number of records from a database with Hibernate.
This records are shown in a web page with a link to another web page where it should be able to change a specific record.
When I klick on the link to change a specific record a new page is shown with the specific record.
In this page I change data for a specific field and click on a Submit button.
My controller (SimpleFormController) will be executed and the formbackingobject method will be executed.
After that onSubmit method will be executed but it the new entered data will not be sent to onSubmit method.
Instead the old data will be send to this method.

Why?

Best regards

/Håkan


Hakan Axheim
Greenhorn

Joined: Nov 24, 2008
Posts: 9
UpdateController.java:

package BL.controller;

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


import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.bind.RequestUtils;
import org.springframework.validation.BindException;

import BL.Project;
import BL.service.ProjectServiceImpl;
import IL.util.DAOException;

public class UpdateProjectController extends SimpleFormController {

// private static final Logger logger = Logger.getLogger(UpdateProjectController.class.getName());
ProjectServiceImpl projectServiceImpl = new ProjectServiceImpl();

public UpdateProjectController() {
logger.info("Current: Inne i UpdateProjectController --> Constructor");
setCommandClass(Project.class);
setCommandName("ProjectForm");
setSessionForm(true);
//setSuccessView("studentSuccess");
logger.info("Current: Constructor slutförd");
}

@Override
protected Object formBackingObject(HttpServletRequest request) throws ServletException {
logger.info("Current: Inne i UpdateProjectController --> formBackingObject ");

logger.info("Current: Project ID " + String.valueOf(RequestUtils.getIntParameter(request,"projectId")));

Project project = new Project();
project = projectServiceImpl.getAllProjects(RequestUtils.getIntParameter(request,"projectId"));

logger.info("Current: Projektnamn " + project.getProjectName());

return projectServiceImpl.getAllProjects(RequestUtils.getIntParameter(request,"projectId"));

}

@Override
public ModelAndView onSubmit(Object command) throws ServletException
{
logger.info("Current: Bekräfta");

Project projectCommand = (Project) command;

logger.info("Current: " + projectCommand.getProjectId());
logger.info("Current: " + projectCommand.getProjectNumber());
logger.info("Current: " + projectCommand.getProjectName());
logger.info("Current: " + projectCommand.getStartDate());
logger.info("Current: " + projectCommand.getEndDate());

try {
projectServiceImpl.updateProject(projectCommand);
} catch (DAOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
return new ModelAndView("readProject","project",projectCommand);

/*
logger.info("Current: Projektnamn " + project.getProjectName());

Project commandName = (Project) command;
project.setProjectName(commandName.getProjectName());
project.setStartDate(commandName.getStartDate());
project.setEndDate(commandName.getEndDate());

logger.info("Current: ProjectId " + String.valueOf(project.getProjectId()));
logger.info("Current: ProjectNr " + project.getProjectNumber());
logger.info("Current: ProjectNamn " + project.getProjectName());
logger.info("Current: StartDate " + project.getStartDate());
logger.info("Current: EndDate " + project.getEndDate());


try {
projectServiceImpl.updateProject(project);
} catch (DAOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}




return new ModelAndView("readProject","project",project);
*/
}

}
Hakan Axheim
Greenhorn

Joined: Nov 24, 2008
Posts: 9
JSP file for updating a record:
Tommy Delson
Ranch Hand

Joined: Apr 13, 2011
Posts: 206
Simply you didn't do it correctly on the next step, there are a lot of work that you need to do here.

1. Put "projectId" in the session.
2. Passing "projectId" in the jsp page that you have the data to edit.
3. Create a new controller to handle the edit process
4. Whatever the next step you like to do from here...

Hope it help....


OCPJP6-05-11
"Your life is in your hands, to make of it what you choose."
Hakan Axheim
Greenhorn

Joined: Nov 24, 2008
Posts: 9
Well the source code I give to you is the Controller that should handle the update.
The JSP page I have posted is the update page - this JSP page should handle the update.
I'm a newbie at Spring therefore my questions.

When I cklick on the Submit button on the web page (see JSP page in this thread) the onSubmit method will be called but the new entered value will not be sent to that method!
I don't know what you mean by putting projectID in the session.

When the JSP page listed in this thread (updateProject.jsp) will be called it receives a projectID and the formBackingObject method will be called.

/Håkan
Tommy Delson
Ranch Hand

Joined: Apr 13, 2011
Posts: 206
I see your update controller code, it's not done correctly. You need a good reference or tutorial to walk you through here...

Check this out: http://static.springsource.org/docs/Spring-MVC-step-by-step/index.html

Basically you need to do several things in order to get an updated data on the next page.

1. Passing "projectId" in param or any way you know how to the request in your first page.

(This is where you've missed)
2. Put "projectId" in the session in the controller where it handle the submit form and set all updated value on the object you want to update

3. Retrieve "projectId" in your update controller and retrieve all the value from an object where you have saved set earlier, and set to the instance variables.

4. On your JSP update page retrieve all updated value that you set in an instance variables.

5. On submit you're done.
Tommy Delson
Ranch Hand

Joined: Apr 13, 2011
Posts: 206
I see your update controller code, it's not done correctly. You need a good reference or tutorial to walk you through here...

Check this out: http://static.springsource.org/docs/Spring-MVC-step-by-step/index.html

Basically you need to do several things in order to get an updated data on the next page.

1. Passing "projectId" in param or any way you know how to the request in your first page.

(This is where you've missed)
2. Put "projectId" in the session in the controller where it handle the submit form and set all updated value on the object you want to update

3. Retrieve "projectId" in your update controller and retrieve all the value from an object where you have saved set earlier, and set to the instance variables. Next pass it or set to an object you want to update.

4. On your JSP update page retrieve all updated value from an object that you set in an instance variables.

5. On submit you're done.


Hope it help...
Hakan Axheim
Greenhorn

Joined: Nov 24, 2008
Posts: 9
I have solved this problem. I had different versions of spring.jar and spring-web.jar.
Everything works as expected!

/Håkan
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regarding onSubmit method
 
Similar Threads
code for modification
pagination in jsp
Performance Problem?
Rereshing after modification / Listing
How to make a MVC update control