@At
public Object GetUser(@Param("username")String name,HttpSession session) throws Exception {
User user = userDao.SelectUser(name);
logger.info("用户信息"+user);
if (user == null) {
return false;
} else {
session.setAttribute("me", user);
logger.info("me"+session.getAttribute("me"));
return true;
}
}
控制台日志
019-04-11 10:59:08,678 net.wendal.nutzbook.action.UserAction.GetUser(UserAction.java:43) INFO - 用户信息User [id=0, VipId=1778762, VipNumber=null, msterID=null, name=甘甘, QQ=null, Phone=18810503492, password=123456, Sex=男, CreateOrgId_Id=null, CreateOrg_Name=北京慈云寺店, FBirthday=1998-09-09T00:00:00, VipCreateDate=null, createTime=Thu Apr 11 10:59:08 CST 2019, updateTime=Thu Apr 11 10:59:08 CST 2019]
2019-04-11 10:59:08,695 net.wendal.nutzbook.action.UserAction.GetUser(UserAction.java:48) INFO - meUser [id=0, VipId=1778762, VipNumber=null, msterID=null, name=甘甘, QQ=null, Phone=18810503492, password=123456, Sex=男, CreateOrgId_Id=null, CreateOrg_Name=北京慈云寺店, FBirthday=1998-09-09T00:00:00, VipCreateDate=null, createTime=Thu Apr 11 10:59:08 CST 2019, updateTime=Thu Apr 11 10:59:08 CST 2019]
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>NutzBook demo</title>
<!-- 导入jquery -->
<script type="text/javascript" src="http://lib.sinaapp.com/js/jquery/2.0.3/jquery-2.0.3.min.js"></script>
<!-- 把user id复制到一个js变量 -->
<script type="text/javascript">
var me = '<%=session.getAttribute("me") %>';
var base = '${base}';
$(function() {
$("#login_button").click(function() {
$.ajax({
url : base + "/user/GetUser",
type: "POST",
data:$('#loginForm').serialize(),
error: function(request) {
alert("Connection error");
},
dataType:"json",
success: function(data) {
alert(data);
if (data == true) {
alert(me);
location.reload();
} else {
alert("登陆失败,请检查账号密码")
}
}
});
return false;
});
if (me != "null") {
$("#login_div").hide();
$("#userInfo").html("您的Id是" + me);
$("#user_info_div").show();
} else {
$("#login_div").show();
$("#user_info_div").hide();
}
});
</script>
</head>
<body>
<div id="login_div">
<form action="#" id="loginForm" method="post">
用户名 <input name="username" type="text" value="admin">
<button id="login_button">提交</button>
</form>
</div>
<div id="user_info_div">
<p id="userInfo"></p>
<a href="${base}/user/logout">登出</a>
</div>
</body>
</html>
64 回复
@At
@Ok("json:full")
public Object GetUser(@Param("username")String name,HttpSession session) throws Exception {
User user = userDao.SelectUser(name);
logger.info("用户信息"+user);
if (user == null) {
return false;
} else {
session.setAttribute("me", user.getPhone());
logger.info("me"+session.getAttribute("me"));
return true;
}
}
2019-04-11 11:11:01,448 net.wendal.nutzbook.action.UserAction.GetUser(UserAction.java:44) INFO - 用户信息User [id=0, VipId=1778762, VipNumber=null, msterID=null, name=甘甘, QQ=null, Phone=18810503492, password=123456, Sex=男, CreateOrgId_Id=null, CreateOrg_Name=北京慈云寺店, FBirthday=1998-09-09T00:00:00, VipCreateDate=null, createTime=Thu Apr 11 11:11:01 CST 2019, updateTime=Thu Apr 11 11:11:01 CST 2019]
2019-04-11 11:11:01,449 net.wendal.nutzbook.action.UserAction.GetUser(UserAction.java:49) INFO - me18810503492
2019-04-11 11:11:09,331 org.nutz.mvc.impl.UrlMappingImpl.get(UrlMappingImpl.java:101) DEBUG - Found mapping for [GET] path=/user/logout : UserAction.logout(UserAction.java:56)
2019-04-11 11:11:09,331 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:165) DEBUG - Get 'userAction'<class net.wendal.nutzbook.action.UserAction>
是这样下面这段代码不走所以导致我联系demo登录成功不跳页面
if (me != "null") {
$("#login_div").hide();
$("#userInfo").html("您的Id是" + me);
$("#user_info_div").show();
} else {
$("#login_div").show();
$("#user_info_div").hide();
}
2019-04-11 11:11:01,449 net.wendal.nutzbook.action.UserAction.GetUser(UserAction.java:49) INFO - me18810503492
控制台打印空null
var me = '<%=session.getAttribute("me") %>';
var base = '${base}';
$(function() {
$("#login_button").click(function() {
$.ajax({
url : base + "/user/GetUser",
type: "POST",
data:$('#loginForm').serialize(),
error: function(request) {
alert("Connection error");
},
dataType:"json",
success: function(data) {
alert(data);
if (data == true) {
//location.reload();
} else {
alert("登陆失败,请检查账号密码")
}
}
});
// alert(me);
return false;
});
console.info(me)
if (me != "null") {
$("#login_div").hide();
$("#userInfo").html("您的Id是" + me);
$("#user_info_div").show();
} else {
$("#login_div").show();
$("#user_info_div").hide();
}
});
```
var me = '<%=session.getAttribute("me") %>';
var base = '${base}';
$(function() {
$("#login_button").click(function() {
$.ajax({
url : base + "/user/GetUser",
type: "POST",
data:$('#loginForm').serialize(),
error: function(request) {
alert("Connection error");
},
dataType:"json",
success: function(data) {
alert(data);
if (data == true) {
//location.reload();
} else {
alert("登陆失败,请检查账号密码")
}
}
});
// alert(me);
return false;
});
console.info(me)
if (me != "null") {
$("#login_div").hide();
$("#userInfo").html("您的Id是" + me);
$("#user_info_div").show();
} else {
$("#login_div").show();
$("#user_info_div").hide();
}
});
@At
@Ok("json:full")
public Object GetUser(@Param("username")String name,HttpSession session) throws Exception {
User user = userDao.SelectUser(name);
logger.info("用户信息"+user);
if (user == null) {
return false;
} else {
session.setAttribute("me", user.getPhone());
logger.info("me"+session.getAttribute("me"));
return true;
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>NutzBook demo</title>
<!-- 导入jquery -->
<script type="text/javascript" src="http://lib.sinaapp.com/js/jquery/2.0.3/jquery-2.0.3.min.js"></script>
<!-- 把user id复制到一个js变量 -->
<script type="text/javascript">
var me = '<%=session.getAttribute("me") %>';
var base = '${base}';
$(function() {
$("#login_button").click(function() {
$.ajax({
url : base + "/user/GetUser",
type: "POST",
data:$('#loginForm').serialize(),
error: function(request) {
alert("Connection error");
},
dataType:"json",
success: function(data) {
alert(data);
if (data == true) {
location.reload();
} else {
alert("登陆失败,请检查账号密码")
}
}
});
// alert(me);
return false;
});
console.info(me)
if (me != "null") {
$("#login_div").hide();
$("#userInfo").html("您的Id是" + me);
$("#user_info_div").show();
} else {
$("#login_div").show();
$("#user_info_div").hide();
}
});
</script>
</head>
<body>
<div id="login_div">
<form action="#" id="loginForm" method="post">
用户名 <input name="username" type="text" value="">
<button id="login_button">提交</button>
</form>
</div>
<div id="user_info_div">
<p id="userInfo"></p>
<a href="${base}/user/logout">登出</a>
</div>
</body>
</html>
GetUser方法不要设置me属性, 只做获取
@At
@Ok("json:full")
public User GetUser(@Param("me")User me) {
return me;
}
再吐槽一下, Java的方法名不要大写开头呀
@At
@Ok("json:full")
public Object login(@Param("username")String name,HttpSession httpSession) throws Exception {
boolean b = userDao.SelectUser(name);
httpSession.setAttribute("me", name);
return b;
}
添加回复
请先登陆