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

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

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

一共分为五个组件(component)

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

 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head> <title>添加信息提交页面</title>
<style type="text/css">
body{
background-image:url(src.img/6.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;
}
input.submit {
align:center;
background:#0080ff;
width: 100px;
font-size: 25px;
font-family:微软雅黑;
color: #ffffff;
border:1px;
border-radius: 6px;
}
</style>
</head>
<body>
<form action="jsreg" method="post">
<table border="0" width="238" height="252">
<tr><td>教师</td><td><input type="text" name="username"></td></tr>
<tr><td>密码</td><td><input type="text" name="ps"></td></tr>
<tr align="center">
<td colspan="2">
<input type="submit" value="登录">
</td>
<td>
<a href="updateteacher.jsp" >忘记密码?</a>
</td>
</tr>
</table>
</form>
</body>
</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>

教师信息如下

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

上一篇:Swift语言学习


下一篇:JavaScript中call,apply,bind方法