我正在尝试实现一种客户端分页,一次只显示 X 条记录,然后当客户端想要查看更多数据时,我会显示下一条 X 条记录,依此类推。为此,我尝试使用会话变量,但每次检查它的值时它都是空的。我真的不太了解 Spring MVC,所以任何帮助将不胜感激:
@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 "";
}
在这里我检查值是否为空,这是我初始化它的值,然后设置值:
@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());
}
}
每次我检查会话变量时,它总是“”,会话变量的生存时间是多久?