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:

猜你喜欢

转载自blog.csdn.net/Mr_xiayijie/article/details/81880535