如何将参数传递给jsp:include通过c:set? JSP中变量​​的范围是什么? [英] How to pass parameter to jsp:include via c:set? What are the scopes of the variables in JSP?

查看:278
本文介绍了如何将参数传递给jsp:include通过c:set? JSP中变量​​的范围是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在welcome.jsp

I have this on welcome.jsp

<c:set var="pgTitle" value="Welcome"/>
<jsp:include page="/jsp/inc/head.jsp" />

这在head.jsp中:

And this in head.jsp:

<title>Site Name - ${pgTitle}</title>

但该变量为空白,输出仅为

But the variable is blank, and the output is merely

Site Name -

我看过很多文章,我无法弄清楚问题是什么。如果我在同一个welcome.jsp中的其他地方回显 $ {pgTitle} ,那么它输出正常。

I have read many articles, and I cannot figure out what the problem is. If I echo ${pgTitle} elsewhere within the same welcome.jsp, then it outputs fine.

我是包括两个页面上的核心标记库。

I am including the core tag library on both pages.

推荐答案

这是因为 pgTitle 变量在页面范围中设置。请查看此处(抱歉,我无法获取此官方文档)。

This is because the pgTitle variable is set in page scope. Check it here(sorry I can't get an official documentation for this).

如果要使其工作,则必须至少在请求范围内设置变量。要在请求范围中设置变量,请使用上的范围属性< c:set>

If you want to make this work, you have to set the variable in request scope at least. To set your variable in request scope, use the scope attribute on <c:set>:

<c:set var="pgTitle" value="Welcome" scope="request" />






根据您的评论,在网站开发中,范围变量很重要,因为它定义了变量的使用位置(类似于在类中声明为字段的变量和在方法中本地声明的变量)。 JSP中有四个范围称为上下文:


Per your comment, in web development, the scope of the variables matter because it defines where the variable can be used (similar to a variable declared as field in a class and a variable declared locally in a method). There are four scopes in JSP known as context:


  • 页面范围(由 PageContext中)。只有在当前页面中设置为属性时才能访问变量。这意味着,只有当前页面可以访问这些属性,包含的页面是不同的页面,因此它们无法访问这些属性。

  • 请求范围(由ServletRequest )。只有在当前请求中设置为属性时才能访问变量。这意味着,在同一请求中处理的每个页面都可以访问这些属性。 重要提示:重定向意味着新的请求/响应流程。这意味着,如果您在请求上设置属性并执行重定向,则这些属性将不会在新请求中设置为属性。

  • 会话范围(由HttpSession )。只有在当前用户会话中设置为属性时才能访问变量。这意味着,在同一用户会话中使用的每个页面都可以使用这些属性,直到它们被删除或会话到期为止。

  • 应用程序范围(由 ServletContext的)。只有在当前上下文中设置为属性时才能达到变量。这意味着,每个会话属性上的每个页面都可以访问这些变量,直到从SessionContext中删除它们或取消部署Web应用程序。

  • Page scope (handled by PageContext). The variables can only be reached if set as attributes in the current page. This means, only current page can access these attributes, included pages are different pages, so they can't access these attributes.
  • Request scope (handled by ServletRequest). The variables can only be reached if set as attributes in the current request. This means, every page handled in the same request can access to these attributes. Important Note: A redirect implies a new request/response process. This means, if you set attributes on the request and execute a redirect, these attributes won't be set as attributes on the new request.
  • Session scope (handled by HttpSession). The variables can only be reached if set as attributes in the current user session. This means, every page used in the same user session can use these attributes until they are removed or the session expires.
  • Application scope (handled by ServletContext). The variables can only be reached if set as attributes in the current context. This means, every page on every session attribute can access to these variables until they are removed from SessionContext or the web application is undeployed.

更多信息:

  • What are the different scopes in JSP?

这是否是正确的方法我想做什么?

Is this the right way to accomplish what I am trying to do?

如果有一个Servlet或其他控制器处理要在请求中设置的属性(例如<来自Spring MVC或JSF托管bean的code> @Controller ,然后在那里设置属性而不是直接在你的页面中。

If there's a Servlet or another controller that handles the attributes to be set in the request (e.g. @Controller from Spring MVC or JSF managed bean), then set the attribute there and not in your page directly.

就个人而言,在Web应用程序上使用时,需要一些时间来获得经验并定义变量的最佳范围。基本示例:

Personally, it takes some time to earn experience and define the best scope of the variables when used on web applications. Basic examples:


  • 出于演示目的,用逗号分割 String 将影响仅限当前视图,因此可以在页面范围内设置。

  • 错误和成功的消息最适合请求范围。如果用户更新页面,除非重新处理数据,否则他/她可能不会看到相同的消息。

  • 用户信息可以在会话范围内设置名称,昵称和首选项。

  • 如果您必须显示国家/地区列表(几天内不应更改),您可以将此列表存储在应用程序范围内。

  • The split of a String by comma for presentation purposes will affect only to current view, so this can be set in page scope.
  • Error and successful messages are best suited in request scope. If user updates the page, he/she probably must not see the same messages unless the data is re-processed.
  • User info as name, nickname and preferences can be set in session scope.
  • If you have to display a list of Countries (that should not change in few days), you can store this list in application scope.

这篇关于如何将参数传递给jsp:include通过c:set? JSP中变量​​的范围是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