File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Spring and the fly likes How to persist form data across different pages Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "How to persist form data across different pages" Watch "How to persist form data across different pages" New topic
Author

How to persist form data across different pages

Faheem Farhan
Greenhorn

Joined: Aug 04, 2009
Posts: 2
Hi

I have a registration form (register.jsp) when submitted goes to
RegisterPersonController(extends SimpleFormController) --> RegisterPersonService --> RegisterPersonDAO
and saves the details to DB.

The successView is defined as registered.jsp

In registered.jsp I am displaying the congratulations message plus two links

1) View details in PDF
2) View details in Excel

I am capturing the form details from register.jsp into RegisterPersonController in the command object (person) and passing it to registered.jsp through modelAndView object, where i am able to display the details directly on the page .

But how do I pass this model or (command object) from registered.jsp to AbstractPdfView

For your ready reference I am pasting my code here and not attaching ( sorry for long post)

------------------------------------------------------------------------------------------------------------------------------------------------
register.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri = "http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="spring" uri="/spring" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form method="post" action="./registerPerson.htm">
<spring:bind path="person">
<c:forEach items="${status.errorMessages}" var = "errorMessage">
<c:out value="${errorMessage}" /> <br/>
</c:forEach>
</spring:bind>

<br/>

<spring:bind path="person.firstName">
FirstName : <input type="text" name = "<c:out value="${status.expression}" />"
value = "<c:out value = "${status.value}" />" />
<c:out value="${status.errorMessage}"></c:out>
</spring:bind>

<br/>

<spring:bind path="person.lastName">
LastName : <input type="text" name = "<c:out value="${status.expression}" />"
value = "<c:out value = "${status.value}" />" />
<c:out value="${status.errorMessage}"></c:out>
</spring:bind>

<br/>

<spring:bind path="person.password">
Password : <input type="password" name = "<c:out value="${status.expression}" />"
value = "<c:out value = "${status.value}" />" />
</spring:bind>

<br/>

<spring:bind path="person.email">
Email : <input type="text" name = "<c:out value="${status.expression}" />"
value = "<c:out value = "${status.value}" />" />
</spring:bind>

<br/>

<input type="submit" value="Register" />

</form>
</body>
</html>
------------------------------------------------------------------------------------------------------------------------------------------------
RegisterPersonController

package com.ff.controller;

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

import com.ff.beans.Person;
import com.ff.service.RegisterPersonService;

public class RegisterPersonController extends SimpleFormController{
private RegisterPersonService rps;

public RegisterPersonService getRps() {
return rps;
}

public void setRps(RegisterPersonService rps) {
this.rps = rps;
}

public RegisterPersonController(){
setCommandClass(com.ff.beans.Person.class);
setCommandName("person");
}

protected ModelAndView onSubmit(Object command , BindException errors) throws Exception{
Person p = (Person)command;
rps.addPerson(p);
return new ModelAndView("registered","personDetails",p);

}
}


------------------------------------------------------------------------------------------------------------------------------------------------
registered.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri = "http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>!!! Congratulations ...... You have registered successfully !!!</h1>

<a href="./displayDetailsInPDF.htm"> View details in PDF</a>
<a href="./displayDetailsInExcel.htm"> View details in Excel</a>

</body>
</html>
------------------------------------------------------------------------------------------------------------------------------------------------
DisplayDetailsInPdfController

package com.ff.controller;

import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractCommandController;

import com.ff.beans.Person;

public class DisplayDetailsInPDF extends AbstractCommandController {

public DisplayDetailsInPDF(){
setCommandClass(com.ff.beans.Person.class);
}
protected ModelAndView handle(HttpServletRequest arg0,
HttpServletResponse arg1, Object command, BindException arg3)
throws Exception {

Person p = (Person)command;
System.out.println("The value of p:" + p);
System.out.println("firstname : "+p.getFirstName());
TreeMap map = new TreeMap();
map.put("personDetails", p);
return new ModelAndView(new DisplayDetailsInPDFView(),map);
}
}

------------------------------------------------------------------------------------------------------------------------------------------------
DisplayDetailsInPDFView

public class DisplayDetailsInPDFView extends AbstractPdfView {

protected void buildPdfDocument(Map model, Document doc, PdfWriter writer,
HttpServletRequest request, HttpServletResponse response) throws Exception {

Person p = (Person)model.get("personDetatils");
Chunk heading = new Chunk("Person Details");
heading.setBackground(Color.RED);
heading.setUnderline(3f, -3f);
doc.add(heading);
Paragraph para = new Paragraph();
if(p!=null){

para.add(p.getFirstName());
para.add("\n");
para.add(p.getLastName());
para.add("\n");
para.add(p.getPassword());
para.add("\n");
para.add(p.getEmail());


}else{
para.add("Person details not available");
}
doc.add(para);

}

}

------------------------------------------------------------------------------------------------------------------------------------------------
Faheem Farhan
Greenhorn

Joined: Aug 04, 2009
Posts: 2
No replies .......may be the length of the question is the cause.
ok

How to read the form content (data) in the next to next page(Page3) ?

Page1 (Form) ---> Page2 ---> Page3.
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
Have you tried AbstractWizardFormController?


SCJA 1.0, SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJP 5.0, SCEA 5, SCBCD 5; OCUP - Fundamental, Intermediate and Advanced; IBM Certified Solution Designer - OOAD, vUML 2; SpringSource Certified Spring Professional
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

You can -
  • Put the data in the session
  • Pass the data to the new controller through a request parameter.
  • Use AbstractWizardFormController
  • Use SpringWebFlow.


  • -Nate
    Write once, run anywhere, because there's nowhere to hide! - /. A.C.
    Hong Anderson
    Ranch Hand

    Joined: Jul 05, 2005
    Posts: 1936
    As Nathan suggested there are many ways.
    But from your requirement, Spring Web Flow is overkill, you can just use AbstractWizardFormController.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: How to persist form data across different pages