Jsp+Struts2+JavaBean+DAO开发模式(1)

DAO模式就实现了把数据库表的操作转化对Java类的操作,从而提高程序的可读性,并实现更改数据库的方便性。其架构图如下图。

 一共分为五个组件(component)

jsp提交页面(一下四其中的一个例子,HTML的例子)

 

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <html>
 4 <head> <title>添加信息提交页面</title>
 5 <style type="text/css">
 6 body{  
 7      background-image:url(src.img/6.jpg); /*图片地址*/                             
 8      background-origin:content;   /*从content区域开始显示背景*/  
 9      background-position:50% 5%; /*             图片上下左右居中  */
10      background-size:cover;       /*           保持图像本身的宽高比例,将图片缩放到宽度或高度正好适应定义背景的区域  */
11      background-repeat:no-repeat;     /*         图像不重复显示  */
12 }
13     td{font-size: 12px;}
14     h2{margin: 0px}
15     table {
16     background-color: rgba(255, 255, 255, 0.9);
17     margin-top:200px;
18     margin-left: center;
19     height: 50px;
20     width: 300px;
21     border-radius: 6px;
22 }
23     td.{
24     text-align: center;
25     align: center;
26     font-family: 微软雅黑;
27     font-size: 20px;
28 }
29     input.submit {
30     align:center;
31     background:#0080ff;
32     width: 100px; 
33     font-size: 25px; 
34     font-family:微软雅黑;
35     color: #ffffff;
36     border:1px;
37     border-radius: 6px;
38 }
39 </style> 
40 </head>
41 <body>
42 <form action="jsreg" method="post">
43 <table border="0" width="238" height="252">
44 <tr><td>教师</td><td><input type="text" name="username"></td></tr>
45 <tr><td>密码</td><td><input type="text" name="ps"></td></tr>
46 <tr align="center">
47 <td colspan="2">
48 <input type="submit" value="登录">
49 </td>
50 <td>
51 <a href="updateteacher.jsp" >忘记密码?</a>
52 </td>
53 </tr>
54 </table>
55 </form>
56 </body>
57 </html>

Struts.xml的配置(Congfiguration)文件

    </action>
            <action name="jsreg" class="Action.ther">
            <result name="success">/index_student_left.jsp</result>
        </action>

JavaBean如下

public class Teacher {
    private String username;
    private String ps;
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPs() {
        return ps;
    }
    public void setPs(String ps) {
        this.ps = ps;
    }
}

接口类型如下

import domain.Teacher;

public interface Iteacher {
    public abstract    Teacher find (Teacher te) throws Exception;
}

DAO如下

package dao;

import java.sql.*;
import db.DbConnect;
import domain.Teacher;

public  class TeacherDAO implements Iteacher{
    protected static final String FIELDS_INSERT="username,ps";
    protected static String SELECT_SQL="select "+FIELDS_INSERT+" from Teacher where username=?";
    protected static String UPDATE_SQL="update Teacher set "+"username=?,ps=? where username=?";
    public Teacher find(Teacher te) throws Exception {
        Connection con=null;
        PreparedStatement prepStmt=null;
        ResultSet rs=null;
        Teacher te2=null;
        try{
            con=DbConnect.getDBconnection();
            prepStmt=con.prepareStatement(SELECT_SQL);
            prepStmt.setString(1, te.getUsername());
            rs=prepStmt.executeQuery();
            if(rs.next()){
                te2=new Teacher();
                te2.setUsername(rs.getString(1));
                te2.setPs(rs.getString(2));
            }
        }catch(Exception e){
        }finally{
            DbConnect.closeDB(con, prepStmt, rs);
        }
        return te2;
    }

连接数据库的Dbconnect

package db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DbConnect {
    private static String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
    private static String userName="sa";
    private static String userpwd="13481195624q";
    private static String dbName="Student";
    public    static Connection getDBconnection() {
    String dbURL="jdbc:sqlserver://localhost:1433;DatabaseName=Student";
         try {
              Class.forName(driverName);
              Connection con=DriverManager.getConnection(dbURL,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();
             }
        }
    }

执行Action页面

package Action;

import dao.TeacherDAO;
import domain.Teacher;
import java.util.Map;
import java.util.regex.*;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;


public class ther extends ActionSupport{
    private HttpServletRequest request;
    public String execute() throws Exception {
    request=ServletActionContext.getRequest();
    Teacher te=new Teacher();
    te.setUsername(request.getParameter("username"));
    te.setPs(request.getParameter("ps"));
    TeacherDAO te2=new TeacherDAO();
    try {
        te=te2.find(te);
        request.setAttribute("teacher", te);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "success";
    }
}

 

显示的JSP页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="java.util.List"%>
<%@page import="domain.Teacher"%>
<html>
<head><title>所有学生信息</title>
<style type="text/css">
body{  
     background-image:url(src.img/7.jpg); /*图片地址*/                             
     background-origin:content;   /*从content区域开始显示背景*/  
     background-position:50% 5%; /*             图片上下左右居中  */
     background-size:cover;       /*           保持图像本身的宽高比例,将图片缩放到宽度或高度正好适应定义背景的区域  */
     background-repeat:no-repeat;     /*         图像不重复显示  */
}
    td{font-size: 12px;}
    h2{margin: 0px}
    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;
}
</style>
</head>
<body>
    <table align="center" width="450" border="1" height="180"
        bordercolor="white" bgcolor="black" cellpadding="1" cellspacing="1">
        <tr bgcolor="white">
            <td align="center" colspan="5">
                <h2>所有教师信息</h2>
            </td>
        </tr>
        <tr align="center" bgcolor="#e1ffc1">
            <td><b>姓名</b></td>
            <td><b>密码</b></td>
        </tr>
        <%Teacher teacher=(Teacher)request.getAttribute("teacher");%>
                <tr align="center" bgcolor="white">
                    <td><%=teacher.getUsername()%></td>
                    <td><%=teacher.getPs()%></td>
                </tr>
    </table>
</body>
</html>

教师信息如下

posted on 2018-11-30 22:56  马家升  阅读(1004)  评论(0编辑  收藏  举报