An overview,

From now on, I have time to summarize some of the things I practiced before. I hope that the summary can be more convenient to learn in the future. I also hope that I can help the fellow pedestrians who need these things to learn together and make progress together.

Ii. Summary of login functions

2.1 Overview of login functions

This technology is mainly the use of Struts2+ Hibernate + Spring technology to achieve the login function, and is implemented in a specific project, for future reference can provide convenient.

2.2. Specific implementation

2.2.1 Implementation of the login page

<% @ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"% >

<%@taglib prefix="s" uri="/struts-tags"% >



<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<link href="css/index.css" rel="stylesheet" type="text/css" />

<script type="text/javascript" src="Js/jquery - 1.8.3. Js"></script>

<title>Jiangxi Normal University QualityControl - System login page</title>

<script>

$(function() {

$("#login_ok").click(function() {

$("form:first").submit();

});

});

function MM_swapImage(srcObj,image_src){

srcObj.src=image_src;

}

</script>

</head>

<body>

<s:actionerror/>

<div class="container-login">

<div class="login-pic">

<div class="login-text">

<s:form action="emp_login" method="post">

<table width="100%" border="0" cellpadding="0" cellspacing="0">

<tr>

<td width="19%" height="28">User name:</td>

<td colspan="2">

<s:textfield name="em.userName" size="18" value="admin"/>

</td>

</tr>

<tr>

<td height="31">Dense & have spent &nbsp; Code:</td>

<td colspan="2">

<s:textfield name="em.pwd" size="18" value="admin"/>

</td>

</tr>

<tr>

<td height="30">Verification code:</td>

<td width="43%">

<input type="text" size="9" />

</td>

<td width="32%"><img src="images/test.gif" />

</td>

</tr>

<tr>

<td height="30">&nbsp;</td>

<td colspan="2">

<a href="javascript:void(0)" id="login_ok">

<img src="images/denglu_bg_03.gif"

name="Image1" width="40"

height="22" border="0"

onmouseover="MM_swapImage(this,'images/denglu_h_03.gif')"

onmouseout="MM_swapImage(this,'images/denglu_bg_03.gif')" />
</a>

<a href="#">

<img src="images/giveup_bg_03.gif"

name="Image2" width="42"

height="22" border="0"

onmouseover="MM_swapImage(this,'images/giveup_h_03.gif')"

onmouseout="MM_swapImage(this,'images/giveup_bg_03.gif')" />
</a>

</td>

</tr>

</table>

</s:form>

</div>

</div>

</div>

</body>

</html>

Copy the code

Textfield name=”em.userName” size=”18″ value=”admin”/>

2.2.2 Implementation of Action

/ / login

public String login(){

HttpServletRequest request = getRequest();

String loginIp = request.getHeader("x-forwarded-for");

if(loginIp == null || loginIp.length() == 0 || "unknown".equalsIgnoreCase(loginIp)) {

loginIp = request.getHeader("Proxy-Client-IP");

}

if(loginIp == null || loginIp.length() == 0 || "unknown".equalsIgnoreCase(loginIp)) {

loginIp = request.getHeader("WL-Proxy-Client-IP");

}

if(loginIp == null || loginIp.length() == 0 || "unknown".equalsIgnoreCase(loginIp)) {

loginIp = request.getRemoteAddr();

}

EmpModel loginEm = empEbi.login(em.getUserName(),em.getPwd(),loginIp);

if(loginEm == null) {

this.addActionError("Sorry, wrong username and password!");

return "loginFail";

}else{

// Query all permissions of the user upon successful login

List<ResModel> resList = resEbi.getResByEm(loginEm.getUuid());

StringBuilder sbf = new StringBuilder();

for (ResModel rm : resList) {

sbf.append(rm.getText());

sbf.append(",");

}

// System.out.println("11111111111111");

// System.out.println(sbf.toString()+"=="+loginEm.getUuid());

// System.out.println("11111111111111");

loginEm.setResAll(sbf.toString());

putSession(EmpModel.EMP_LOGIN_USER_OBJECT_NAME, loginEm);

return "loginSuccess";

}

}

Copy the code

2.2.3 Service implementation

public EmpModel login(String userName, String pwd,String lastLoginIp) {

/ / the MD5 encryption

pwd = MD5Utils.md5(pwd);

// Invoke the data layer

EmpModel loginEm = empDao.getByUserNameAndPwd(userName,pwd);

if(loginEm ! =null) {

// Login succeeded

// Add login information

// Number of logins +1

loginEm.setLoginTimes(loginEm.getLoginTimes()+1);

// Last login time

loginEm.setLastLoginTime(System.currentTimeMillis());

// Last login IP address

loginEm.setLastLoginIp(lastLoginIp);

// Snapshot update

}

return loginEm;

}

Copy the code

Dao mainly queries whether the user exists by the user name and password

public EmpModel getByUserNameAndPwd(String userName, String pwd) {

String hql ="from EmpModel where userName = ? and pwd = ?";

List<EmpModel> temp = this.getHibernateTemplate().find(hql,userName,pwd);

return temp.size()>0 ? temp.get(0) :null;

}

Copy the code

2.2.5 User entity classes

package org.sihai.qualitycontrol.auth.emp.vo;

import java.text.DateFormat;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.HashMap;

import java.util.Map;

import java.util.Set;

import org.sihai.qualitycontrol.auth.dep.vo.DepModel;

import org.sihai.qualitycontrol.auth.role.vo.RoleModel;

import org.sihai.qualitycontrol.util.format.FormatUtil;

