struts2后台写cookie,前台读jquery读cookie
2014-04-03 22:57
337 查看
Login.jsp
var ck=$.cookie("JSUSUUID");
if(ck!=null) {
var up=ck.split("\,");
$("#t_UserName1").val(up[0].replace("\"",""));
$("#t_UserPass1").val(up[1].replace("\"",""))
}
LoginAction.java
public String userLogin(){
if( userService.userLogCheck(userDto.getName(), userDto.getPassword()).equals(SUCCESS))
{
User user=userService.loadUserByName(userDto.getName());
session.put("USER", user);
if(userDto.isRememberpassword()){
((HttpServletResponse)ActionContext.getContext().get(ServletActionContext.HTTP_RESPONSE)).addCookie(new CookieUtil().addCookie(user));
}
return SUCCESS;
}else{
this.addActionMessage("*用户名或密码错误");
return INPUT;
}
}cookieUtil.java
public class CookieUtil {
public static final String USER_COOKIE = "JSUSUUID";
// 添加一个cookie
public Cookie addCookie(User user) {
Cookie cookie = new Cookie(USER_COOKIE, user.getName() + ","
+ user.getPassword());
System.out.println("添加cookie");
cookie.setMaxAge(60 * 60 * 24 * 14);// cookie保存两周
cookie.setPath("/");
return cookie;
}
var ck=$.cookie("JSUSUUID");
if(ck!=null) {
var up=ck.split("\,");
$("#t_UserName1").val(up[0].replace("\"",""));
$("#t_UserPass1").val(up[1].replace("\"",""))
}
<form action="front/userLogin!userLogin" method="post" name="form1" id="form1"> <dl> <dt class="f14">会员登陆</dt> <div style="color:red;margin-bottom:10px; font-size:12px;"><s:property value="actionMessages[0]" /><s:property value="errors.inputError[0]"/></div> <dd><span class="title">登录账号:</span><input class="reg_input" name="userDto.name" id="t_UserName1" value="" type="text" onblur="checkp(this.id)"/><span id="t_UserName1Tip" class="onshow"></span></dd> <dd style="margin-top:20px;"><span class="title">密码:</span><input class="reg_input" name="userDto.password" id="t_UserPass1" type="password" onblur="checkp(this.id)"/><span id="t_UserPass1Tip" class="onshow"></span><div style="text-align:right; width:235px; margin-top:20px;"> <span class="clew_txt" style="padding:0;"><input type="checkbox" name="userDto.rememberpassword" id="" value="false" onclick="if(this.checked)this.value=true;else this.value=false;"/>自动登录 <a href="lostpwd.html">忘记密码?</a></span></div></dd> </dl> <div class="f_reg_but"><input id="button" name="button" type="submit" value="登陆" class="r_button" onclick="return check()"/></div> </form>
LoginAction.java
public String userLogin(){
if( userService.userLogCheck(userDto.getName(), userDto.getPassword()).equals(SUCCESS))
{
User user=userService.loadUserByName(userDto.getName());
session.put("USER", user);
if(userDto.isRememberpassword()){
((HttpServletResponse)ActionContext.getContext().get(ServletActionContext.HTTP_RESPONSE)).addCookie(new CookieUtil().addCookie(user));
}
return SUCCESS;
}else{
this.addActionMessage("*用户名或密码错误");
return INPUT;
}
}cookieUtil.java
public class CookieUtil {
public static final String USER_COOKIE = "JSUSUUID";
// 添加一个cookie
public Cookie addCookie(User user) {
Cookie cookie = new Cookie(USER_COOKIE, user.getName() + ","
+ user.getPassword());
System.out.println("添加cookie");
cookie.setMaxAge(60 * 60 * 24 * 14);// cookie保存两周
cookie.setPath("/");
return cookie;
}
}
相关文章推荐
- jQuery代码的编写---1.3.3: jQuery代码风格
- JQuery Selectors 方法说明
- jQuery遍历对象/数组/集合
- Jquery常用函数
- jquery 事件冒泡的介绍以及如何阻止事件冒泡
- jQuery代码的编写---1.3.2: 编写简单的jQuery代码
- jQuery代码的编写---1.3.1: 配置jQuery环境
- jQuery+css+html实现页面遮罩弹出框
- JQuery判断radio是否选中,获取选中值
- 加入jQuery---1.2.2: jQuery的优势
- 实现点击图片新浪微博或腾讯微博分享
- jQuery之防止冒泡事件
- 加入jQuery---1.2.1: jQuery简介
- Add or remove tab in tabs widget of JQuery
- 第10-11章: jQuery各个版本的变化及jQuery性能优化和技巧---目录
- jQuery:balloon气泡提示插件
- 第9章: jQuery Mobile---目录
- 第8章: 用jQuery打造个性网站---目录
- 第7章: jQuery插件的使用和写法---目录
- 第6章: jQuery与Ajax的应用---目录