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

查看:24
本文介绍了如何通过 c:set 将参数传递给 jsp:include?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).

如果你想让这个工作,你必须至少在请求范围内设置变量.要在请求范围内设置变量,请使用 上的 scope 属性:

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" />

<小时>

根据您的评论,在 Web 开发中,变量的范围很重要,因为它定义了可以使用变量的位置(类似于在类中声明为字段的变量和在方法中本地声明的变量).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.

更多信息:

这是完成我想要做的事情的正确方法吗?

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

如果有一个 Servlet 或另一个控制器来处理要在请求中设置的属性(例如 @Controller 来自 Spring MVC 或 JSF 托管 bean),则在那里设置属性而不是在您的页面中直接.

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.

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

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