基於struts2的日誌管理系統
既然是基於struts2框架做的,就必須瞭解struts框架。struts詳解:http://baike.baidu.com/link?url=thjh0ktwQ5cQhzKnUVmSdIqXicPUX_fhAnDTkYcuaPk-hqTAZVuDyMBuVGroODXbuGg5VaNSyaEXI8Y11EAWGa
先附上一張整體工程圖,功能主要包括登入註冊,使用者管理及日誌管理。
系統整體工程圖
首先是建立工程,配置好tomcat伺服器,匯入struts的包,以及連線上資料庫,我用的是mysql資料庫。
資料庫
一:下面開始附上程式碼:
1.登入介面login.jsp:
<form action="UserAction" method="post">
<div class="zhuce_kong login_kuang">
<div class="zc">
<div class="bj_bai">
<h3>登入</h3>
<input id="username" name="username" type="text" class="kuang_txt" placeholder="使用者名稱">
<input id="password" name="password" type="password" class="kuang_txt" placeholder="密碼">
<input id="submit" type="submit" class="btn radius btn-success btn-big" value=" 登 錄 ">
<input id="reset" type="reset" class="btn radius btn-default btn-big" value=" 取 消 ">
</div>
<div class="bj_right">
<p>如果還沒有賬號,請註冊</p>
<img src="images/logo.png" width="140" height="80" border="0">
<p>未有賬號?<a href="register.jsp">立即註冊</a></p>
</div>
</div>
<P>@qq.com 版權所有@:76班</P>
</div>
</form>
登入
2.註冊介面register.jsp
<form action="registerAction" method="post">
<div class="zhuce_kong login_kuang">
<div class="zc">
<div class="bj_bai">
<h3>註冊</h3>
<input id="username" name="username" type="text" class="kuang_txt" placeholder="使用者名稱">
<input id="password" name="password" type="password" class="kuang_txt" placeholder="密碼">
<input id="submit" type="submit" class="btn radius btn-success btn-big" value=" 注 冊 ">
<input id="reset" type="reset" class="btn radius btn-default btn-big" value=" 取 消 ">
</div>
<div class="bj_right">
<p>使用以下賬號直接登入</p>
<img src="images/logo.png" width="140" height="80" border="0">
<p>已有賬號?<a href="login.jsp">立即登入</a></p>
</div>
</div>
<P>@qq.com 版權所有@:76班</P>
</div>
</form>
註冊
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');
}
</script>
後臺管理介面
4.使用者管理介面customerManage.jsp
<tr>
<th style="text-align: center;"><%=rs.getString("username") %></th>
<th style="text-align: center;"><%=rs.getString("password") %></th>
<th style="text-align: center;">
<td width="50%">
<a href="updateUser.jsp?usernamesession=<%=rs.getString("username") %> ">更新</a>
<a href="deleteUser.jsp?usernamesession=<%=rs.getString("username") %> " onClick="return confirmDel()">刪除</a>
</td>
</tr>
使用者管理
5.日誌管理logManage.jsp
tr>
<th style="text-align: center;"><%=rs.getInt("id") %></th>
<th style="text-align: center;"><%=rs.getString("title") %></th>
<th style="text-align: center;"><%=rs.getString("content") %></th>
<th style="text-align: center;"><%=rs.getString("time") %></th>
<th style="text-align: center;"><%=rs.getString("author") %></th>
<th style="text-align: center;">
<td width="50%">
<a href="updateLog.jsp?logsession=<%=rs.getInt("id") %> ">更新</a>
<a href="deleteLog.jsp?logsession=<%=rs.getInt("id") %> " onClick="return confirmDel()">刪除</a>
</td>
</tr>
日誌管理
二:當然,某種程度來說,一個好的工程主要依靠配置檔案,並不能依賴於程式碼。。
使用struts框架,需要配置struts.xml跟web.xml配置檔案。
1.web.xml:
web.xml
2.struts.xml:
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("資料庫連線成功");
}
}