使用从一个jsp页面到另一个超出范围的jsp页面的变量

时间:2016-05-09 16:30:29

标签: java spring jsp

我想使用在一个jsp文件中定义的变量,但是我要在其中使用此变量的文件是在一些超出范围的文件夹中?我该怎么做?页面导入是否适用于这种情况?

2 个答案:

答案 0 :(得分:0)

您可以使用会话存储变量,然后将其从其他jsp中恢复为@SURESH ATTA建议。如果管理不好,可能会导致在会话中存储大量变量而不需要。

您可以使用包含params的页面包含:

<jsp:include page="otherjsp.jsp" >
    <jsp:param name="param1" value="value" />
</jsp:include>

然后在你的otherjsp.jsp上,你可以从请求中获取参数。

答案 1 :(得分:0)

您可以在此会话中存储值。您可以使用像用户这样的密钥在会话中举例说明用户。如果你想要一辆你可以使用的汽车,你可以把你想要的东西放在一个会话中:

session.setAttribute("Car", car);

用户代码:

FacesContext context = FacesContext.getCurrentInstance();

HttpSession session = (HttpSession) context.getExternalContext().getSession(true);

session.setAttribute("User", logedinUser);

当你完成这个操作后,你可以使用你设置了varibale的键调用getAttribute来调用这个值,在这个例子中它是“User”

FacesContext context = FacesContext.getCurrentInstance();

HttpSession session = (HttpSession) context.getExternalContext().getSession(true);

session.getAttribute("User");