Java 从视图返回时,会话属性返回null
我目前正在尝试将搜索条件属性保存到两个页面之间的会话:搜索页面和编辑页面。目标是保存三个变量:sYear、submission、collectionPeriod。我正在搜索屏幕控制器中将这些添加到下面的会话中:Java 从视图返回时,会话属性返回null,java,spring,jsp,spring-mvc,Java,Spring,Jsp,Spring Mvc,我目前正在尝试将搜索条件属性保存到两个页面之间的会话:搜索页面和编辑页面。目标是保存三个变量:sYear、submission、collectionPeriod。我正在搜索屏幕控制器中将这些添加到下面的会话中: request.getSession().setAttribute(“sYearSave”,sYear); request.getSession().setAttribute(“submissionSave”,submission); request.getSession().setAt
request.getSession().setAttribute(“sYearSave”,sYear);
request.getSession().setAttribute(“submissionSave”,submission);
request.getSession().setAttribute(“collectionPeriodSave”,collectionPeriod)代码>
在编辑屏幕控制器中,我将布尔值isFromEditScreen
设置为true。
这样我就知道我来自编辑屏幕。我确实打印出了变量,并且在编辑控制器屏幕中正确地得到了值
request.getSession().setAttribute("isFromEditScreen", new Boolean(true));
sYearSave = (String)request.getSession().getAttribute("sYearSave");
collectionPeriodSave = (String)request.getSession().getAttribute("collectionPeriodSave");
submissionSave = (String)request.getSession().getAttribute("submissionSave");
但问题是,当我使用后退按钮返回搜索屏幕时,搜索条件sYearSave、collectionPeriodSave和submissionSave
值返回NULL。
出于某种原因,isFromEditScreen
布尔值工作正常,返回true。
它实际上输入了语句,但搜索条件返回null。
搜索控制器代码如下:
if (isFromEditScreen != null && isFromEditScreen == true) {
System.out.println("Inside isFromEditScreen ==== true");
sYear = (String)request.getSession().getAttribute("sYearSave");
collectionPeriod = (String)request.getSession().getAttribute("collectionPeriodSave");
submission = (String)request.getSession().getAttribute("submissionSave");
sYearSave = (String)request.getSession().getAttribute("sYearSave");
collectionPeriodSave = (String)request.getSession().getAttribute("collectionPeriodSave");
submissionSave = (String)request.getSession().getAttribute("submissionSave");
System.out.println("sYearSave ==== " + sYearSave);
System.out.println("submissionSave ==== " + submissionSave);
System.out.println("collectionPeriodSave ==== " + collectionPeriodSave);
System.out.println("isFromEditScreen set in else ==== " + isFromEditScreen);
}
任何帮助都将不胜感激 如果您使用的是SpringMVC(如问号所示),为什么不尝试使用SessionAttributes注释并使用Spring中的ModelAndView API呢?还要确保属性名称在整个应用程序中是唯一的
@RequestMapping("view-name")
@SessionAttributes( { "isFromEditScreen" })
public class YourController {
...
@RequestMapping
public ModelAndView display() {
ModelAndView modelAndView = new ModelAndView("view-name");
modelAndView.addObject("isFromEditScreen", new Boolean(true));
return modelAndView;
}
...
}
我的会话属性被覆盖。忽略错误。当您使用浏览器的“后退”按钮时,它不会向服务器生成请求。浏览器通常从缓存加载页面,您是否尝试过此wia链接按钮或类似的方法?并且您确定您输入会话的值不是null
?因为,如果返回到搜索屏幕并使用“空白”URL重新加载页面(即,没有给定参数),最终可能会覆盖会话的值,具体取决于搜索控制器的实现。除此之外,你有没有想过使用标签浏览?一个会话、两个选项卡、许多问题……您应该避免从会话
传递搜索参数。您应该使用搜索参数。@rusty我在页面上有一个返回的按钮,可以重新加载页面;该按钮不使用历史记录。返回()@我确定是skirsch,因为在我的编辑控制器中,搜索值可以很好地打印出来。我正在查看代码,在这里看不到任何重写。我到处都在做印刷品。不,我还没有想到标签浏览。这没什么大不了的。我们不是用这种方法来构建控制器的。我希望我们已经开始使用这种方法的项目,因为它似乎更有组织性和多功能性。