Request.SetAttribute JSP 的角色
Role Of Request.SetAttribute JSP
这是我的 jsp 页面:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Test EL</title>
</head>
<body>
<p>
<%
/* Creation */
String[] animals = {"dog", "cat", "mouse", "horse"};
request.setAttribute("animals" , animals);
%>
${ animals[2] }<br />
</p>
</body>
我不明白的是:指令的用途是什么:"request.setAttribut",我已经声明了 table,但是当我删除该指令时我不明白为什么我无法获得 animals[2] 的价值..请问我在这里缺少什么?!
${}
这是一个 JSP EL 表达式。 EL 只能引用 scoped 变量而不是 local 变量。 Scoped 变量是对作为 属性 添加到四个可用范围之一的对象的引用,pageContext
、request
、session
和 application
.
在您的示例中,String[] animals
是一个 local 变量,因此无法通过 ${}
单独访问。要使 animals
数组可用于 JSP EL,需要先将其放入任何一个可用范围。
因此,在您的示例中,以下是将数组放置在 request
范围内。
// restricted to current request cycle
request.setAttribute("animals" , animals);
您还可以根据您的应用程序要求使用以下任何一项。
// restricted to this JSP page
pageContext.setAttribute("animals" , animals);
// restrcited to this user's session
session.setAttribute("animals" , animals);
// available throughout the application
application.setAttribute("animals" , animals);
${animals[i]}
会按以下顺序自动解析上述任一作用域中的对象:首先查找 pageContext
,然后是 request
,然后是 session
, 最后 application
.
要覆盖上述查找顺序,scope 也可以明确指定为
${pageScope.animals[i]}
${requestScope.animals[i]}
${sessionScope.animals[i]}
${applicationScope.animals[i]}
除 Ravi 外,如果您仍想在不设置其中一个属性的情况下访问数组,请使用 jsp 表达式标记来显示:
Eg: <%=animals[2]%> <br/>
如 ServletRequest documentation 中所述,setAttribute()
方法用于在请求中存储属性,以便您稍后访问它。
并且 ${variable}
是一个 jsp EL,用于在您的 Web 应用程序中轻松访问这些存储的数据,请查看 JSP - Expression Language (EL) 了解更多信息。
如果您在声明变量的同一页中,则可以避免使用 setAttribute() 和 EL 并使用 out.print()
打印变量结果,如下所示:
<% out.println(animals[2]);%>
这是我的 jsp 页面:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Test EL</title>
</head>
<body>
<p>
<%
/* Creation */
String[] animals = {"dog", "cat", "mouse", "horse"};
request.setAttribute("animals" , animals);
%>
${ animals[2] }<br />
</p>
</body>
我不明白的是:指令的用途是什么:"request.setAttribut",我已经声明了 table,但是当我删除该指令时我不明白为什么我无法获得 animals[2] 的价值..请问我在这里缺少什么?!
${}
这是一个 JSP EL 表达式。 EL 只能引用 scoped 变量而不是 local 变量。 Scoped 变量是对作为 属性 添加到四个可用范围之一的对象的引用,pageContext
、request
、session
和 application
.
在您的示例中,String[] animals
是一个 local 变量,因此无法通过 ${}
单独访问。要使 animals
数组可用于 JSP EL,需要先将其放入任何一个可用范围。
因此,在您的示例中,以下是将数组放置在 request
范围内。
// restricted to current request cycle
request.setAttribute("animals" , animals);
您还可以根据您的应用程序要求使用以下任何一项。
// restricted to this JSP page
pageContext.setAttribute("animals" , animals);
// restrcited to this user's session
session.setAttribute("animals" , animals);
// available throughout the application
application.setAttribute("animals" , animals);
${animals[i]}
会按以下顺序自动解析上述任一作用域中的对象:首先查找 pageContext
,然后是 request
,然后是 session
, 最后 application
.
要覆盖上述查找顺序,scope 也可以明确指定为
${pageScope.animals[i]}
${requestScope.animals[i]}
${sessionScope.animals[i]}
${applicationScope.animals[i]}
除 Ravi 外,如果您仍想在不设置其中一个属性的情况下访问数组,请使用 jsp 表达式标记来显示:
Eg: <%=animals[2]%> <br/>
如 ServletRequest documentation 中所述,setAttribute()
方法用于在请求中存储属性,以便您稍后访问它。
并且 ${variable}
是一个 jsp EL,用于在您的 Web 应用程序中轻松访问这些存储的数据,请查看 JSP - Expression Language (EL) 了解更多信息。
如果您在声明变量的同一页中,则可以避免使用 setAttribute() 和 EL 并使用 out.print()
打印变量结果,如下所示:
<% out.println(animals[2]);%>