I am trying to implement a type of client-side paging, where I only display X records at a time, then when the client wants to see more data I display the next X records and so forth. To do this I am trying to use a session variable, but every time I examine its value it is empty. I don’t really know all that much about Spring MVC so any help would be appreciated:
@Controller
@RequestMapping(value = "/api/rest/da")
@SessionAttributes({"sessionRowKey"})
public class DAController {
/**
* Default constructor for DAController
*/
public DAController() {
}
/**
* Initialize the SessionAttribute 'sessionRowKey' if it does not exist
*/
@ModelAttribute("sessionRowKey")
public String createSessionRowKey()
{
return "";
}
Here I check if the value is empty, which is what I initialize it to, then set the value:
@RequestMapping(value = "/getModelData/{namespace}/{type}/{rowkey:[^\\.]*\\.[^\\.]*}", method = RequestMethod.GET)
public
@ResponseBody
Map<String, Map<String, String>> getModelData(String namespace,
String type,
String rowkey,
@ModelAttribute("sessionRowKey") String sessionRowKey,
HttpServletRequest request)
{
try
{
ModelType modelType = ModelType.fromString(type);
Model model;
if(modelType == ModelType.STATISTICAL) //page the data
{
//code abstracted
List<KeyValue> records = results_arr[30].list();
if(sessionRowKey.equals(""))
{
model = modelReader.readModel(namespace, modelType, rowkey);
request.getSession().setAttribute("sessionRowKey", records.get(0).toString());
}
else model = modelReader.readModel(namespace, modelType, sessionRowKey);
}
else
{
model = modelReader.readModel(namespace, modelType, rowkey);
}
}
catch (Exception e)
{
logger.log(Level.ERROR, "Error in DAController.getModelData: " + e.getMessage());
}
}
Every time that I examine the session variable it is always “”, How long does the session variable live?
Instead of the @ModelAttribute annotated parameter (sessionRowKey), use an HttpSession parameter and get the sessionRowKey using this parameter. For example:
Note: the above is for Java EE 5 and above. For J2EE 1.4 and before use the HttpSession.getValue and HttpSession.setValue methods.