数据库使用mysql,下载链接https://dev.mysql.com/downloads/mysql/
安装配置完成后,使用控制台进入mysql,创建数据库ssh_example
开发工具与环境:MyEclipse2017,jdk1.8,tomcat8.0
jar包下载地址https://pan.baidu.com/s/1pQT91AYImJcX32lXa-lATg 提取码:5zjd
将所有的jar包复制到工程的WebRoot/WEB-INF/lib目录下,并Add to Build Path
接下来就开始写代码了。
首先在src下创建包entity,在此包下创建User.java和User.hbm.xml文件
User.java:
package entity;
public class User {
private Integer uid;
private String username;
private String password;
public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User [uid=" + uid + ", username=" + username + ", password=" + password + "]";
}
}
User.hbm.xml:映射文件中配置的是数据库表和实体类User的映射关系
在src下创建dao包,并在包内创建UserDao.java
UserDao.java:
package dao;
import java.util.List;
import org.springframework.orm.hibernate5.HibernateTemplate;
import entity.User;
public class UserDao {
private HibernateTemplate hibernateTemplate;
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
// 添加用户
public boolean addUser(User user) {
if(!verifyUsername(user.getUsername())) {
hibernateTemplate.save(user);
return true;
}
return false;
}
// 验证用户名是否存在
public boolean verifyUsername(String username) {
List users = (List) hibernateTemplate.find("from User where username=?", username);
return users.isEmpty() ? false:true;
}
// 验证密码是否正确
public boolean verifyPassword(String username, String password) {
List queryList = hibernateTemplate.find("select password from User where username=?", username);
return ( queryList.get(0).toString() ).equals(password);
}
}
在src下创建service包,并在包内创建UserService.java
package service;
import org.springframework.transaction.annotation.Transactional;
import dao.UserDao;
import entity.User;
@Transactional
public class UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public boolean loginVerify(String username, String password) {
boolean passLogin = false;
passLogin = userDao.verifyUsername(username);
if(passLogin) {
passLogin = userDao.verifyPassword(username, password);
}
return passLogin;
}
public boolean userRegister(String username, String password) {
User user = new User();
user.setUsername(username);
user.setPassword(password);
return userDao.addUser(user);
}
}
由于需要实现登录和注册两个功能,所以编写两个action类LoginActon和RegisterAction,分别实现登录和注册功能
在src下创建action包,并在此包内创建LoginAction.java和RegisterAction两个类
LoginAction.java
package action;
import java.util.Map;
import org.apache.struts2.interceptor.SessionAware;
import com.opensymphony.xwork2.ActionSupport;
import entity.User;
import service.UserService;
public class LoginAction extends ActionSupport implements SessionAware {
private UserService userService;
private String username;
private String password;
private Map session;
public void setSession(Map session) {
this.session = session;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public void validate() {
super.clearErrorsAndMessages();
if("".equals(username)) {
super.addActionError("用户名不能为空!");
}
if("".equals(password)) {
super.addActionError("密码不能为空!");
}
}
public String execute() {
if(userService.loginVerify(username, password)){
User user = new User();
user.setUsername(username);
user.setPassword(password);
session.put("user", user);
return "success";
} else {
super.addActionError("登录失败!");
return INPUT;
}
}
}
RegisterAction.java
package action;
import com.opensymphony.xwork2.ActionSupport;
import service.UserService;
public class RegisterAction extends ActionSupport {
private UserService userService;
public void setUserService(UserService userService) {
this.userService = userService;
}
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public void validate() {
super.clearErrorsAndMessages();
if("".equals(username)) {
super.addActionError("用户名不能为空!");
}
if("".equals(password)) {
super.addActionError("密码不能为空!");
}
}
public String execute() {
if(userService.userRegister(username, password)) {
super.addActionMessage("注册成功!");
} else {
super.addActionError("注册失败,该用户名已存在!");
}
return "success";
}
}
(1)log4j配置文件
工程需要使用log4j作为日志输出工具,故需要log4j的配置文件
在src下创建log4j.properties文件,内容如下:
#log4j.properties
log4j.rootLogger=info, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] -%m%n
(2)struts2的配置文件
在src下创建struts.xml文件,注册action,内容如下:
/loginSuccess.jsp
/login.jsp
/register.jsp
/register.jsp
(3)hibernate核心配置文件
在该例中将hibernate核心配置文件中的配置写在了下面的spring配置文件中
(4)spring的配置文件
spring配置文件起到了整合struts2和hibernate的重要作用
在src目录下创建spring配置文件applicationContext.xml,内容如下:
true
true
update
entity/User.hbm.xml
(5)web.xml配置
在WebRoot/WEB-INF目录下创建web.xml文件,内容如下:
ssh_example
contextConfigLocation
classpath:applicationContext.xml
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
*.action
struts2
*.jsp
org.springframework.web.context.ContextLoaderListener
login.jsp
最后就是编写简单的前台界面,与后台进行交互
需要的界面由登陆界面,注册界面和登录成功的界面
在WebRoot目录下分别创建login.jsp,register.jsp和loginSuccess.jsp三个jsp文件,内容如下:
(1)login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
%>
login page
登录
注册页面
(2)register.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
register page
注册
登录页面
(3)loginSuccess.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
login success page
登录成功!
当前登录用户为:
代码终于编写完了,现在开始部署工程,测试功能有没有实现吧!
部署工程后启动tomcat服务器,在浏览器的地址栏输入http://localhost:8080/ssh_example,回车就进入了下面的界面
然后点击注册页面先去注册一个账号吧
注册成功后再进入登陆页面,输入刚才注册的用户名和密码,点击登陆,如果出现下面的界面就表示大功告成了
注:如果要使密码栏不可见,只需修改jsp中密码的input标签即可
密码 :
最后附上整个工程的代码链接:
https://pan.baidu.com/s/1qZQ2jmc,密码:zh4k