1. 程式人生 > >Struts2標籤 %{ } %{# }詳解

Struts2標籤 %{ } %{# }詳解

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

一.Struts2的值棧l結構?

1.root--結構是List集合

2.context--結果是map集合

可通過jsp頁面<s:debug></s:debug>或<s:debug/>檢視相應的結構

注:Struts2 2.5.16版本需要再struts.xml配置常量:?<constant name="struts.devMode" value="true" />顯示【debug】超連結

二.#、%{}、%{#}的作用

#可以取出堆疊上下文(context)中的存放的物件

%{}可以取出存在值堆疊(root)中的物件(可以理解成強轉為物件)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib prefix="s" uri="/struts-tags" %>
<!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 -->
--域物件存入了context<br/>
context取值:<s:property value="request.req"/><br/>
context取值(#):<s:property value="#request.req"/>
<hr/>
---%{#}強轉成物件<br/>
字串效果:<s:textarea value="#request.req"></s:textarea><br/>

物件:<s:textarea value="%{#request.req}"></s:textarea>

<hr/>


--物件存於root<br/>
<s:property value="p.id"/>
<s:property value="p.name"/><br/>
<hr/>
獲取root中物件(強轉):<s:textarea value="%{p.id}"></s:textarea><br/>
獲取context中物件:<s:textarea value="%{#p.id}"></s:textarea>


<s:debug></s:debug>




</body>
</html>

結果:

開啟debug:

值棧中的:root:

值棧中的:context: