既然是基于struts2框架做的,就必须了解struts框架。struts详解:http://baike.baidu.com/link?url=thjh0ktwQ5cQhzKnUVmSdIqXicPUX_fhAnDTkYcuaPk-hqTAZVuDyMBuVGroODXbuGg5VaNSyaEXI8Y11EAWGa
先附上一张整体工程图,功能主要包括登录注册,用户管理及日志管理。
首先是建立工程,配置好tomcat服务器,导入struts的包,以及连接上数据库,我用的是mysql数据库。
一:下面开始附上代码:
1.登录界面login.jsp:
2.注册界面register.jsp
3.后台管理界面index.jsp
var _menus = {
"menus":[
{"menuid":"1","icon":"icon-sys","menuname":"日志管理",
"menus":[
{"menuid":"11","menuname":"查看日志信息","icon":"icon-nav","url":"logManage.jsp"},
{"menuid":"12","menuname":"上传日志信息","icon":"icon-nav","url":"addLog.jsp"},
{"menuid":"13","menuname":"修改日志信息","icon":"icon-nav","url":"updateLog.jsp"}
]
},{"menuid":"2","icon":"icon-sys","menuname":"用户管理",
"menus":[
{"menuid":"21","menuname":"查看用户信息","icon":"icon-set","url":"customerManage.jsp"},
{"menuid":"22","menuname":"删除用户信息","icon":"icon-role","url":"deleteUser.jsp"},
{"menuid":"23","menuname":"修改用户信息","icon":"icon-log","url":"updateUser.jsp"}
]
}
]};
//关闭登录窗口
function closePwd() {
$('#w').window('close');
}
4.用户管理界面customerManage.jsp
" onClick="return confirmDel()">删除
5.日志管理logManage.jsp
tr>
" onClick="return confirmDel()">删除
二:当然,某种程度来说,一个好的工程主要依靠配置文件,并不能依赖于代码。。
使用struts框架,需要配置struts.xml跟web.xml配置文件。
1.web.xml:
2.struts.xml:
三:登录注册功能java源代码:
1.登录:
import java.sql.*;
import com.opensymphony.xwork2.ActionSupport;
import com.javaweb.database.*;
@SuppressWarnings("serial")
public class UserAction extends ActionSupport{
private DBconn dao = new DBconn();
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 String login() {
String sql = "select * from tab_user where username='" + getUsername()+"' and password ='"+getPassword()+"'";
ResultSet rS = dao.executeQuery(sql);
try {
if (rS.next()) {
System.out.println("hhhh");
return "success";
}
return "error";
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "error";
}
}
}
2.注册:
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import com.javaweb.database.DataBase;
public class registerAction{
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 String regist(){
String sql = "insert into tab_user " + " (username,password)" +" values (?,?)";
Connection conn=DataBase.getConn();
PreparedStatement ps;
if(username == null||username.length()==0||password == null||password.length()==0){
return "error";
}
try {
ps = conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
ps.setString(1, username);
ps.setString(2, password);
ps.executeUpdate();
return "success";
} catch (SQLException e) {
e.printStackTrace();
return "error";
}
}
}
3.数据库连接:
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
public class DataBase {
private final static String driver = "com.mysql.jdbc.Driver";
private final static String url = "jdbc:mysql://localhost:3306/hun_aqt";
private final static String user = "root";
private final static String pwd = "123456";
public static Connection getConn() {
try {
Class.forName(driver);
return DriverManager.getConnection(url,user,pwd);
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
public static void closeConn(ResultSet rs,PreparedStatement pstmt,Connection conn) {
try {
if(rs!=null){
rs.close();
}
if(pstmt!=null){
pstmt.close();
}
if (conn != null && conn.isClosed() == false) {
conn.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
DataBase.getConn();
System.out.println("数据库连接成功");
}
}