struts2 OGNL表达式小记

适用环境:JavaServer Pages. Any JSP 1.2 +Struts2


用法:

Application Scope Attribute


<s:property value="%{#myContextParam}" />


Session Scope Attribute

 
<s:property value="%{#session.mySessionAttribute}" />



Session Scope Attribute


<s:property value="%{#session.mySessionAttribute}" />



Request Scope Attribute


<s:property value="%{#request.myRequestAttribute}" />



Request Parameter

 

<s:property value="%{#parameters.myParameter}" />


(e.g. 超链接传参数)

Context Scope Parameter


<s:property value="%{#myContextParam}" />



等等……


---------------------------------华丽的demo分割线---------------------------------------------

首先导入标签库!

<%@ taglib uri="/struts-tags" prefix="s2" %>

放在request作用域的数据:

request.setAttribute("products", products);

猥琐的HTML代码!。。。。。。。

获取request数据!

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
request.setAttribute("basePath",basePath);
%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="/struts-tags" prefix="s2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>商品列表</title>
<link rel="stylesheet" href="${basePath}product/css/type.css" type="text/css"/>
<link type="text/css" rel="stylesheet" href="${basePath}index/common/common.css"/>
<script language="javascript" type="text/javascript" src="${basePath}index/common/jquery132min.js"></script>
<script language="javascript" type="text/javascript" src="${basePath}index/common/common.js"></script>
<style type="text/css">
<!--
.STYLE1 {color: #0000FF}
.STYLE2 {color: #FFFFFF}
.STYLE3 {
	color: #FF0000;
	font-weight: bold;
	font-size: 18px;
}
.STYLE4 {color: #FF6600}
.STYLE5 {color: #FF6600; font-size: 12px; }
.STYLE9 {
	font-size: 14px;
	color: #FFFFFF;
}
.STYLE12 {
	color: #666666;
	font-weight: bold;
}
.STYLE13 {
	color: #666666;
	font-size: 14px;
	font-weight: bold;
}
.STYLE14 {font-size: 12px}
body{
	margin:0px 0px 0px 0px;
	padding:0px;
	text-align:center;
}
#Layer0 {
	position:relative;
	margin:0px auto 0px auto;
	height:228px;
	z-index:1;
	width:929px;
	text-align:left;
	top:0px;
	
}
#Layer02 {
	position:relative;
	width:929px;
	height:59px;
	z-index:3;
}
#Layer1 {
	position:relative;
	width:929px;
	height:300px;
	padding-top:10px;
	z-index:5;
}
#bottom {
	position:absolute;
	width:888px;
	height:80px;
	z-index:15;
	left:73px;
	top:1350px;
	border:1px solid #D3D2D2;
}
-->
</style>
</head>

<body>
<jsp:include page="../index/top.jsp" flush="false"/>
<div id="Layer0">
<div id="Layer1">
<table width="900" height="100" border="1" cesllpadding="1" cellspacing="0" >
      <tr>
      <td height="25" colspan="3" class="big1">
      <span class="STYLE12">
筛选条件:
      </span>
      </td>
      </tr>
  <tr>
    <td width="100%" height="100">
	  <table width="917" border="0">
	     <tr>
		    <td width="12"></td>
		    <td width="98" height="30" align="center"><span class="STYLE13">品牌:</span></td>
			<td width="783" class="big1">
 			   <a href="#">品牌名称</a>
 			</td>
   </tr>
		 <tr>
		    <td colspan="3"><hr style="border:1px dashed #CCCCCC; height:1px"/> </td>
		 </tr>
		 <tr>
		    <td></td>
		    <td height="30" align="center"><span class="STYLE13">价格:</span></td>
			<td class="big1">
			<a href="#">500元及以下</a>
			<a href="#">500-1000元</a>
			<a href="#">1000-2000元</a>
			<a href="#">2000-3000元</a>
			<a href="#">3000-4000元</a>
			<a href="#">4000元以上</a>
			<a href="#">全部</a>
			</td>
		 </tr>
		  <tr>
		    <td colspan="3"><hr style="border:1px dashed #CCCCCC; height:1px"/> </td>
		 </tr>
	  </table>	</td>
  </tr>
</table>
<br />
       <span class="STYLE14">当前第<font color="#FF0000">1</font>/2页
	   <a href="#">上一页</a>
	   <a href="#">下一页</a></span>
 <table width="931" height="113" border="1" cellpadding="2" cellspacing="0" bordercolorlight="#CCCCCC" bordercolordark="#FFFFFF" bgcolor="#FFFFEE">
   <tr>
    <td width="15" height="84" bgcolor="#CCCCCC"></td>
    <td width="835">
	   <table width="900" height="149" border="0">
 	      				<tr>
 	       			 <td width="108">            </td>
		     	     <td width="108" height="22"></td>
		    		 <td height="27" bgcolor="#FFFFFF" colspan="4">
					 </td>
		 				 </tr>
		  <s2:iterator var="product" value="%{#request.products}" id="product">
		 				 <tr>
		    		<td width="148" class="big2"><img src="${basePath}product/images/<s2:property value="%{#product.pimage}" />"
		    		 width="80" height="80"  border="0" title="点击浏览"/></td>
		     		<td width="200" height="116" class="big2">
			 名称:<s2:property value="%{#product.pname}"/><br />
                       屏幕尺寸:<s2:iterator var="parameter" value="%{#product.parameters}">
 				<s2:property value="%{#parameter.screensize}" />
				</s2:iterator>
                <br />
			 系列:<s2:property value="%{#product.series}" /></td>
		     		<td width="300" height="116" class="big2">
			 系统:
			 <s2:iterator var="parameter" value="%{#product.parameters}">
			 <s2:property value="%{#parameter.os}" />	
			 </s2:iterator>
			 <br />
			 CPU:
			 <s2:iterator var="parameter" value="%{#product.parameters}">
			 <s2:property value="%{#parameter.cpu}" />	
			 </s2:iterator>
		    		</td>
					<td width="159" height="116" class="big2">
		内存容量: <s2:iterator var="parameter" value="%{#product.parameters}">
			 <s2:property value="%{#parameter.memcap}" />	
			 </s2:iterator><br />			  
	           硬盘容量: <s2:iterator var="parameter" value="%{#product.parameters}">
			 <s2:property value="%{#parameter.diskcap}" />	
			 </s2:iterator>
			 		</td>
					<td width="109" rowspan="2" class="big2">
	         颜色/价格:
			<br /> 
			<s2:iterator var="color" value="%{#product.colorses}">
			<s2:property value="%{#color.color}" />/<s2:property value="%{#color.price}" /><br />	
			</s2:iterator>
					</td>
			<td width="84" rowspan="2" class="big2">
			<a href="#">加入购物车</a>
					</td>
		  				</tr>
		  </s2:iterator>
        </table>	
	  				</td>
  						</tr>
 
</table>
</div>
</div>
<!-- bottom -->
<div id="bottom">
<jsp:include page="../index/bottom.jsp" flush="true"/>
</div>

</body>
</html>



来源:小伙伴开发网http://www.kaifaer.com


你可能感兴趣的:(struts2)