[英]Spring MVC - Variables between pages, and Unsetting a SessionAttribute
The question sounds weird, I'm playing around with Spring MVC and am trying to move between two pages and basically I'm creating a JSP page using Spring Form JSTL's so it just uses a POST, and I use a controller to move from one页到下一页。 但是模型从一页到另一页都丢失了,我想隐藏实际变量,所以 QueryStrings 是不可能的(据我所知)。 我知道我可以使用 InternalResourceView,但只允许我使用 model。
我想传输一个该页面独有的变量,没有 model 或使用 QueryStrings 的最佳方法是什么?
我计划使用 SessionAttribute 轻松定义它们,但想知道如何删除 SessionAttribute 创建的变量? 我尝试了 HttpSession.removeAttribute ,但它似乎没有用。
您还可以像这样使用 SessionStatus.setComplete() :
@RequestMapping(method = RequestMethod.GET, value="/clear")
public ModelAndView clear(SessionStatus status, ModelMap model, HttpServletRequest request) {
model.clear();
status.setComplete();
return new ModelAndView("somePage");
}
或 DefaultSessionAttributeStore.cleanUpAttribute 像这样:
@RequestMapping(method = RequestMethod.GET, value="/clear")
public ModelAndView clear(DefaultSessionAttributeStore status, WebRequest request, ModelMap model) {
model.remove("mySessionVar");
status.cleanupAttribute(request, "mySessionVar");
return new ModelAndView("somePage");
}
我在其中一个具有多个 sessionAttributes 的 forms 上使用它,我只想删除其中一个。
您可以使用 HttpSession class 中的removeAttribute方法。
您可以使用与 Spring @SessionAttributes
一起使用的WebRequest.removeAttribute(String name, int scope)
。 引用@SessionAttributes
javadoc - “或者,考虑使用通用 {@link org.springframework.web.context.request.WebRequest} 接口的属性管理功能。”
也看看我的例子。
@Controller
@SessionAttributes({"sessionAttr"})
public class MyController {
@ModelAttribute("sessionAttr")
public Object defaultSessionAttr() {
return new Object();
}
@RequestMapping(value = "...", method = RequestMethod.GET)
public String removeSessionAttr(WebRequest request, Model model) {
request.removeAttribute("sessionAttr", WebRequest.SCOPE_SESSION);
model.addAttribute("sessionAttr", defaultSessionAttr());
return "myView";
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.