DWR 与HttpSession

[b]DWR访问Http Session[/b]

有时为了能在JavaScript访问Session中的值,在JavaScript中根据 session中的值做相应的动作,如根据Session中的值选中相应的 radio,checkbox,select或其它操作等

DWR已经提供了访问Session中的方法
主要用到import org.directwebremoting.WebContextFactory;
通过其来访问 HttpSession

例 根据Session中的值选中相应的checkbox
1. 实体类 用户中有属性:
private Set<AccountItemType> outtypes = new HashSet<AccountItemType>();
AccountItemType为另一实体类
含有以下属性:
private  int id;

/**
* 账目项目所属的类别 0表示收入 1表示支出
*/
private int itemType;

/**
* 0表示公共项目 1表示会员私有项目
*/
private int isMember;

/**
* 项目名称
*/
private String typename; ------对应页面中的一组checkBOX

2. 用户登录后将用户信息 保存在Session中key为”MEMBER”;


3. 通过DWR获取Session中的用户,并选中 用户属性outtypes的属性 typename对应的checkbox
3.1先写一个JAVA类获取Session中的member,
import javax.servlet.http.HttpSession;

import mybook.entity.Member;

import org.directwebremoting.WebContextFactory;

public class DwrTest {

public Member testSession(){
HttpSession session = WebContextFactory.get().getSession();

Member member =(Member)session.getAttribute("MEMBER");

return member;
}

2.在DWR中配置相应的类 相关的类都要配置如 DwrTest,Member,AccountItemType,其中Member,AccountItemType的属性可以不用全部配置,只要配置需要在 DWR callback中需要访问的属性即可

<allow>
<create javascript="dwrsession" creator="new">
<param name="class" value="mybook.test.DwrTest" />
</create>


<convert match="mybook.entity.Member" converter="bean">
<param name="include" value="username,password,gender,outtypes" />
</convert>

<convert match="mybook.entity.AccountItemType" converter="bean">
<param name="include" value="typename" />
</convert>
</allow>

4. 在web.xml中配置dwr
<!-- 配置dwr 不同版本对应的servlet-class可能会有不同-->
<servlet>
<servlet-name>dwr-servlet</servlet-name>
<servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dwr-servlet</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>


5. 访问工程目录下的dwr目录 获取dwr产生的JavaScript
如 http://localhost:8080/abc/dwr;abc为工程名

并将其添加至 jsp页面中

6. 在JSP页面中通DWR提供的JS得到 DwrTest 类中相应方法返回的值并做相应操作


<!-- 以下三个JS文件为DWR提供,第一个为dwr根据配置文件生成,后两个为dwr提供的工具JS -->

<script type='text/javascript' src='/accountbook/dwr/interface/dwrsession.js'></script>
<script type='text/javascript' src='/accountbook/dwr/engine.js'></script>
<script type='text/javascript' src='/accountbook/dwr/util.js'></script>

<script type="text/javascript">
function test(){
dwrsession.testSession(callBack);

}

/****==
dwr 获取Session中的值
member为Dwr和JAVA程序交互获得的 用户登录后存在HttpSession中的 会员对象,
username 为member 的String属性, outtypes为member的Set<AccountItemType>集合在dwr中
接收为String[],typename为 AccountItem的String属性

在DWR配置时需要 配置 交互的JAVA类,Member,AccountItemType总之要用到的类都要配置;

*/
function callBack(member){
/*window.alert(member.username);
window.alert(member.outtypes.length); //打印集合的长度
window.alert(member.outtypes[0].typename);
window.alert(member.outtypes[1].typename);
window.alert(member.outtypes[2].typename);*/

//便利member中的outtypes
for( var i = 0; i < member.outtypes.length;i++ ){
//window.alert("====="+member.outtypes[i].typename+"=====");
var cid=member.outtypes[i].typename;
//window.alert(cid+"-----------");
//根据typename的值得到页面中相应的checkbox对象
var cBox=document.getElementById(cid);
//window.alert(cBox.value);
选中相应的对像
cBox.checked=true;

}

}


/*
function init(){
var cBox1 = document.getElementById("工作");
//window.alert(cBox1.value+"-----------");
if(cBox1.value=="工作"){
cBox1.checked=true;
}

} */
</script>

</head>

<body onload="test()">
<input type="button" onclick="test()" value="dwrSession测试"/>
<hr/>
<input type="checkbox" id="工作" value="工作"/>工作
<input type="checkbox" id="饮食" value="工作"/>饮食
<input type="checkbox" id="居住 " value="居住"/>居住
<input type="checkbox" id="文化/娱乐 " value="文化/娱乐"/>文化/娱乐
<input type="checkbox" id="其它" value="其它"/>其它
<hr/>
<input type="button" onclick="init()" value="checkboxtext"/>
</body>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值