public class EmpModel {

public static final String EMP_LOGIN_USER_OBJECT_NAME = "loginEm";

// Application of data structure ideas

public static final Integer EMP_GENDER_OF_MAN = 1;

public static final Integer EMP_GENDER_OF_WOMAN = 0;

public static final String EMP_GENDER_OF_MAN_VIEW = "Male";

public static final String EMP_GENDER_OF_WOMAN_VIEW = "Female";

public static final Map<Integer, String> genderMap = new HashMap<Integer, String>();

static{

genderMap.put(EMP_GENDER_OF_MAN, EMP_GENDER_OF_MAN_VIEW);

genderMap.put(EMP_GENDER_OF_WOMAN, EMP_GENDER_OF_WOMAN_VIEW);

}

private Long uuid;

private String userName;

private String name;

private String pwd;

private String email;

private String tele;

private String address;

private String lastLoginIp;

private Integer loginTimes;

private Long lastLoginTime;

private Integer gender;

private String resAll;

public String getResAll(a) {

return resAll;

}

public void setResAll(String resAll) {

this.resAll = resAll;

}

/ *

//Long: records the value of milliseconds

//Date: wrapping long Advantage: good format, disadvantage: slightly complex calculation time

The current date is April 31, 2020

What was the date 180 days ago?

Long System.currentTimemillis ()-180*24*60*60*1000

long-long >0

Date: January 4, 2014 14:21

Date: January 4, 2014 14:22

* /


private Long birthday;

private Set<RoleModel> roles;/ / many-to-many

public Set<RoleModel> getRoles(a) {

return roles;

}

public void setRoles(Set<RoleModel> roles) {

this.roles = roles;

}

// View value: A view value is a variable value used for display in the interface. It does not correspond to a database field, but serves a database field

// If the value of a field in the database cannot be displayed directly, add a view value for the field to display the corresponding information

//1. Define a variable of type String. The variable name is the field name of the field that cannot be displayed properly +View

//2. Provide its get method

//3. Initialize the View value in the set method of its corresponding variable

private String birthdayView;

private String genderView;

private String lastLoginTimeView;

public String getLastLoginTimeView(a) {

return lastLoginTimeView;

}

public String getLastLoginIp(a) {

return lastLoginIp;

}

public void setLastLoginIp(String lastLoginIp) {

this.lastLoginIp = lastLoginIp;

}

public Integer getLoginTimes(a) {

return loginTimes;

}

public void setLoginTimes(Integer loginTimes) {

this.loginTimes = loginTimes;

}

public Long getLastLoginTime(a) {

return lastLoginTime;

}

public void setLastLoginTime(Long lastLoginTime) {

this.lastLoginTime = lastLoginTime;

this.lastLoginTimeView = FormatUtil.formatDate(lastLoginTime);

}

public String getGenderView(a) {

return genderView;

}

public String getBirthdayView(a) {

return birthdayView;

}

/ / a

private DepModel dm;

public Long getUuid(a) {

return uuid;

}

public void setUuid(Long uuid) {

this.uuid = uuid;

}

public String getUserName(a) {

return userName;

}

public void setUserName(String userName) {

this.userName = userName;

}

public String getName(a) {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getPwd(a) {

return pwd;

}

public void setPwd(String pwd) {

this.pwd = pwd;

}

public String getEmail(a) {

return email;

}

public void setEmail(String email) {

this.email = email;

}

public String getTele(a) {

return tele;

}

public void setTele(String tele) {

this.tele = tele;

}

public String getAddress(a) {

return address;

}

public void setAddress(String address) {

this.address = address;

}

public Integer getGender(a) {

return gender;

}

public void setGender(Integer gender) {

this.gender = gender;

this.genderView = genderMap.get(gender);

}

public Long getBirthday(a) {

return birthday;

}

public void setBirthday(Long birthday) {

this.birthday = birthday;

this.birthdayView = FormatUtil.formatDate(birthday);

}

public DepModel getDm(a) {

return dm;

}

public void setDm(DepModel dm) {

this.dm = dm;

}

}

Copy the code

2.2.5.1 The usage of view value is displayed in the entity class by converting data into the form required to be displayed on the page.

Entity class:

    private String genderView;

// Application of data structure ideas

public static final Integer EMP_GENDER_OF_MAN = 1;

public static final Integer EMP_GENDER_OF_WOMAN = 0;

public static final String EMP_GENDER_OF_MAN_VIEW = "Male";

public static final String EMP_GENDER_OF_WOMAN_VIEW = "Female";

public static final Map<Integer, String> genderMap = new HashMap<Integer, String>();

static{

genderMap.put(EMP_GENDER_OF_MAN, EMP_GENDER_OF_MAN_VIEW);

genderMap.put(EMP_GENDER_OF_WOMAN, EMP_GENDER_OF_WOMAN_VIEW);

}

// Use the set method to change the gender from 0/1 to male or female

public void setGender(Integer gender) {

this.gender = gender;

this.genderView = genderMap.get(gender);

}

Copy the code

Page display:

${genderView}

Copy the code

2.2.5.2 Map value in Struts2

<s:select name="eqm.gender" list="@org.sihai.qualitycontrol.auth.emp.vo.EmpModel@genderMap" cssStyle="width:190px"

headerKey="1" headerValue="---- please choose ----"></s:select>

Copy the code

The list is first evaluated using Struts2 tags, and then the OGNL expression is used to evaluate the map on the page.

2.2.5.3 Struts2 Values for list

<s:select name="eqm.dm.uuid" list="depList" listKey="uuid" listValue="name" cssStyle="width:190px"

headerKey="1" headerValue="---- please choose ----"></s:select>

Copy the code

Welcome to follow my wechat official accountGood good study Java, we will provide you with quality video tutorials, source code and cutting-edge fresh articles.