JSP代码 MVC编程开发实例
我之前一直搞不明白JAVABEAN在MVC中到底充当一个什么角色。现在来看一个实例定义一个JAVABEAN
package com.bean;
public class User {
private String userName;
private String userPwd;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPwd() {
return userPwd;
}
public void setUserPwd(String userPwd) {
this.userPwd = userPwd;
}
}
JAVABEAN的作用:是一个PO对象之前我以为是VO对象。搞错了!
恩我们可以这样来理解JAVABEAN的作用其实就是用来传递数据的一个东西。在我们这次PHPCMS项目中相当于一个结果集的传输。控制器接收到一个来自M层的数据之后如何传递到视图层作渲染处理。可以依据这样的方式。
再来看我们的Action类
package com.dao;
import java.sql.*;
import com.bean.User;
public class DAO {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
PreparedStatement pstmt = null
public DAO(){
try {
Class.forName("com.mysql.jdbc.Driver");
conn = getConnection();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Connection getConnection(){
String url = "jdbc:mysql://localhost:3306/person";
String user = "root";
String password = "123456";
try {
conn = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
public int addPerson(User user){
int flag = 0;
String sql = "insert into user(user_name,user_pwd) values(?,?)";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, user.getUserName());
pstmt.setString(2, user.getUserPwd());
flag = pstmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return flag;
}
public ResultSet showPerson(){
String sql = "select * from user";
try {
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}
public void close(){
try {
if(rs!=null)rs.close();
if(pstmt!=null)pstmt.close();
if(conn!=null)conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
这个类把数据库连接操作也放在这里了。其实是不正确的处理方式。正确的方式应当是将数据库层单独写到一个CLASS类里面去。但这里面我们来看一下:
public int addPerson(User user){
int flag = 0;
String sql = "insert into user(user_name,user_pwd) values(?,?)";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, user.getUserName());
pstmt.setString(2, user.getUserPwd());
flag = pstmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return flag;
}
这个DAO对象它的作用是接收来自JAVABEAN层的数据。
再看是如何SET值到这个BEAN的
package com.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.bean.User;
import com.dao.DAO;
public class AddUser extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String userName = request.getParameter("userName");
String userPwd = request.getParameter("userPwd");
User user = new User();
user.setUserName(userName);//依据的是SET与GET方式了吧!!!!
user.setUserPwd(userPwd);
DAO dao = new DAO
补充:Web开发 , Jsp ,