关于前端:关于EL表达式中requestScope和param区别

3次阅读

共计 602 个字符,预计需要花费 2 分钟才能阅读完成。

在浏览器地址输出,示意传入一个参数 test,值为 123

http://localhost:8888/Test/index.jsp?test=123

在 index.jsp 中尝试应用 EL 表达式取出,代码如下:

 <body>
    ${test}
 </body>

发现毫无结果,再应用 requestScope 尝试取出:

  <body>
    ${requestScope.test}
  </body>

发现还是毫无结果,感到十分惊讶,遂罗唆应用 java 脚本尝试取出。

  <body>
      <%request.getAttribute("test"); %>
  </body>

仍然无解。

之后发现,若应用已下代码向 request 作用域赋值,则用下面代码能够取出

<%
    request.setAttribute("test", "123");
 %>

查问材料后发现,应用以下代码能够取出之前的申请参数:
EL:

  <body>
    ${param.test}
  </body>

JAVA 脚本:

  <body>
      <%=request.getParameter("test") %>
  </body>

论断就是 :${param.name} 等价于 request.getParamter(“name”),这两种办法个别用于服务器从页面或者客户端获取的内容。

${requestScope.name} 等价于 request.getAttribute(“name”),个别是从服务器传递后果到页面,在页面中取出服务器保留的值。

正文完
 0