MVC模式分析与实现Word文档格式.docx
《MVC模式分析与实现Word文档格式.docx》由会员分享,可在线阅读,更多相关《MVC模式分析与实现Word文档格式.docx(10页珍藏版)》请在冰豆网上搜索。
<
1>
、ControllerServlet.java:
请求分发控制器,解析请求中的动作名字,调用动作工厂对象生成的动作对象处理请求,处理后返回的结果为目的URL,控制器再将请求和应答对象转向目标URL
2>
、ActionFactory.java:
该工厂类将请求中的动作名转换成servlet可以用来完成其工作的动作类
3>
、Action.java:
接口类,该接口定义所有动作的公共接口
4>
、具体Action的实现:
指实现了Action接口的类,会被ActionFactory工厂根据请求中的名字创建,从而调用其具体实现的方法处理Reques\Response对象后,返回一个URL,由主控servlet转发给用户。
可以理解为一个具体的Action实现类用于处理页面上每一种用户点击(请求)
5>
、系统视图View层实现:
*.JSP、*.HTML
2)实现源码:
<
1>
控制器Servlet实现
Java代码
1.package
wxy.MVCDemo.Servlet;
2.
3.import
java.io.IOException;
4.import
javax.servlet.ServletException;
5.import
javax.servlet.http.HttpServlet;
6.import
javax.servlet.http.HttpServletRequest;
7.import
javax.servlet.http.HttpServletResponse;
8.
9.import
wxy.MVCDemo.action.Action;
10.import
wxy.MVCDemo.action.ActionFactory;
11.
12./**
13.
*
系统控制器实现
14.
功能:
15.
1、解析请求中的命令名
16.
2、根据请求的命令调用工厂对象创建命令处理对象
17.
3、调用命令对象处理请求和应答对象,返回目标路径名
18.
4、本控制器将请求和应答转发至目标路径名
19.
Servlet
implementation
class
ControllerServlet
20.
*/
21.public
extends
HttpServlet
{
22.
private
static
final
long
serialVersionUID
=
1L;
23.
24.
public
ControllerServlet()
25.
super();
26.
}
27.
/**
28.
主控servlet分发用户请求的入口
29.
30.
void
service(HttpServletRequest
request,HttpServletResponse
response)throws
ServletException,IOException{
31.
try{
32.
//得到请求的命令名字:
解析出*.do请求中*所指代的字符串,即动作名字
33.
String
ActionName=getActionName(request);
34.
System.out.println("
request
Action
is:
"
+ActionName);
35.
//根据请求的动作名,得到控制器servlet中配置的Action实现类名字
36.
ActionClassName=this.getInitParameter(ActionName);
37.
ActionClassName
+ActionClassName);
38.
//通过工厂类创建命令对象
39.
action=ActionFactory.getIns().getAction(ActionClassName);
40.
//使用动作对象处理动作,返回结果为处理后要输出的目标页面
41.
URL=action.execute(request,response);
42.
if(URL!
=null){
43.
//输出目标页面
44.
dest
URL
is
:
+URL);
45.
getServletContext().getRequestDispatcher(URL).forward(request,
response);
46.
47.
}catch(Exception
e){
48.
e.printStackTrace();
49.
getServletContext().getRequestDispatcher("
/error.jsp"
).forward(request,
50.
51.
52.
53.
解析到请求中的“命令”字
54.
@param
55.
@return
56.
57.
protected
getActionName(HttpServletRequest
request)
58.
path=request.getServletPath();
59.
return
path.substring(1,path.lastIndexOf("
."
));
60.
61.}
2>
Action对象工厂类实现
wxy.MVCDemo.action;
2./**
3.
命令对象工厂类实现
4.
根据命令名字,创建命令对象
5.
ActionFactory是一个单实例类(整个系统只需要使用其一个对象)
6.
@author
wxy
7.
9.public
ActionFactory
10.
单实例访问方法
12.
单实例对象
getIns(){
if(null==ac){
ac=new
ActionFactory();
ac;
21.
根据具体的action类名字创建Action对象
actionClass
具体的Action类全名
Action类型对象
getAction(String
actionClass){
actionInstance=null;
Class
c=Class.forName(actionClass);
actionInstance=(Action)c.newInstance();
actionInstance;
//不需要创建对象
ActionFactory(){}
ac=null;
38.}
3>
Action接口类定义
6./**
系统中的命令处理器接口
9.
11.public
interface
所有的具体Action实现这个接口
请求对象
reponse
应答对象
结果页面
execute(HttpServletRequest
reponse);
19.}
web.xml配置
1.<
?
xml
version="
1.0"
encoding="
UTF-8"
>
2.<
web-app
xmlns:
xsi="
xmlns="
web="
xsi:
schemaLocation="
id="
WebApp_ID"
2.5"
servlet>
description>
/description>
display-name>
控制器ControllerServlet<
/display-name>
servlet-name>
ControllerServlet<
/servlet-name>
servlet-class>
wxy.MVCDemo.Servlet.ControllerServlet<
/servlet-class>
init-param>
!
--
将具体Action名字和全类配置到servlet参数中
-->
param-name>
loginAction<
/param-name>
param-value>
java.action.LoginAction<
/param-value>
/init-param>
/servlet>
MVCWeb<
welcome->
welcome-<
/welcome-file>
/welcome->
将所有以.do路径结尾的请求发送到主控servlet处理
servlet-mapping>
url-pattern>
*.do<
/url-pattern>
/servlet-mapping>
23.<
/web-app>
4>
具体的Action实现类
由login.jsp输出账户密码,如果账号正确,则在Session中放入用户对象,转向主页;
如果错误,转向登录页面
LoginAction
implements
Action{
request,
HttpServletResponse
reponse)
UserName=request.getParameter("
UserName"
);
pwd=request.getParameter("
pwd"
//数据库操作
//..........
if(UserName.equals("
wxy"
)&
&
pwd.equals("
)){
//将用户对象放入session
request.getSession().setAttribute("
UserName);
/mainPage.jsp"
;
ERROR_MSG"
账号或者密码输入有误!
/login.jsp"
28.}
RegUserAction.do
DisplayMainPageAction.do
。
。
(记着在web.xml中配置)
5>
系统view层实现
login.jsp
%@
page
language="
java"
contentType="
text/html;
charset=utf-8"
pageEncoding="
utf-8"
%>
3.<
DOCTYPE
html
PUBLIC
-//W3C//DTD
HTML
4.01
Transitional//EN"
4.<
html>
5.<
head>
6.<
meta
http-equiv="
Content-Type"
content="
7.<
title>
用户登录<
/title>
8.<
/head>
9.<
body>
10.<
form
action="
loginAction.do"
method="
post"
用户名:
input
type="
text"
name="
userName"
br>
密码:
password"
userPwd"
submit"
value="
登录"
/>
14.<
/form>
15.<
/body>
16.<
/html>
mainPage.jsp
Insert
title
here<
h1
style="
color:
red"
%=request.getAttribute("
)
登录成功<
/h1>
11.<
12.<