您的位置:首页 > 编程语言 > Java开发

struts2.X心得15--OGNL表达式语言和struts标签案例解析

2013-03-06 21:09 513 查看
1.
传值分析

如果从jsp页面传过来的值,那么要用set注入接收值,还要用get传出去值;如果不写get方法那么,在后面接收值的jsp页面上通过域对象就不能直接接收action传出来的值;但是如果不是从一个jsp页面传进来的值,而是在这个action中直接new出来一个对象给其构造方法传值或者直接声明的一个变量值,那么可以不通过set注入值也可以不用get方法传值。

使用struts的标签可以看出其中的原理,第一种并没有产生一个javabean对象,也没有在request、session等域中存进去一个有值的对象,而后者却恰恰相反。

2.在action中存入下操作值,在jsp页面输出结果

下面通过一个案例来分析struts中的这项操作

在Action中存值

//测试ognl的request,session,application
public String ognlTest1(){

this.setName("aaa");
ActionContext.getContext().put("pass", "context_pass");
ActionContext.getContext().getSession().put("name","session_kai");
ServletActionContext.getRequest().setAttribute("pass","request_pass");
ServletActionContext.getContext().getApplication().put("name","application_kai");

//存值
ActionContext ac = ActionContext.getContext();
ac.put("username","request-yang");
//存session
ac.getSession().put("username","session-yang");
//存application
ac.getApplication().put("username","application-yang");

ac.put("user",new Users(1,"aa","123","aa@qq.com"));
ac.getSession().put("user",new Users(2,"bb","bbbb","bb@qq.com"));
ac.getApplication().put("user",new Users(3,"cc","ccccc","cc@qq.com"));

return "index1";

在jsp中取值
<div align="center">
<h3>ongl标签对比,jsp显示界面的不同显示取值方式</h3>
姓名: ${ name }<br> request:${ requestScope.pass }<br>
session:${ sessionScope["name"] }<br> application:${
applicationScope.name }<br> <u>通过以下案例来说说明attr取值顺序的原理:page--request--session--application</u><br>
attr-name:
<s:property value="#attr.name" />
<br>attr-pass:
<s:property value="#attr.pass" />
</div>
<hr>
<div align="center">
<h3>显示ognl标签,测试ognl的request,session,application</h3>
request:---
<s:property value="#request.username" />
<br> session:---
<s:property value="#session['username']" />
<br> <br> application:---
<s:property value="#application.username" />
<br> <br>

</div>
<hr>

<div align="center">
<h3>显示ognl标签,测试ognl的request,session,application</h3>
request之request.user['email']显示方式:---
<s:property value="#request.user['email']" />
<br> session之session['user']显示方式:---
<s:property value="#session['user']" /><br>
session['user'].pass显示方式:
<s:property value="#session['user'].pass" />
<br> <br> applicationz之application.user.name显示方式:---
<s:property value="#application.user.name" />
<br> <br>
</div>

3.集合标签案例
Jsp 标签显示

div align="center">
<div>
<h3>显示测试值栈ValueStack</h3>
直接获取值栈中的值:
<s:property value="message" />
<br> 通过action中的get方法传值过来:
<s:property value="msg" />
<br> <br> <u>查看stack值栈传过来的对象属性的获取方法:</u>
<s:property value="user" />
<br>
<s:property value="name" />
----
<s:property value="email" />
<br> 直接写property标签,取得是栈顶的值
<s:property />
</div>
<hr>
<div>
<h3>构造map集合</h3>
使用var属性的情况(需要使用#访问):<br>
<s:iterator var="entity" value="#{1:'aa',2:'bb',3:'cc' }">
<s:property value="#entity.key" />----
<s:property value="#entity.value" />
</s:iterator>
<br> <br> 不使用var属性的情况:<br>
<s:iterator value="#{1:'说的',2:'达到',3:'低调' }">
<s:property value="key" />----
<s:property value="value" />
</s:iterator>
<br> <br> ------------------------------------------ <br>
<br>
<h3>构造list集合</h3>
使用var属性的情况:
<s:iterator var="edu" value="{'as','df','gh'}">
<s:property value="edu" />
</s:iterator>
<br> 不使用var属性的情况(property标签可以不用value访问):
<s:iterator value="{'as','df','gh'}">
<s:property />
</s:iterator>
</div>

</div>

Action传值
//测试值栈ValueStack
public String ognlTest2(){
this.setMsg("江南style");
ValueStack stack1 = ServletActionContext.getContext().getValueStack();
ValueStack stack = ActionContext.getContext().getValueStack();
stack.set("message", "你好啊!");

//将值栈中放入两个user对象,这两个对象默认追加到defaultTestProvider之后,但是可以为其设置下标的,因为其返回的类型CompoundRoot继承了arraylist接口
stack1.getRoot().add(1,new Users(1,"宿舍","ss","sd。@"));
stack.getRoot().add(2,new Users(2,"撒旦法","222","222。@"));
stack.push(new Users(2,"22222","222","d222@"));
stack.push(new Users(3,"dddd","dddd","dddd@"));
return SUCCESS;
查看jsp源码查看:翻译成普通标签后的jsp源码

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="http://localhost:8080/struts_ognl/">

<title>index</title>

</head>

<body>
<div align="center">
<h3>登陆操作</h3>

<form id="UserAction_login" name="UserAction_login" action="/struts_ognl/csdn/UserAction_login.action" method="post">
<table class="wwFormTable">

<tr>
<td class="tdLabel"><label for="UserAction_login_name" class="label">用户名:</label></td>
<td
><input type="text" name="name" value="dddd" id="UserAction_login_name"/></td>
</tr>

<tr>
<td class="tdLabel"><label for="UserAction_login_pass" class="label">密码:</label></td>
<td
><input type="password" name="pass" value="dddd" id="UserAction_login_pass"/></td>
</tr>

<tr>
<td colspan="2"><div align="right"><input type="submit" id="UserAction_login_0" value="登录"/>
</div></td>
</tr>
</table></form>

<div align="center">
<div>
<h3>显示测试值栈ValueStack</h3>
直接获取值栈中的值:
你好啊!
<br> 通过action中的get方法传值过来:
江南style
<br> <br> <u>查看stack值栈传过来的对象属性的获取方法:</u>

<br>
dddd
----
dddd@
<br> 直接写property标签,取得是栈顶的值
Users [id=3, name=dddd, pass=dddd, email=dddd@]
</div>
<hr>
<div>
<h3>构造map集合</h3>
使用var属性的情况(需要使用#访问):<br>

1----
aa

2----
bb

3----
cc

<br> <br> 不使用var属性的情况:<br>

1----
说的

2----
达到

3----
低调

<br> <br> ------------------------------------------ <br>
<br>
<h3>构造list集合</h3>
使用var属性的情况:

as

df

gh

<br> 不使用var属性的情况(property标签可以不用value访问):

as

df

gh

</div>

</div>
</body>
</html>
4.struts常用标签案例分析
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<title>My JSP 'collection.jsp' starting page</title>

<style type="text/css">
.odd{
background-color: blue;
}
.even{
background-color: red;
}
</style>
</head>

<body>
<div align="center">
<div>
<h3>单选按钮标签</h3>
<u>value属性用来指定默认值,如果map的key值是整数,那么value属性的值不用使用单引号,其他的要加上,否则不起作用</u><br>
写法一(map方式):
<s:radio list="#{0:'男',2:'女'}" value="0" />
<br> 写法二(map方式):
<s:radio list="#{'m':'男','w':'女'}" value="'w'" />
<br> 写法三(map方式):
<s:radio list="#{'01':'男','02':'女'}" value="'01'" />
<br> 写法四(list方式):
<s:radio list="{'男','女'}" value="'女'" />
</div>
<hr>
<div>
<h3>多选按钮标签</h3>
s:checkbox 标签的使用:
<s:checkbox name="fav" label="唱歌" />
<s:checkbox name="fav" label="跳舞" />
<s:checkbox name="fav" label="玩游戏" />
<s:checkbox name="fav" label="打篮球" />
<br> <br> s:checkboxlist 标签的使用(map方式):
<s:checkboxlist name="fav" list="#{1:'唱歌',2:'跳舞',3:'玩游戏',4:'打篮球'}"
value="1,2" />
<br> <br> s:checkboxlist 标签的使用(list方式):
<s:checkboxlist name="fav" list="{'唱歌','跳舞','玩游戏','打篮球'}"
value="'唱歌','打篮球'" />
</div>
<hr>
<div>
<h3>下拉按钮标签</h3>
<s:select name="edu" list="{'aa','bb','cc'}" label="选择值" value="'cc'" />
<br>
<s:select name="edu" list="#{1:'java',2:'net',3:'php'}" label="选择值"
headerKey="-1" headerValue="选择专业" />

</div>
<hr>
<div>
<h3>if else标签</h3>
<s:if test="'a' not in {'a','b','c'}">a 在集合中</s:if>
<s:else>a不在集合中 </s:else>
</div>
<hr>
<div>
<h3>push标签</h3>
注意:push标签中的value属性要加单引号的,而且访问的property只能在push标签内使用,外面使用娶不到push中的值<br>
<s:push value="'yang'">
<s:property />
</s:push>
<br>
<s:property />
</div>
<hr>
<div>
<h3>bean标签</h3>
<s:bean name="www.csdn.domain.Users" var="entity">
<s:param name="id" value="1" />
<s:param name="name" value="'kai'" />
<s:param name="pass" value="123" />
<s:param name="email" value="'kai@123'" />
内部输出:<s:property value="name" />
</s:bean>
<br> 直接输出:
<s:property value="#entity.email" />
<br> 先使用set标签后输出:
<s:set value="#entity" var="user"></s:set>
<s:property value="#user.pass" />
</div>
<hr>
<div>
<h3>迭代标签</h3>
<table border="1">
<s:iterator var="edu"
value="{'java.....','php......','3G.....','net...','liunx...'}"
status="st">
<tr class="<s:property value='#st.even?"even":"odd"'/>">
<td><s:property value="edu" /></td>
</tr>
</s:iterator>

</table>
</div>
<div>
<h3>set标签的属性</h3>
<s:set name="a" value="'aa<br>aaa'"></s:set>
<s:property value="a" escape="true"/>

</div>
</div>
</body>
</html>
查看jsp源码查看:翻译成普通标签后的jsp源码

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<title>My JSP 'collection.jsp' starting page</title>

<style type="text/css">
.odd{
background-color: blue;
}
.even{
background-color: red;
}
</style>
</head>

<body>
<div align="center">
<div>
<h3>单选按钮标签</h3>
<u>value属性用来指定默认值,如果map的key值是整数,那么value属性的值不用使用单引号,其他的要加上,否则不起作用</u><br>
写法一(map方式):
<tr>
<td class="tdLabel"></td>
<td
>
<input type="radio" id="0" checked="checked" value="0"/><label for="0">男</label>
<input type="radio" id="2" value="2"/><label for="2">女</label>
</td>
</tr>

<br> 写法二(map方式):
<tr>
<td class="tdLabel"></td>
<td
>
<input type="radio" id="m" value="m"/><label for="m">男</label>
<input type="radio" id="w" checked="checked" value="w"/><label for="w">女</label>
</td>
</tr>

<br> 写法三(map方式):
<tr>
<td class="tdLabel"></td>
<td
>
<input type="radio" id="01" checked="checked" value="01"/><label for="01">男</label>
<input type="radio" id="02" value="02"/><label for="02">女</label>
</td>
</tr>

<br> 写法四(list方式):
<tr>
<td class="tdLabel"></td>
<td
>
<input type="radio" id="男" value="男"/><label for="男">男</label>
<input type="radio" id="女" checked="checked" value="女"/><label for="女">女</label>
</td>
</tr>

</div>
<hr>
<div>
<h3>多选按钮标签</h3>
s:checkbox 标签的使用:
<tr>
<td valign="top" align="right">
</td>
<td valign="top" align="left">

<input type="checkbox" name="fav" value="true" id="fav"/><input type="hidden" id="__checkbox_fav" name="__checkbox_fav" value="true" /> <label for="fav" class="checkboxLabel">唱歌</label> </td>
</tr>

<tr>
<td valign="top" align="right">
</td>
<td valign="top" align="left">

<input type="checkbox" name="fav" value="true" id="fav"/><input type="hidden" id="__checkbox_fav" name="__checkbox_fav" value="true" /> <label for="fav" class="checkboxLabel">跳舞</label> </td>
</tr>

<tr>
<td valign="top" align="right">
</td>
<td valign="top" align="left">

<input type="checkbox" name="fav" value="true" id="fav"/><input type="hidden" id="__checkbox_fav" name="__checkbox_fav" value="true" /> <label for="fav" class="checkboxLabel">玩游戏</label> </td>
</tr>

<tr>
<td valign="top" align="right">
</td>
<td valign="top" align="left">

<input type="checkbox" name="fav" value="true" id="fav"/><input type="hidden" id="__checkbox_fav" name="__checkbox_fav" value="true" /> <label for="fav" class="checkboxLabel">打篮球</label> </td>
</tr>

<br> <br> s:checkboxlist 标签的使用(map方式):
<tr>
<td class="tdLabel"></td>
<td
>
<input type="checkbox" name="fav" value="1"
id="fav-1"        />
<label for="fav-1" class="checkboxLabel">唱歌</label>
<input type="checkbox" name="fav" value="2"
id="fav-2"       checked="checked"        />
<label for="fav-2" class="checkboxLabel">跳舞</label>
<input type="checkbox" name="fav" value="3"
id="fav-3"        />
<label for="fav-3" class="checkboxLabel">玩游戏</label>
<input type="checkbox" name="fav" value="4"
id="fav-4"        />
<label for="fav-4" class="checkboxLabel">打篮球</label>
<input type="hidden" id="__multiselect_fav" name="__multiselect_fav"
value=""        />    </td>
</tr>

<br> <br> s:checkboxlist 标签的使用(list方式):
<tr>
<td class="tdLabel"></td>
<td
>
<input type="checkbox" name="fav" value="唱歌"
id="fav-1"        />
<label for="fav-1" class="checkboxLabel">唱歌</label>
<input type="checkbox" name="fav" value="跳舞"
id="fav-2"        />
<label for="fav-2" class="checkboxLabel">跳舞</label>
<input type="checkbox" name="fav" value="玩游戏"
id="fav-3"        />
<label for="fav-3" class="checkboxLabel">玩游戏</label>
<input type="checkbox" name="fav" value="打篮球"
id="fav-4"       checked="checked"        />
<label for="fav-4" class="checkboxLabel">打篮球</label>
<input type="hidden" id="__multiselect_fav" name="__multiselect_fav"
value=""        />    </td>
</tr>

</div>
<hr>
<div>
<h3>下拉按钮标签</h3>
<tr>
<td class="tdLabel"><label for="edu" class="label">选择值:</label></td>
<td
><select name="edu" id="edu">
<option value="aa">aa</option>
<option value="bb">bb</option>
<option value="cc" selected="selected">cc</option>

</select>
</td>
</tr>

<br>
<tr>
<td class="tdLabel"><label for="edu" class="label">选择值:</label></td>
<td
><select name="edu" id="edu">
<option value="-1"
>选择专业</option>
<option value="1">java</option>
<option value="2">net</option>
<option value="3">php</option>

</select>
</td>
</tr>

</div>
<hr>
<div>
<h3>if else标签</h3>

a不在集合中
</div>
<hr>
<div>
<h3>push标签</h3>
注意:push标签中的value属性要加单引号的,而且访问的property只能在push标签内使用,外面使用娶不到push中的值<br>

yang

<br>
com.opensymphony.xwork2.DefaultTextProvider@38a133ff
</div>
<hr>
<div>
<h3>bean标签</h3>

内部输出:kai

<br> 直接输出:
kai@123
<br> 先使用set标签后输出:

123
</div>
<hr>
<div>
<h3>迭代标签</h3>
<table border="1">

<tr class="odd">
<td>java.....</td>
</tr>

<tr class="even">
<td>php......</td>
</tr>

<tr class="odd">
<td>3G.....</td>
</tr>

<tr class="even">
<td>net...</td>
</tr>

<tr class="odd">
<td>liunx...</td>
</tr>

</table>
</div>
<div>
<h3>set标签的属性</h3>

aa<br>aaa

</div>
</div>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: