ognl的%,#的使用

来源:互联网 发布:windows微信在哪里 编辑:程序博客网 时间:2024/05/16 01:34

ognl#的使用

  • 使用#获取context里面数据
  • 演示#操作
    • 向request域放值
    • 在页面中使用ognl赋值
public class ContextAction extends ActionSupport{           @Override        public String execute() throws Exception {            HttpServletRequest request=ServletActionContext.getRequest();            request.setAttribute("req","request");                      return SUCCESS;        }}
 <action name="context" class="cn.itcast.eitity.ContextAction">                        <result name="success">/context.jsp</result>                 </action>
<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><%@ taglib uri="/struts-tags" prefix="s"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body>     <!-- 获取context里面数据 写ognl时候 首先添加符号     #context的key的名称.域对象名称 -->  <s:property value="#request.req"/></body></html>

这里写图片描述

ognl%的使用

  • 在struts2的标签中表单标签
  • %使用:在struts2标签里面使用ognl表达式 如果直接在struts2表单标签里面使用ognl表达式不识别 只有%之后才会识别
<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><%@ taglib uri="/struts-tags" prefix="s"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body>     <!-- 获取context里面数据 写ognl时候 首先添加符号     #context的key的名称.域对象名称 -->            <s:property value="#request.req"/><br/>       <input type="text" name="name" value="${req}"><br/>      <s:textfield name="username" value="{#request.req}"></s:textfield>//没有加%</body></html>

这里写图片描述

发现我们并没有获取到值

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><%@ taglib uri="/struts-tags" prefix="s"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body>     <!-- 获取context里面数据 写ognl时候 首先添加符号     #context的key的名称.域对象名称 -->            <s:property value="#request.req"/><br/>       <input type="text" name="name" value="${req}"><br/>      <s:textfield name="username" value="%{#request.req}"></s:textfield>//加上%号后</body></html>

这里写图片描述

0 0
原创粉丝点击