jsp+servlet+javabean实现ssh的mvc模式

干了很久的项目都是使用SSH框架的,现在团队有位搞美工的美女突然想要搞java了,让我做下指导并打个基本的框架,需要使用最基本的架构,这样说来那SSH只能滚得远远的了;
最基本的架构也就jsp+servlet+javabean,但心里感觉还是不爽,搞这个不是纯属浪费时间啊。后来一想,对了我自己可以不用框架来设计出一个简单的mvc架构来实现框架带来的分层效果:
这里我就以登录为例子来讲解下,讲解的顺序为v--->c---->m

页面端:login.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
<html>
<head>
<title>amdinLogin.html</title>
</head>

<body>
<form action="../AdminLogin.action" method="post">
<input type = "text" name = "username"/>
<input type = "password" name = "password"/>
<input type = "submit" name = "login" value = "注册"/>
</form>
</body>
</html>

配置文件web.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
    <servlet>     
<servlet-name>controlServlet</servlet-name>
<servlet-class>xidian.sl.equipment.Servlet.ControlServlet</servlet-class>
<!-- 这里的配置与一般的servlet一样,但由于整个项目只会有一个servlet,所有的访问都将访问这个servlet,因此不同的访问只需要添加下面的参数即可-->
<init-param>
<!-- 页面请求的地址-->
<param-name>AdminLogin</param-name>
<!-- 处理action的后台Action地址-->
<param-value>xidian.sl.equipment.action.LoginAction</param-value>
</init-param>
<init-param>
<param-name>picListAction</param-name>
<param-value>xidian.sl.equipment.action.PicListAction</param-value>
</init-param>
<init-param>
<param-name>newListAction</param-name>
<param-value>xidian.sl.equipment.action.NewListAction</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>controlServlet</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>

充当控制层的servlet:xidian.sl.equipment.Servlet.ControlServlet(很重要)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package xidian.sl.equipment.Servlet;  

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import xidian.sl.equipment.action.interfaces.Action;
import xidian.sl.equipment.actionfactory.ActionFactory;

public class ControlServlet extends HttpServlet{
/**
* 任何请求都会到这个servlet中,这个servlet就是充当MVC模式中的C(控制层)
*/
private static final long serialVersionUID = 1L;

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/**
* 得到当前Servlet的请求路径
* */
String pathName = request.getServletPath();
//System.out.println("pathName:"+pathName);
/**
* 得到请求的Action名字
* */
int index = pathName.indexOf(".");
String ActionName = pathName.substring(1, index);
//System.out.println(ActionName);
/**
* 获取运行时参数
* */
String ActionClassName = this.getInitParameter(ActionName);
//System.out.println("actionclassName "+ ActionClassName);
/**
* 得到Action对象
* */
Action action = ActionFactory.getActionFactory().getAction(ActionClassName);
//System.out.println("action "+action);
/**
* 执行Action的execute得到要返回的URL路径
* */
String url = action.execute(request, response);
if(url == null){
request.getRequestDispatcher("error.jsp").forward(request, response);
}else{
request.getRequestDispatcher(url).forward(request, response);
}
}
}

Action:面向接口编程,提供一个统一的Action接口,里面就一个方法execute();模拟struts1.x

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package xidian.sl.equipment.action.interfaces;  

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public interface Action {
/**
* 所有的具体Action实现这个接口
* @param request 请求对象
* @param response 应答对象
* @return :结果页面
*/
public String execute(HttpServletRequest request, HttpServletResponse response);

}

LoginAction:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package xidian.sl.equipment.action;  

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import xidian.sl.equipment.action.interfaces.Action;
import xidian.sl.equipment.dao.impl.AdminDAOImpl;
import xidian.sl.equipment.dao.interfaces.AdminDAO;

public class LoginAction implements Action {

@Override
public String execute(HttpServletRequest request,
HttpServletResponse response) {
/**
* 从request中得到相应的值
* */
String username = request.getParameter("username");
String password = request.getParameter("password");
/**
* 实例化DAO层,但其实这样做还是不完美的,最好自己写一个依赖注入的类
* */
AdminDAO adminDAO = new AdminDAOImpl();
/**
* 返回查找后的二维数组
* */
String[][] data = adminDAO.findAdmin(username, password);
if(data == null|| (data != null&& data.length == 0)){
//数组为空,表示无此账号存在
return "admin/login.html";
} else {
return "admin/index.html";
}
}

}

在实现类LoginAction中实例化了AdminDAO进行持久化操作,这里为了方便起见是直接使用new进行实例化,但这样硬编码带来耦合,如需要改进可以参考模拟spring的ioc
http://www.cnblogs.com/shenliang123/archive/2012/05/10/2494412.html 讨论简单工厂模式时有简单的实现,大家可以进行改进使其更加完美

AdminDAO:

1
2
3
4
5
6
7
8
package xidian.sl.equipment.dao.interfaces;  

public interface AdminDAO {
/**
* 根据用户名与密码来查找相应用户
* */
public String[][] findAdmin(String username, String password);
}

AdminDAOImpl:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package xidian.sl.equipment.dao.impl;  

import xidian.sl.equipment.dao.interfaces.AdminDAO;
import xidian.sl.equipment.util.DbConn;

public class AdminDAOImpl implements AdminDAO{

@Override
public String[][] findAdmin(String username, String password) {
String sql = "select * from admin as ad where ad.aId = '"+username+"' and ad.aPassword = '"+password+"'";
String[][] data = DbConn.query(sql);
return data;
}

}

该实现类中使用了已封装好的数据库操作类,详细见:http://www.cnblogs.com/shenliang123/archive/2012/05/10/2494874.html
数据库的设计比较简单:只需要主键id, 用户账号:username, 账号密码:password
这样就基本实现了mvc的效果,你还可以根据项目的复杂程度向DAO与Action之间再插入一层Service