MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写。
MVC 结构图
mvc的优点:
下面做一个简单的MVC模式下的用户登录。
1 建立动态web工程,名称为Struct2_login。
在 webContent目录下简历三个jsp文件。分别为login.jsp,success.jsp,error.jsp
在src 目录下简历如下图结构的包:
Po 包用于存放用户类等。
Servlet包用于存放创建建好的servlet.穿件servlet时会自动将servlet添加到web目录下。
Dao包用于处理相关业务。
最后结构图为:
其中login.jsp代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>标题</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<center>
<form name="login" action="UserServlet" method="post">
用户名:<input name="user" type = "text"/>
密码: <input name="password" type = "password"/>