环境:
<span style="font-family:Microsoft YaHei;font-size:14px;">JDK:1.8
IDE:NetBeans 8.0.2
struts:2.3.15
MySQL:5.6.26</span>
-- 创建数据库
CREATE DATABASE `attendance`;
-- 创建表
CREATE TABLE `admin` (
`id` varchar(10) NOT NULL,
`pwd` varchar(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- 插入记录,测试用户名"admin",密码"admin"
INSERT INTO attendance.admin(id,pwd) values('admin','admin');
2. 编写数据库连接类SQLCon
package SQLHelper;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
* @author falleyes
*/
public class SQLCon {
// 连接实例
private static Connection conn = null;
//连接地址
String url = "jdbc:mysql://localhost:3306/mysql";
// MySQL用户名
String user = "sa";
// MySQL密码
String password = "mysql";
public SQLCon() throws Exception {
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection(url,user,password);
}
//获得连接对象
public static Connection getConnection(){
return conn;
}
//关闭连接
public static void CloseCon() throws SQLException{
conn.close();
}
}
3. 设计网站的结构
4. 配置web.xml并且编写index.jsp页面(该页面只是个跳转页面)
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
<%--
Document : index.jsp
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</style>
</head>
<body>
<jsp:forward page="Login/LoginJSP.jsp"/>
</body>
</html>
<%--
Document : LoginJSP.jsp
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
<style type="text/css">
#MainLogin {
position: absolute;
width: 500px;
top: 30%;
left: 50%;
font-size:24px;
margin-left:-250px;
}
</style>
</head>
<body>
<div class="MainLogin" id="MainLogin">
<s:form action="LoginAction">
<table>
<tr>
<td style="height:30px; text-align:center; font-size:24px; ">
欢迎登录
</td>
</tr>
<tr>
<td><br/></td>
</tr>
<tr>
<td>
<input name="userid" type="text" placeholder="User ID" style="width:500px; height:30px; text-align:center; font-size:24px; "/>
</td>
</tr>
<tr>
<td>
<input name="userpwd" type="password" placeholder="Password" style="width:500px; height:30px; text-align:center; font-size:24px; "/>
</td>
</tr>
<tr>
<td align="center">
<input type="submit" value="Submit" style="width:30%; height:30px; font-size:24px; " />
</td>
</tr>
</table>
</s:form>
</div>
</body>
</html>
<%--
Document : loginsuccess.jsp
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Success!</h1>
</body>
</html>
<%--
Document : loginfail.jsp
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Fail!</h1>
</body>
</html>
以上三个文件,LoginJSP.jsp为登录界面,loginsuccess.jsp为登录成功的页面,loginfail.jsp为登录失败的页面。
6. 编写登录Action,配置struts.xml文件。Action中的属性名称需要和jsp页面中form表单中的标签name值保持一致,这样才可以在action触发的时候获得相应的属性值,并执行execute()中的代码。注意,structs.xml文件中的路径填写是很容易出错的,要根据自己实际项目的包和文件夹路径来填写。
package Login;
import SQLHelper.SQLCon;
import com.opensymphony.xwork2.ActionSupport;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* @author falleyes
*/
public class LoginAction extends ActionSupport {
private String userid;
private String userpwd;
public LoginAction() {
}
//执行部分
public String execute() throws SQLException {
//新建连接
try {
new SQLCon();
} catch (Exception e) {
return INPUT;
}
//SQL语句
String sql = "select Count(*) as Total From attendance.admin Where id='"
+ userid + "' and pwd='" + userpwd + "'";
ResultSet rs = null;
//获得检索结果并返回结果字符串
try {
rs = SQLCon.getConnection().createStatement().executeQuery(sql);
if (rs.next()) {
return rs.getInt("Total") >= 1 ? SUCCESS : INPUT;
}
} catch (SQLException ex) {
return INPUT;
} finally {
SQLCon.CloseCon();
}
return INPUT;
}
/**
* @return the userid
*/
public String getUserid() {
return userid;
}
/**
* @param userid the userid to set
*/
public void setUserid(String userid) {
this.userid = userid;
}
/**
* @return the userpwd
*/
public String getUserpwd() {
return userpwd;
}
/**
* @param userpwd the userpwd to set
*/
public void setUserpwd(String userpwd) {
this.userpwd = userpwd;
}
}
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="base" extends="struts-default">
<action name="LoginAction" class="Login.LoginAction">
<result name="success">loginsuccess.jsp</result>
<result name="input">loginfail.jsp</result>
</action>
</package>
</struts>
7. 测试登录
测试输入"admin","admin",跳转到Success!的页面,输入其他,跳转到Fail!的页面。