java,ognl表达式%号的使用

<%@page import="java.util.Map"%> <%@page import="java.util.HashMap"%> <%@ 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>

<h1>通用标签库</h1>

<h3>property标签</h3>

主要用来读取 ognl 表达式的值 

<%  request.setAttribute("msg","<h1>hello</h1>,tag"); %>

<s:property value="#request.msg" escape="false"/>

<br/>

<s:property value="name" default="name属性不存在"/>

<hr/>

<h3>set标签</h3> 用法等同于 c:set <!-- value默认按照ognl解析  -->

<s:set var="name" scope="request" value="%{'张三'}"/>

${name}

<hr/>

<h3>iterator标签</h3> 主要用于遍历,功能和c:forEach 很类似 <br/>

<%

 Map map = new HashMap();     map.put("company", "itcast");   

  map.put("pnum", 100);    

request.setAttribute("map", map);

%>

//注意status是用来判断状态的,

<s:iterator value="#request.map" var="entry" status="status">

 <!-- entry在 root 和 contextMap 各保存一份 -->  

 <s:if test="#status.isEven()">

  <div style="color:red">

 </s:if>

 <s:else>  

 <div style="color:blue">  

</s:else>

 key : <s:property value="key"/> , value : <s:property value="#entry.value"/>

 </div>

</s:iterator>

<hr/>

<h3>if/elseif/else标签</h3>

<%  request.setAttribute("age", 48); %>

<s:if test="#request.age <20">青年</s:if>

<s:elseif test="#request.age<40">中年</s:elseif>

<s:else>老年</s:else>

<hr/>

<h3>url标签 对url的参数进行编码</h3>

<s:url action="download" namespace="/" var="myurl">

 <s:param name="filename" value="%{'MIME协议简介.txt'}"></s:param>

</s:url> <s:property value="#myurl"/>

<hr/>

<h3>a标签,链接对中文参数编码</h3>

<s:a action="download" namespace="/" >  

<s:param name="filename" value="%{'张三'}"/>  下载MIME协议简介.txt

</s:a>

</body>

</html>

 

posted @ 2013-10-04 16:32  sadan  阅读(559)  评论(0)    收藏  举报