jsp onload自动加载js的一个函数访问action时,数据重复
1、jsp:<bodyonload="getUserId();">2、js:functiongetUserId(){varuser_id=0;varaa=getxmlht...
1、jsp:
<body onload="getUserId();">
2、js:
function getUserId(){
var user_id = 0;
var aa = getxmlhttp();
aa.open("POST","/CampusNetwork/indexJSON/getUserId.action?time="+new Date().getTime(),false);
aa.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
aa.onreadystatechange = function(){
if (aa.readyState == 4) {
if(aa.status == 200){
var xx = JSON.parse(aa.responseText);
user_id = xx.user_id;
}else{
alert("Failed: " + aa.status);
}
}
};
aa.send(null);
}
3、action:
package com.cn.campus.common.index.action;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.cn.campus.common.user.entity.User;
import com.opensymphony.xwork2.ActionSupport;
public class GetUserIdAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = -992778018380291822L;
private HttpServletRequest request = ServletActionContext.getRequest();
private int user_id;
public int getUser_id() {
return user_id;
}
public String getUserId() throws Exception{
System.out.println("------------------000000000000---------------------");
User user = (User)request.getSession().getAttribute("user");
if (user != null){
this.user_id = user.getUser_id();
}else{
this.user_id = 0;
}
return SUCCESS;
}
}
访问了一次action,怎么打印了两次‘------------------000000000000---------------------’? 展开
<body onload="getUserId();">
2、js:
function getUserId(){
var user_id = 0;
var aa = getxmlhttp();
aa.open("POST","/CampusNetwork/indexJSON/getUserId.action?time="+new Date().getTime(),false);
aa.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
aa.onreadystatechange = function(){
if (aa.readyState == 4) {
if(aa.status == 200){
var xx = JSON.parse(aa.responseText);
user_id = xx.user_id;
}else{
alert("Failed: " + aa.status);
}
}
};
aa.send(null);
}
3、action:
package com.cn.campus.common.index.action;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.cn.campus.common.user.entity.User;
import com.opensymphony.xwork2.ActionSupport;
public class GetUserIdAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = -992778018380291822L;
private HttpServletRequest request = ServletActionContext.getRequest();
private int user_id;
public int getUser_id() {
return user_id;
}
public String getUserId() throws Exception{
System.out.println("------------------000000000000---------------------");
User user = (User)request.getSession().getAttribute("user");
if (user != null){
this.user_id = user.getUser_id();
}else{
this.user_id = 0;
}
return SUCCESS;
}
}
访问了一次action,怎么打印了两次‘------------------000000000000---------------------’? 展开
1个回答
2014-03-25
展开全部
先判断数据容器是否为空 把
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询