package luju.me.teach.struts2.login;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.Action;
/**
* @author 鲁炬 http://luju.me
*
*/
public class LoginAction {
private String loginname;
private String password;
private String msg;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getLoginname() {
return loginname;
}
public void setLoginname(String loginname) {
this.loginname = loginname;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
/** 用户登陆 */
public String login() {
if(StringUtils.isBlank(this.loginname)) {
return Action.INPUT;
}
/*
这里写你自己通过用户名查找用户信息的业务逻辑
比如:使用手机号登陆
Citizen user = prmService.queryEGovCitizenByMobile(this.loginname);
....
*/
if(user == null || user.getPwd() == null || !user.getPwd().getValue().equals(this.password)) {
//登陆失败
this.msg = "用户不存在或密码错误!";
return Action.INPUT;
} else {
//登陆成功
//设置session
this.getSession().setAttribute("_USER_INFO_LOGIN_NAME_", this.loginname);
this.getSession().setAttribute("_USER_INFO_USER_ID_", user.getId().getValue());
this.getSession().setAttribute("_USER_INFO_USER_INFO_", user);
//设置cookie
this.getResponse().addCookie(new Cookie("_USER_INFO_LOGIN_NAME_", this.loginname));
this.getResponse().addCookie(new Cookie("_USER_INFO_USER_ID_", user.getId().getValue()));
return Action.SUCCESS;
}
}
/**
* 注销
*/
public String loginout() {
//清空session
this.getSession().invalidate();
return Action.SUCCESS;
}
public HttpSession getSession() {
return ServletActionContext.getRequest().getSession();
}
public HttpServletRequest getRequest() {
return ServletActionContext.getRequest();
}
public HttpServletResponse getResponse() {
return ServletActionContext.getResponse();
}
}
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="common" namespace="/common"> <action name="login" class="luju.me.site.common.action.LoginAction" method="login"> <result name="input">login.jsp</result> <result name="success" type="redirect">/page.jsp</result> </action> <action name="loginout" class="luju.me.site.common.action.LoginAction" method="loginout"> <result name="success" type="redirect">login.action</result> </action> </package> </struts>
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>欢迎登录</title>
</head>
<body>
<div id="login">
<span>${msg}</span>
<form name="form1" method="post" action="<c:url value="/common/login.action" />" >
<span>
<label>用户名:</label>
<input name="loginname" id="loginname" type="text" value="admin" />
</span>
<span>
<label>密码:</label>
<input type="password" name="password" id="password" value="123"/>
</span>
<span>
<input type="submit" value="登陆" />
</span>
</form>
</div>
</body>
</html>
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
boolean isLogin = false;
String loginName = (String)request.getSession().getAttribute("_USER_INFO_LOGIN_NAME_");
if(loginName != null && !"".equals(loginName)){
isLogin = true;
}
request.setAttribute("isLogin",isLogin);
request.setAttribute("loginName",loginName);
%>
<c:if test="${isLogin}">
你好:${loginName}
<a href="<c:url value="/common/loginout.action" />">注销</a>
</c:if>
<c:if test="${!isLogin}">
<form name="login_form" method="post" action="<c:url value="/common/login.action" />" >
<span>
<label>手机号:</label>
<input name="loginname" id="loginname" type="text" value="" />
</span>
<span>
<label>密码:</label>
<input type="password" name="password" id="password" value=""/>
</span>
<span>
<input type="submit" value="登陆" />
</span>
</form>
</c:if>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有