aspose file tools*
The moose likes Struts and the fly likes How to store a value so that any JSP page can read it? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How to store a value so that any JSP page can read it?" Watch "How to store a value so that any JSP page can read it?" New topic
Author

How to store a value so that any JSP page can read it?

Nick R Wilson
Greenhorn

Joined: Jun 30, 2009
Posts: 2
I’m writing a web application using Struts 1.1. One of the initial web pages displays a grid of rows, loaded from a database table. The primary key of this table is not shown as part of the grid; instead it is displayed as a drop-down box so that only the rows for the selected key will be displayed.

What I want to do is to remember which value has been selected by the user, when they select a different drop-down value, and then choose to go to another page. This primary key is shared by several other tables, and I want it to default to display the corresponding rows with the same selected key, when the user moves to another JSP page.

On the web pages, this drop down box is stored within a form and is changed when the user clicks on the ‘Change’ button. What I need to know is where to store the value, so that it can later be retrieved by a different JSP page. Note that the web page that fails to find the String is not the next page in succession; the selected JSP page is one of several and is chosen via some JavaScript that choose which action to take, e.g.:
<li><a href="#"
onmouseover="mopen('m4')"
onmouseout="mclosetime()">Control Types</a>
<div id="m4"
onmouseover="mcancelclosetime()"
onmouseout="mclosetime()">
<html:link forward="AddCTypeNew">Add Control Type</html:link>
<html:link forward="ListCType">List Control Types</html:link>
</div>
</li>

The code I am using to display the drop down box in the JSP pages is as follows:

<%
String controllerID = (String) System.getProperty("controllerID");
String valSelect = null;
%>
<form action="/GameControls/listBCode.do">
<select name="controllerID">
<c:forEach items="${cTypes}" var="cType">
<c:choose>
<c:when test='${cType.controllerID == controllerID}'>
<c:set var="valSelect" scope="page" value="selected=true" />
</c:when>
<c:otherwise>
<c:set var="valSelect" scope="page" value="" />
</c:otherwise>
</c:choose>
<option <c:out value='${valSelect}'></c:out> value='<c:out value='${cType.controllerID}'></c:out>'>
<c:out value="${cType.controllerDescription}"></c:out></option>
</c:forEach>
</select> <input type="submit" value="Change"></form>


My problem is in storing the String ‘controllerID’. I have tried storing is as a parameter, an attribute, and, in this example, as a System.getProperty. The above code successfully reads from a List of cType objects. But the check as to which value needs the “selected=true” attribute always fails because the controllerID String is always null.

In my struts-config.xml file, here is an example of an action that is called, that tries to retrieve the controllerID String:

<action path="/addBCodeNew" type="filemaint.AddButtonCodePreAction"
name="cTypeForm" attribute="cType" validate="false" scope="session">
<forward name="success" path="/WEB-INF/pages/tiles/buttonCodeAddTiles.jsp" />
</action>

I have tried both ‘session’ and ‘request’ as values for the scope attribute, but neither has worked.

I’m not sure what else to illustrate in regards to my problem. If I’ve missed any important information about my configuration, I’ll happily supply it.
My bottom line questions are:
Why is my controllerID String always null? What is the correct way to store a value submitted to a form, so that any other JSP page can successfully retrieve it?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61648
    
  67

Please be sure to ask Struts questions in the Struts forum. I have moved this post there for you.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

You have to save the value in the action, not on the JSP page.
 
 
subject: How to store a value so that any JSP page can read it?