您的位置:首页 > 其它

EL表达式与隐式对象,实现前台从后台取值

2015-12-07 15:40 344 查看
EL表达式强大的功能在于在js、jsp页面中直接访问服务器作用域,非常的简洁高效,作用域存储数据的格式为key/value的键值对,在EL表达式中key,则可以取出对应的value值

1、EL表达式直接访问相应的作用域,访问的格式为:${作用域.对象}

Page:PageScope,使用方式${ pageScope.page_name }

Request:RequestScope,使用方式${ requestScope.request_name },相当于<%=request.getAttribute("request_name");%>

Session:SessionScope,使用方式${ sessionScope.session_name },相当于<%=session.getAttribute("session_name");%>

Application:ApplicationScope,使用方式${ applicationScope.application_name },相当于<%=application.getAttribute("application_name");%>


2、如果不指定访问的作用域,则访问顺序为:Page、Request、Session、Application范围查找(范围从小到大),访问格式为:对象,{对象},{user}、user.name,例如:<{user.name},例如:<%=user.getAddr( ) %> 等价于 {user.addr} (user为一个变量,在action或者severlet中存放于指定的作用域中,addr为user的属性)

3、EL表达式中的隐式对象,方便的获取作用域中特定的参数值,包括:

${param},将请求参数名称映射到单个字符串参数值,表达式 $(param.name) 相当于 <%=request.getParameter(name) %>

${paramValues},将请求参数名称映射到一个数值数组,表达式 ${paramvalues. name) 相当于 <%=request.getParamterValues(name)%>

${header},储存用户浏览器和服务端用来沟通的数据,表达式 ${header.name} 相当于 <%=request.getHeader(name)%>

${headerValues},有可能同一标头名称拥有不同的值,表达式 ${headerValues.name} 相当于 <%=request.getHeaderValues(name)%>

${cookie},将cookie名称映射到单个cookie对象,表达式 ${cookie.name.value} 返回带有特定名称的第一个cookie 值。如果请求包含多个同名的 cookie,则应该使用 ${headerValues.name} 表达式

${initParam},将上下文初始化参数名称映射到单个值,表达式 ${initParam.userid} 相当于 <%=application.getInitParameter("userid")%>


4、使用pageContext对象取得请求地址的详细信息:

${pageContext.request.queryString},取得请求的参数字符串,相当于<%=request.getQueryString()%>

${pageContext.request.requestURL},取得请求的URL,但不包括请求之参数字符串,相当于<%=request.getRequestURL()%>

${pageContext.request.contextPath},服务的web application 的名称,相当于<%=request.getContextPath()%>

${pageContext.request.method},取得HTTP 的方法(GET、POST)

${pageContext.request.protocol},取得使用的协议(HTTP/1.1、HTTP/1.0)

${pageContext.request.remoteUser},取得用户名称

${pageContext.request.remoteAddr },取得用户的IP 地址

${pageContext.session.new},判断session 是否为新的

${pageContext.session.id},取得session 的ID

${pageContext.servletContext.serverInfo},取得主机端的服务信息


5、可以查看我之前的一篇总结:/article/7846084.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: