码迷,mamicode.com
首页 > Web开发 > 详细

MVC设计模式-学习笔记

时间:2018-04-18 15:57:21      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:MVC设计模式-学习笔记

1)如果严格按照MVC思想的话,浏览器首页访问的是控制器,然后再由控制器转发到视图
2)位于WEB-INF/目录下的资源,客户端无法直接访问,只能通过服务端转发进入
3)request对象也可用于资源的转发
request
.getRequestDispatcher("/WEB-INF/success.html")
.forward(request,response);
4)通过正则表达式验证中文:[\u4E00-\uFA29]+
5)对于重定向而言,前后的二个Request域对象不同,如果取值不到,返回"null",不会报错

java代码:

ModelBean

public class ModelBean {
    public boolean validate(String username){
        boolean flag = false;
        //username.matches("[a-zA-Z0-9]+")) 用户名是数字与字母
        if(username!=null && username.matches("[\u4E00-\uFA29]+")){
            flag = true;
        }
        return flag;
    }
}

Demo8


import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.itcast.web.domain.ModelBean;

public class Demo8 extends HttpServlet {
    //NO1
    public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        //转发到view.html页面
        RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/view.html");
        rd.forward(request,response);
    }
    //NO2
    public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String username = request.getParameter("username");
        ModelBean mb = new ModelBean();
        boolean flag = mb.validate(username);
        if(flag){
            //将用户名绑定到Request域对象中
            request.setAttribute("USERNAME",username);

            /*转发到ListServlet.java
            request
                .getRequestDispatcher("/ListServlet")
                .forward(request,response);
            */  

            //重定向到ListServlet.java
            response.sendRedirect("/day07/ListServlet");

        }else{
            request
            .getRequestDispatcher("/WEB-INF/fail.html")
            .forward(request,response);
        }
    }
}

ListServlet

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ListServlet extends HttpServlet {
    public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        String username = (String) request.getAttribute("USERNAME");
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().write("欢迎"+username+"光临");
    }
    public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        String username = (String) request.getAttribute("USERNAME");
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().write("欢迎"+username+"光临");     
    }
}

html

view.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>用户登录</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <form action="/day07/Demo8" method="post">
        <table border="1" align="center">
            <caption>用户登录</caption>
            <tr>
                <th>用户名</th>
                <td><input type="text" name="username"/></td>
            </tr>
            <tr>
                <td colspan="2" align="center">
                    <input type="submit" value="提交"/>
                        ????
                    <input type="reset" value="重填"/>
                </td>
            </tr>   
        </table>
    </form>
  </body>
</html>

sucess.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>用户登录</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  </head>
  <body>
    登录成功  
  </body>
</html>

fail.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>用户登录</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  </head>
  <body>
    登录失败  
  </body>
</html>

MVC设计模式-学习笔记

标签:MVC设计模式-学习笔记

原文地址:http://blog.51cto.com/357712148/2104909

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!