File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes beginners struts question - scope request Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "beginners struts question - scope request" Watch "beginners struts question - scope request" New topic
Author

beginners struts question - scope request

Ss Wooly
Greenhorn

Joined: Aug 07, 2003
Posts: 2
Please can somebody help me out with this query, I'm sure it is something obvious. I would like to have an ActionForm which is used by say three pages, when I move from page one to two, the values are remembered and when I move to page two to three the values are again remembered. I thought, by setting the scope to 'request', this should be happening, however it does not seem to remember the values. Please see the test example below.
I would like to go to firstStage.jsp showing 'FIRST STAGE' and the First stage title, then on to the secondStage.jsp showing 'SECOND STAGE' and the Second stage title and then on to the thirdStage.jsp showing 'THIRD STAGE and the Third stage title, etc... So the information is kept throughout the entire request and passed through. Any help would be appreciated
WEB.XML:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.0//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_0.dtd">
<struts-config>
<!-- ========== Form Bean Definitions ================= -->
<form-beans>
<form-bean name="testForm"
type="test.TestForm"/>

</form-beans>
<!-- ========== Action Mapping Definitions ============ -->
<action-mappings>
<action path="/submit"
type="test.TestAction"
name="testForm"
input="/submit.jsp"
scope="request">
<forward name="firstStage" path="/firstStage.jsp"/>
<forward name="secondStage" path="/secondStage.jsp"/>
<forward name="thirdStage" path="/thirdStage.jsp"/>
</action>
</action-mappings>
</struts-config>
Initial page - submit.jsp:
<%@ page language="java" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<html>
<head><title>Submit example</title></head>
<body>
<h3>Example Submit Page</h3>
<html:errors/>
<html:form action="submit.do">
<html:submit/>
</html:form>
</body>
</html>
First page: firstStage.jsp
<%@ page import="java.util.*,test.TestForm"%>
<%@ page language="java" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<html>
<head><title>Submit example</title></head>
<body>
<bean efine id="formData0" name="testForm" scope="request"/>
<%
TestForm testData = (TestForm)formData0;
%>
<h3>First Stage Page</h3>
<html:errors/>
<html:form action="submit.do">
<html:submit/>
</html:form>

Hello
<%=" ["+testData.getName()+"]"%>

</body>
</html>
second and third pages here are the same, apart from the heading in this example... just to ensure flow is correct (or not as it is at the moment).
TestForm.java
package test;
import org.apache.struts.action.ActionForm;
public class TestForm extends ActionForm{
private int processStage = Constants.FIRST_STAGE;
private String name;
public int getProcessStage() {
return processStage;
}
public void setProcessStage(int processStage) {
this.processStage = processStage;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
TestAction.java
package test;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.io.IOException;
public class TestAction extends Action {
public ActionForward perform(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
TestForm testForm;
if (form == null){
testForm = new TestForm();
}
else {
testForm = (TestForm)form;
}
if (testForm.getProcessStage() == Constants.FIRST_STAGE){
testForm.setName("FIRST STAGE ");
testForm.setProcessStage(Constants.SECOND_STAGE);
return mapping.findForward("firstStage");
}
else if (testForm.getProcessStage() == Constants.SECOND_STAGE){
testForm.setName("SECOND STAGE");
testForm.setProcessStage(Constants.THIRD_STAGE);
return mapping.findForward("secondStage");
}
else {
testForm.setName("THIRD STAGE");
return mapping.findForward("thirdStage");
}
}
}

Constants.java
package test;
public class Constants {
public static int FIRST_STAGE = 1;
public static int SECOND_STAGE = 2;
public static int THIRD_STAGE = 3;
}
:roll:
[ August 07, 2003: Message edited by: Ss Wooly ]
Michal Bienek
Greenhorn

Joined: Jun 17, 2002
Posts: 29
By submitting your page, you are in fact making a POST which will invalidate the request. To keep everything in the same request you need to ensure that you are performing a GET to the action.
Additionally, and this is far cleaner, to keep information alive between pages, you should actually put it into the session (scope=session, or request.getSession().setAttribute(...,...)).
You also never seem to actually add your form to the request as an attribute, so unless your first jsp page actually managed to submit a TestForm object, it'll never find its way into the request scope.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: beginners struts question - scope request