在Action中存值
public String ognltest1(){
ActionContext ac=ActionContext.getContext();
ac.put("username", "request-chenhj");
ac.getSession().put("username", "session-chenhj");
ac.getApplication().put("username", "application-chenhj");
ac.put("user", new Users(1,"张三","123","aaaa@qq.com"));
ac.getSession().put("user", new Users(2,"lisi","1ad2adad3","aaddfggaaa@qq.com"));
ac.getApplication().put("user", new Users(3,"adada","1ad23","adada@qq.com"));
return SUCCESS;
}
//在jsp页面中取值
<div align="center">
<div id="ognl1">
<h3>显示request,session,application采用ognl表达式</h3><br>
request:<s:property value="#request.username"/><br>
session:<s:property value="#session.['username']"/><br>
application:<s:property value="#application.username"/><br>
</div>
</div>
讲解:
private User user;
必须提供get方法 才能在页面中通过上述标签获取
如果 在域中 直接存放对象 也可以采用上面的标签获取
分析:
第一个案例:struts_scope案例
第二个案例:struts_ognl
Context --_root和valueStack root是一样
Root;值栈
如果访问action之后比如:UserAction_ognltest1.action
Root 第一个对象:当前Action
第二个对象:DefaultTextProvider
对象栈:
valueSack.getRoot().add(new Users(1,”chenhj”,”123”,”chenhj@qq.com”));
valueSack.getRoot().add(new Users(2,”zhangsan”,”123”,”zhangsan@qq.com”));
valueSack.getRoot().add(0,new Users(1,”chenhj”,”123”,”chenhj@qq.com”));
valueSack.getRoot().add(1,new Users(2,”zhangsan”,”123”,”zhangsan@qq.com”));
语法#{key:value,key1:value1,key2:value2}
案例:
<s:iterator var="entity" value="#{1:'chenhj',2:'caojianbo',3:'liqiang' }">
<s:property value="key"/>
<s:property value="value"/>
<%-- <s:property value="#entity.key"/>
<s:property value="#entity.value"/> --%>
</s:iterator>
构造List
语法:{value1,value2,value3}
<s:iterator var="edu" value="{'java','php','3g'}">
<s:property/>
<s:property value="edu"/>
</s:iterator>
单选按钮:
<s:radio list="#{'w':'女','m':'男' }" listKey="key" listValue="value" value="'w'"></s:radio>
<br>
<s:radio name="sex" list="#{'1':'男','0':'女'}" value="'0'"></s:radio>
<br>
<s:radio list="{'女','男'}" value="'女'"></s:radio>
多选按钮:
<s:checkbox label="看书" name="fav"></s:checkbox>
<s:checkbox label="打篮球" name="fav"></s:checkbox>
<s:checkbox label="写字" name="fav"></s:checkbox>
<s:checkbox label="读书" name="fav"></s:checkbox>
<br>
<s:checkboxlist name="fav" list="{'看书','打篮球','写字','读书'}" value="{'读书','打篮球'}" />
<br>
<s:checkboxlist name="fav" list="#{1:'看书',2:'打篮球',3:'写字',4:'读书'}" listKey="key" listValue="value" value="{1,2,3}"/>
下拉列表:
<s:select label="Months" name="moths" headerKey="-1" headerValue="选择月份" list="#{'01':'Jan','02':'Feb','03':'Mat' }" value="selectedMonth" required="false"></s:select>
<br>
<s:select list="{'java','3g','php','.net'}" label="选择专业" name="edu" value="'.net'"></s:select>
<br>
<s:select list="#{1:'瑜伽',2:'户外'}" name="map" value="1"></s:select>
<hr>
if标签:
<s:if test="'Java' not in{'Java','Net'}">
Java in
</s:if>
<s:else>
Java not in
</s:else>
Set标签
<s:set var="msg" value="'aaa<br/>aaaaaa'" scope="request" ></s:set>
<s:property value="#requestmsg" escape="false"/>
<hr>
push:
<s:push value="'abc'">
<s:property/>
</s:push>
<hr>
Bean标签:
<s:bean name="www.csdn.struts2_tag.domain.Users" var="myUsers">
<s:param name="id" value="1"/>
<s:param name="name" value="'hahaha'"/>
<s:param name="pass" value="'123'"/>
<s:param name="email" value="'sdadsadsa@asdasd.as'"/>
<s:property value="name"/>
</s:bean>
输出1:
<s:property value="#myUsers.name"/>
<s:set var="user" value="#myUsers"></s:set>
输出2:
<s:property value="#user.name"/>
<hr>
iterator标签:
<table border="1">
<s:iterator var="name" value="{'java','net','php','3g'}" status="st">
<tr class="<s:property value='#st.even?"even":"odd"'/>">
<td>
<s:property value="name"/>
</td>
</tr>
</s:iterator>
</table>