配置环境:MyEclipse 2017 CI、SQL Server 2014、以及12个Struts2的jar文件包(附链接)
项目打包上传于:https://download.csdn.net/download/qq_40956679/10819079
项目文件清单:
文件简述:
*****src*****
1.User_Action.java (控制类——Action类)User.java
package Action;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.websocket.Session;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
import beans.User;
import db.ConnectDbase;
import sql.Moye_sql;
public class User_Action extends ActionSupport{
private HttpServletRequest request;
//调用列出数据库全部记录的方法
public String findAll() throws Exception{
request = ServletActionContext.getRequest();
Moye_sql findAll = new Moye_sql();
List user = new ArrayList();
try{
user = findAll.findAll();
request.setAttribute("list", user);
}catch(Exception e){
e.printStackTrace();
}
return "success";
}
//调用向数据库查询记录的方法
public String find() throws Exception{
request = ServletActionContext.getRequest();
User user = new User();
user.setXm(request.getParameter("xm"));
Moye_sql find = new Moye_sql();
User student = find.find(user);
request.setAttribute("stu", student);
return "success";
}
//调用向数据库添加记录的方法
public String create() throws Exception{
request = ServletActionContext.getRequest();
User user = new User();
user.setXm(request.getParameter("xm"));
user.setMm(request.getParameter("mm"));
Moye_sql create = new Moye_sql();
User student = create.create(user);
request.setAttribute("stu", student);
return "success";
}
//调用向数据库删除记录的方法
public String delete() throws Exception{
request = ServletActionContext.getRequest();
User user = new User();
user.setXm(request.getParameter("xm"));
Moye_sql delete = new Moye_sql();
delete.delete(user);
request.setAttribute("stu", user);
return "success";
}
//验证是否管理员
public String yanzheng() throws Exception{
request = ServletActionContext.getRequest();
User user = new User();
user.setXm(request.getParameter("xm"));
user.setMm(request.getParameter("mm"));
String xm = user.getXm();
String mm = user.getMm();
if(xm.equals("admin")&&mm.equals("admin")){
System.out.print("您为管理员,自动跳转管理员页面!");
return "admin";
}else{
System.out.print("您为用户会员,自动跳转会员登录页面!");
return "error";
}
}
}
2.User.java (建立对应数据库表结构的JavaBean——用户类User)
package beans;
public class User {
private String xm = null;
private String mm = null;
public String getXm() {
return xm;
}
public void setXm(String xm) {
this.xm = xm;
}
public String getMm() {
return mm;
}
public void setMm(String mm) {
this.mm = mm;
}
}
3.ConnectDbase.java (实现数据库连接与关闭的java类)
package db;
import java.sql.*;
import beans.User;
public class ConnectDbase {
private static String diverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; // 驱动程序名
private static String userName = "sa"; // 用户名
private static String userPwd = "125113jie@"; // 密码
//实现数据库连接的方法
public static Connection getConnect() {
String url1 = "jdbc:sqlserver://localhost:1433;DatabaseName=MOYE";
try{
Class.forName(diverName);
Connection con = DriverManager.getConnection(url1,userName,userPwd);
return con;
}catch(Exception e){
e.printStackTrace();
}
return null;
}
public static void closeDB(Connection con,PreparedStatement pstm,ResultSet rs){
try{
if(rs!=null)rs.close();
if(pstm!=null)pstm.close();
if(con!=null)con.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
4.Moye_sql.java (对各种数据库操作进行访问逻辑处理的类Moye_sql)
package sql;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import db.ConnectDbase; //导入连接类ConnectDbase
import beans.User; //导入JavaBean User类
public class Moye_sql {
protected static final String FIELDS_INSERT="Uno,Upwd";
protected static String INSERT_SQL="insert into moye_user("
+FIELDS_INSERT+")"+"values(?,?)"; //SQL插入语句(用户注册)
protected static String SELECT_SQL="select "
+FIELDS_INSERT+" from moye_user where Uno=?"; //SQL查询语句
protected static String DELETE_SQL="delete from moye_user where Uno=?"; //SQL删除语句
//实现列出数据库全部记录的方法
public List findAll()throws Exception{
Connection con = null;
PreparedStatement prepStmt = null;
ResultSet rs = null;
List user = new ArrayList();
con = ConnectDbase.getConnect();
prepStmt = con.prepareStatement("select * from moye_user");
rs = prepStmt.executeQuery();
while(rs.next()){
User stu2 = new User();
stu2.setXm(rs.getString(1));
stu2.setMm(rs.getString(2));
user.add(stu2); //将引用添加到集合中
}
ConnectDbase.closeDB(con, prepStmt, rs);
return user;
}
//实现向数据库查询记录的方法
public User find(User stu)throws Exception{
Connection con = null;
PreparedStatement prepStmt = null;
ResultSet rs = null;
User stu2 = null;
try{
con = ConnectDbase.getConnect();
prepStmt = con.prepareStatement(SELECT_SQL);
prepStmt.setString(1,stu.getXm());
rs = prepStmt.executeQuery();
if(rs.next()){
stu2 = new User();
stu2.setXm(rs.getString(1));
stu2.setMm(rs.getString(2));
}
}catch(Exception e){
//handle exception
}finally{
ConnectDbase.closeDB(con, prepStmt, rs);
}
return stu2;
}
//实现向数据库添加记录的方法
public User create(User stu)throws Exception{
Connection con = null;
PreparedStatement prepStmt = null;
ResultSet rs = null;
try{
con = ConnectDbase.getConnect();
prepStmt = con.prepareStatement(INSERT_SQL);
prepStmt.setString(1, stu.getXm());
prepStmt.setString(2, stu.getMm());
prepStmt.executeUpdate();
}catch(Exception e){
}finally{
ConnectDbase.closeDB(con, prepStmt, rs);
}
return stu;
}
//实现删除数据库指定的记录方法
public void delete(User stu)throws Exception{
Connection con = null;
PreparedStatement prepStmt = null;
ResultSet rs = null;
try{
con = ConnectDbase.getConnect();
prepStmt = con.prepareStatement(DELETE_SQL);
prepStmt.setString(1, stu.getXm());
prepStmt.executeUpdate();
}catch(Exception e){
//
}finally{
ConnectDbase.closeDB(con, prepStmt, rs);
}
}
}
5.struts.xml(strutsi配置文件配置动态Action)
/moye_main.jsp
/moye_login_Success.jsp
/moye_findAll_Success.jsp
/moye_find_Success.jsp
/moye_create_Success.jsp
/moye_delete_Success.jsp
*****4个CSS文件*****
1.Css_zhuye.css (修饰主页 zhuye.jsp)
@CHARSET "UTF-8";
/*
**给主页修饰
*/
a{ /*超链接标签*/
margin-left:300px;
font-size: 20px;
font-family:宋体;
color: #e1dbde;
text-decoration: none;
id:bian;
onmouseover:change();
onmouseout:back();
}
a.root{
margin-left:30px;
font-size: 20px;
font-family:宋体;
color: #e1dbde;
text-decoration: none;
}
a.right{
margin-left:100px;
font-size: 30px;
height:80px;
font-family:宋体;
color: #e1dbde;
text-decoration: none;
}
img{ /*背景图片格式*/
width:90%;
margin-left:80px
}
img.logo{ /*logo格式*/
width:90px;
height:60px;
}
div.top{ /*top标签*/
margin-left:-10px;
margin-top:-4px;
float:left;
align:center;
width: 800px;
line-height:50px;
width:110%;
height:50px;
background:#000;
color:#FFF;
}
div.logo{ /*第一个间隙*/
width:100%;
height:120px;
background:#ffffff;
color:#000000;
}
div.logoRight{
margin-left:50px;
font-size: 18px;
font-family:微软雅黑;
}
div.logo2{ /*第二个间隙*/
width:100%;
height:50px;
background:#ffffff;
color:#000000;
}
div.root{ /*底端间隙*/
margin-left:-10px;
width:110%;
height:200px;
background:#3c3c3c;
color: #ffffff;
}
2.Css_login.css (修饰登录页面 login.jsp)
@charset "UTF-8";
/*
**给登录页面修饰
*/
body{
background-image:url(image/beijing3.jpg); /*图片地址*/
background-origin:content; /*从content区域开始显示背景*/
background-position:50% 50%; /* 图片上下左右居中 */
background-size:cover; /* 保持图像本身的宽高比例,将图片缩放到宽度或高度正好适应定义背景的区域 */
background-repeat:no-repeat; /* 图像不重复显示 */
}
table {
background-color: rgba(255, 255, 255, 0.9);
margin-top:200px;
margin-left: center;
height: 50px;
width: 300px;
border-radius: 6px;
}
td.{
text-align: center;
align: center;
font-family: 微软雅黑;
font-size: 20px;
}
td.left{
align: center;
font-family: 微软雅黑;
font-size: 25px;
color: #0080ff;
}
tr {
align: center;
}
input.normal {
align: center;
height: 40px;
width: 250px;
font-size: 20px;
background: none;
border: 1;
font-family: 微软雅黑;
border-radius: 6px;
}
a {
font-family: 微软雅黑;
text-decoration: none;
font-size: 28px;
font-weight: bold;
color: dodgerblue;
}
input.submit {
align:center;
background:#0080ff;
width: 100px;
font-size: 25px;
font-family:微软雅黑;
color: #ffffff;
border:1px;
border-radius: 6px;
}
3.Css_moye.css (修饰框架和操作表单页面 moye_main.jsp、moye_right.jsp、moye_top.jsp、moye_left.jsp、SQL_find.jsp、 SQL_delete.jsp、SQL_create.jsp)
@CHARSET "UTF-8";
/*
**给JSP框架页面修饰
*/
body.normal{
background-image:url(image/moye.jpg); /*图片地址*/
background-size:cover; /* 保持图像本身的宽高比例,将图片缩放到宽度或高度正好适应定义背景的区域 */
background-repeat:no-repeat; /* 图像不重复显示 */
}
body.top{
background-color:#000000;
}
form{
margin-top:80px;
}
div.top{
color:#FFF;
margin-left:35%;
font-size: 30px;
font-family:微软雅黑;
}
img.logo{ /*logo格式*/
width:90px;
height:60px;
}
input{
margin-top:100px;
margin-left:5px;
font-size: 20px;
font-family:宋体;
color: #e1dbde;
background-color:#000000;
text-decoration: none;
}
a{
margin-left:30px;
font-size: 20px;
font-family:宋体;
color: #e1dbde;
text-decoration: none;
}
4.Css_sql.css (修饰各个数据库操作成功页面 moye_login_Success.jsp、moye_findAll_Success.jsp、 moye_create_Success.jsp、moye_find_Success.jsp、moye_delete_Success.jsp)
@CHARSET "UTF-8";
/*
**给各个数据库操作方法页面修饰
*/
body{
background-image:url(image/beijing2.jpg); /*图片地址*/
background-size:cover; /* 保持图像本身的宽高比例,将图片缩放到宽度或高度正好适应定义背景的区域 */
background-repeat:no-repeat; /* 图像不重复显示 */
}
table {
background-color: rgba(255, 255, 255, 0.8); /*模糊背景90%*/
margin-top:200px;
margin-left: center;
height: 40px;
width: 480px;
border-radius: 6px;
}
table.findAll{
background-color: rgba(255, 255, 255, 0.8); /*模糊背景90%*/
margin-top:50px;
margin-left: center;
height: 40px;
width: 480px;
border-radius: 6px;
}
td.text{
text-align: center;
font-family: 微软雅黑;
font-size: 20px;
height:20px;
width:180px;
}
td.findAll{
text-align: center;
font-family: 微软雅黑;
font-size: 16px;
height:15px;
width:180px;
}
td.findAll_left{
align: center;
color: #0080ff;
text-align: center;
font-family: 微软雅黑;
font-size: 20px;
}
td.left{
align: center;
font-family: 微软雅黑;
font-size: 20px;
color: #0080ff;
}
input.normal {
height: 30px;
width: 180px;
font-size: 15px;
background: none;
border: 1;
font-family: 微软雅黑;
border-radius: 6px;
}
input.submit {
align:center;
background:#0080ff;
width: 100px;
font-size: 20px;
font-family:微软雅黑;
color: #ffffff;
border:1px;
border-radius: 6px;
}
*****1个JavaScript文件*****
Script.js (实现表单用户名、密码为空提示和按钮变色功能)
/**
*
*/
function on_submit() { //验证数据的合法性
if (form1.xm.value == "") {
alert("请输入用户名!");
form1.xm.focus();
return false;
}
if (form1.mm.value == "") {
alert("请输入登录密码!");
form1.mm.focus();
return false;
}
}
function change(){ //鼠标移入变色
var tj=document.getElementById("bian");
tj.style.color="blue";
}
function back(){ //鼠标移出变色
var tj=document.getElementById("bian");
tj.style.color="#ffffff";
}
--------------------------------------因页面篇幅太大,分成两篇博客--------------------------------------
附下篇链接:https://blog.csdn.net/qq_40956679/article/details/84648071