mvc理解

     MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一种业务逻辑、数据、界面显示分离的方法组织代码,将业务逻辑聚集到一个部件里面,在改进和个性化定制界面及用户交互的同时,不需要重新编写业务逻辑。MVC被独特的发展起来用于映射传统的输入、处理和输出功能在一个逻辑的图形化用户界面的结构中。


MVC编程模式


MVC 是一种使用 MVC(Model View Controller 模型-视图-控制器)设计创建 Web 应用程序的模式:

  • Model(模型)表示应用程序核心(比如数据库记录列表)。
  • View(视图)显示数据(数据库记录)。
  • Controller(控制器)处理输入(写入数据库记录)。

MVC 模式同时提供了对 HTML、CSS 和 JavaScript 的完全控制。

Model(模型)是应用程序中用于处理应用程序数据逻辑的部分。

通常模型对象负责在数据库中存取数据。

View(视图)是应用程序中处理数据显示的部分。

通常视图是依据模型数据创建的。

Controller(控制器)是应用程序中处理用户交互的部分。

通常控制器负责从视图读取数据,控制用户输入,并向模型发送数据

/MVC1/WebContent/JSP/  下的jsp页面

<%@ page language="java" contentType="text/html; charsetv=utf-8"

    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<form action="register.action" method="post">
用户名:<input type="text" name="username"/>
密码:<input type="password" name="pwd"/>
<input type="submit" value="提交">
</form>
</body>
</html>

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
  register_success.jsp
</body>
</html>


<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<form action=
"login.action" method="post">
用户名:<input type="text" name="username"/>
密码:<input type="password" name="pwd"/>
<input type="submit" value="提交">
</form>
</body>
</html>


<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
  login_success.jsp
</body>
</html>

//过滤器 过滤所有的*.action

@WebFilter(urlPatterns="*.action")
public class AllFiter implements Filter {


  
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException 
{
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;

String path = req.getRequestURL().toString();
System.out.println(path);//http://localhost:8081/MVC1/JSP/login.action

String page = path.substring(path.lastIndexOf("/")+1);
System.out.println(page);
Action action = null;
if (page.equals("login.action")) 
{
action = new LoginAction(); 
}
if(page.equals("register.action"))
{
action = new RegisterAction();
}
 
String url = action.execute(req, res);
res.sendRedirect(url);
}

............

}

 

package hu.action.inter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public
interface Action {
//
String execute(HttpServletRequest request,HttpServletResponse response);
}


public class RegisterAction implements Action {
@Override
public String execute(HttpServletRequest request,
HttpServletResponse response) {
// TODO Auto-generated method stub
return "register_success.jsp";  
}


}


package hu.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import hu.action.inter.Action;
public class LoginAction
implements Action{
@Override
public String execute(HttpServletRequest request,
HttpServletResponse response) {
// TODO Auto-generated method stub
return "login_success.jsp";
}

 }



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值