MVC

来源:互联网 发布:网络使人更疏远的例子 编辑:程序博客网 时间:2024/05/21 10:06


MVC 结构图

    

    mvc的优点:

 

下面做一个简单的MVC模式下的用户登录。

建立动态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"/>

                                     <input type="submit" value="登录"/>

                   </form>

         </center>

</body>

</html>

 

success.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>

           <h1>登录成功-------</h1>

          

           <a href="<%=path%>/login.jsp" >返回</a>

         </body>

</html>

 

error.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>

         <h1>登录失败-------</h1>

        

           <a href="<%=path%>/login.jsp" >返回</a>

         </body>

</html>

 

Servlert 代码:

 

package Struct2_login.src.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 Struct2_login.src.dao.UserDao;

import Struct2_login.src.po.User;

 

/**

 Servlet implementation class UserServlet

 */

publicclass UserServletextends HttpServlet {

    privatestaticfinallongserialVersionUID = 1L;

      

    /**

     @see HttpServlet#HttpServlet()

     */

    public UserServlet() {

        super();

        //TODO Auto-generated constructor stub

    }

 

    /**

     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

     */

    protectedvoid doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {

       doPost(request,response);

    }

    /**

     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

     */

    protectedvoid doPost(HttpServletRequest request, HttpServletResponse response)throwsServletException, IOException {

       User us = new User();

       us.setUser(request.getParameter("user"));

       us.setPassword(request.getParameter("password"));

       UserDao usDao = new UserDao();

       Boolean test=usDao.checkUserPassword(us);

       if(test ==true){

           response.sendRedirect("success.jsp");

       }

       else{

           response.sendRedirect("error.jsp");

       }

    }

 

}

UserDao 代码:

 

package Struct2_login.src.dao;

 

import Struct2_login.src.po.User;

 

publicclass UserDao {

 

    public Boolean checkUserPassword(User user){

       if(user.getUser().equals("admin")&& user.getPassword().equals("123")){

           returntrue;     

       }

       else{

           returnfalse;

       }

    }

}

User.java 代码

package Struct2_login.src.po;

 

publicclass User {

    public Stringuser;

    public Stringpassword;

   

    public User(){   

    }

    public String getUser() {

       returnuser;

    }

    publicvoid setUser(String user) {

       this.user = user;

    }

    public String getPassword() {

       returnpassword;

    }

    publicvoid setPassword(String password) {

       this.password = password;

    }

   

}

 

将项目部署到tomcat下,然后启动tomcat

访问路径为:http://localhost:8080/Struct2_login/

Struct2_login 为项目名。

 




jsp对应mvc的v层,就是视图层,用来显示用户界面servlet是万能的,你想要它做什么都可以,jsp编译后也是生成servlet,不过,一般来说用来做mvc的c层,就是控制层,用来调用不同的业务逻辑JaveBean对应mvc的m层,就是数据层,负责根据业务逻辑处理数据以上是我自己归纳的,意思应该差不多,呵呵



·                    表示层:负责直接跟用户进行交互,一般也就是指系统的界面,用于数据录入,数据显示等。意味着只做与外观显示相关的工作,不属于他的工作不用做。

·                   业务逻辑层:用于做一些有效性验证的工作,以更好地保证程序运行的健壮性。如完成数据添加、修改和查询业务等;不允许指定的文本框中输入空字符串,数据格式是否正确及数据类型验证;用户的权限的合法性判断等等,通过以上的诸多判断以决定是否将操作继续向后传递,尽量保证程序的正常运行。

·                   数据访问层:顾名思义,就是用于专门跟数据库进行交互。执行数据的添加、删除、修改和显示等。需要强调的是,所有的数据对象只在这一层被引用,如System.Data.SqlClient等,除数据层之外的任何地方都不应该出现这样的引用。


0 1