• JSP_5_JavaBean


    Eclipse:快捷提示 alt +/

    ==============================

    JavaBean附和某种特定规范的类

    1 公有

    2 公有的无参构造方法

    3 属性私有

    4 getter/setter进行封装

    ---5-3 JSP动作元素------------------------------------------------------------------------

    JSP动作元素(action elements),为请求处理阶段提供信息。动作元素遵循XML元素的语法,有一个包含元素名的开始标签,可以有属性,可选的内容,与开始标签匹配的结束标签。

    1 与存储JavaBean有关的,包括:

    <jsp:useBean><jsp:setProperty> <jsp:getProperty> 

    2 是JSP1.2就开始有的基本元素,包括6个动作元素

    <jsp:include> <jsp:forward> <jsp:param> <jsp:plugin> <jsp:params> <jsp:fallback>

    3 是JSP2.0新增的元素,主要与JSP Document有关

    <jsp:root> <jsp:declaration> <jsp:scriptlet> <jsp:expression> <jsp:text> <jsp:output>

    4 是JSP2.0新增的元素,主要是用来动态生成XML元素标签的值,包括3个动作

    <jsp:attribute> <jsp:body> <jsp:element>

    5 是JSP2.0新增的元素,主要是用在Tag File中,有两个元素

    <jsp:invoke> <jsp:dobody>

    ---5-4 普通方式创建JavaBean------------------------------------------------------------------------

    在JSP页面中如何使用Javabeans

    1 像普通Java类一样,创建Javabeans实例(instance)

    javabean:

    package com.po;
    /*
     * 用户类
     * */
    public class Users {
    
        private String username;//用户名
        private String password;//密码
        
        //保留此默认的构造方法
        public Users()
        {
            
        }
    
        public String getUsername() {
            return username;
        }
    
        public void setUsername(String username) {
            this.username = username;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
        
        
    }

     使用javabean:

     1 导入javabean<%@ page import="com.po.Users" %>

     2 使用普通方法创建javabean 

     1 <%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
     2 <%@ page import="com.po.Users" %>
     3 <%
     4 String path = request.getContextPath();
     5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
     6 %>
     7 
     8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
     9 <html>
    10   <head>
    11     <base href="<%=basePath%>">
    12     
    13     <title>My JSP 'index.jsp' starting page</title>
    14     <meta http-equiv="pragma" content="no-cache">
    15     <meta http-equiv="cache-control" content="no-cache">
    16     <meta http-equiv="expires" content="0">    
    17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    18     <meta http-equiv="description" content="This is my page">
    19     <!--
    20     <link rel="stylesheet" type="text/css" href="styles.css">
    21     -->
    22   </head>
    23   
    24   <body>
    25     <% 
    26        Users user = new Users();
    27        user.setUsername("admin"); //设置用户名
    28        user.setPassword("123456");//设置密码
    29     %>
    30     <h1>使用普通方式创建javabean的实例</h1>
    31     <hr>
    32        用户名:<%=user.getUsername() %><br>
    33        密码:<%=user.getPassword() %><br>
    34   </body>
    35 </html>

    ---5-5 useBean动作元素------------------------------------------------------------------------

    2 在JSP页面中通常使用jsp动作标签使用javabean

    与存储JavaBean有关的,包括:

    <jsp:useBean><jsp:setProperty> <jsp:getProperty> 

    1) <jsp:useBean>

      作用:在jsp页面中实例化或者在指定范围内使用javabean:

      <jsp:useBean id="标示符" class="java类名(包名+类名,即全限定名)" scope="作用范围" />

    useBean.jsp:

    Eclipse:快捷提示 alt +/

     1 <%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
     2 <%
     3 String path = request.getContextPath();
     4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
     5 %>
     6 
     7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
     8 <html>
     9   <head>
    10     <base href="<%=basePath%>">
    11     
    12     <title>My JSP 'index.jsp' starting page</title>
    13     <meta http-equiv="pragma" content="no-cache">
    14     <meta http-equiv="cache-control" content="no-cache">
    15     <meta http-equiv="expires" content="0">    
    16     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    17     <meta http-equiv="description" content="This is my page">
    18     <!--
    19     <link rel="stylesheet" type="text/css" href="styles.css">
    20     -->
    21   </head>
    22   
    23   <body>
    24     <jsp:useBean id="myUsers" class="com.po.Users" scope="page"/>
    25     <h1>使用useBean动作创建javabean的实例</h1>
    26     <hr>
    27        用户名:<%=myUsers.getUsername() %><br>
    28        密码:<%=myUsers.getPassword() %><br>
    29   </body>
    30 </html>

    ---5-6 setProperty------------------------------------------------------------------------

    jsp:setProperty用来设置已经实例化的Bean对象的属性,一共四中形式。

    1.跟表单关联 <jsp:setProperty name="JavaBean实例名" property="*" />(跟表单关联)

    2.跟表单关联 <jsp:setProperty name="JavaBean实例名" property="javaBean属性名" />(跟表单关联)

    3.手工设置    <jsp:setProperty name="JavaBean实例名" property="javaBean属性名"  value ="BeanValue" />(手工设置)

    4.request参数关联 <jsp:setProperty name="JavaBean实例名" property="propertyName"  param="request对象中的参数名" />(跟request参数关联)

    表单:

    <%@ page language="java" import="java.util.*" contentType="text/html; charset=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>My JSP 'login.jsp' starting page</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">
        <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        -->
    
      </head>
      
      <body>
        <h1>系统登录</h1>
        <hr>
        <form name="loginForm" action="dologin.jsp?mypass=999999" method="post">
          <table>
            <tr>
              <td>用户名:</td>
              <td><input type="text" name="username" value=""/></td>
            </tr>
            <tr>
              <td>密码:</td>
              <td><input type="password" name="password" value=""/></td>
            </tr>
            <tr>
              <td colspan="2" align="center"><input type="submit" value="登录"/></td>
              
            </tr>
          </table>
        </form>
      </body>
    </html>

     setProperty四种使用方式:

    1.跟表单关联 <jsp:setProperty name="JavaBean实例名" property="*" />

      property="*"自动匹配名字一致的属性,调用setter方法设置

    2.跟表单关联 <jsp:setProperty name="JavaBean实例名" property="javaBean属性名" />

      部分匹配,匹配指定的属性

    3.手工设置    <jsp:setProperty name="JavaBean实例名" property="javaBean属性名"  value ="BeanValue" />(手工设置)

    4.request参数关联 <jsp:setProperty name="JavaBean实例名" property="propertyName"  param="request对象中的参数名" />(跟request参数关联)

      url地址栏传参的方式

     1 <%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
     2 <%
     3 String path = request.getContextPath();
     4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
     5 %>
     6 
     7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
     8 <html>
     9   <head>
    10     <base href="<%=basePath%>">
    11     
    12     <title>My JSP 'dologin.jsp' starting page</title>
    13     
    14     <meta http-equiv="pragma" content="no-cache">
    15     <meta http-equiv="cache-control" content="no-cache">
    16     <meta http-equiv="expires" content="0">    
    17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    18     <meta http-equiv="description" content="This is my page">
    19     <!--
    20     <link rel="stylesheet" type="text/css" href="styles.css">
    21     -->
    22 
    23   </head>
    24   
    25   <body>
    26     <jsp:useBean id="myUsers" class="com.po.Users" scope="page"/>
    27     <h1>setProperty动作元素</h1>
    28     <hr>
    29    <!--根据表单自动匹配所有的属性 -->
    30    <%-- 
    31    <jsp:setProperty name="myUsers" property="*"/>  
    32    --%>
    33    <!--根据表单匹配所有部分的属性 -->
    34    <%-- 
    35    <jsp:setProperty name="myUsers" property="username"/>  
    36    --%>
    37    <!--根表单无关,通过手工赋值给属性 -->
    38    <%-- 
    39    <jsp:setProperty name="myUsers" property="username" value="lisi"/>
    40    <jsp:setProperty name="myUsers" property="password" value="888888"/>
    41    --%>
    42    <!--通过URL传参数给属性赋值 -->
    43    <jsp:setProperty name="myUsers" property="username"/>
    44    <jsp:setProperty name="myUsers" property="password" param="mypass"/>
    45    <!-- 使用传统的表达式方式来获取用户名和密码 -->
    46    <%--     
    47        用户名:<%=myUsers.getUsername() %><br>
    48        密码:<%=myUsers.getPassword() %><br> 
    49    --%>
    50    <!-- 使用getProperty方式来获取用户名和密码 -->
    51       用户名:<jsp:getProperty name="myUsers" property="username"/> <br>
    52       密码:<jsp:getProperty name="myUsers" property="password"/><br>
    53    <br>
    54    <br>
    55       <a href="testScope.jsp">测试javabean的四个作用域范围</a>
    56       <% 
    57          request.getRequestDispatcher("testScope.jsp").forward(request, response);
    58       %>
    59   </body>
    60 </html>

    ---5-7 getProperty------------------------------------------------------------------------

    获取指定javabean对象的属性值

    <jsp:getProperty name="JavaBean实例名" property="属性名" />

    代码参见5-6

    ---5-8 JavaBean四个作用域范围------------------------------------------------------------------------

    使用useBean的scope属性可以指定javabean的作用范围

    ·page        //仅当前页面有效

    ·request     //可以通过HttpRequest.getAttribute()方法取得javaBean对象

    ·session     //可以通过HttpSession.getAttribute()方法取得javaBean对象

    ·application//可以通过application.getAttribute()方法取得javaBean对象

    dologin.jsp:

    从此页设置scope,迁移到testScope.jsp

    1) <a href="testScope.jsp">测试javabean的四个作用域范围</a> 相当于请求重定向,request无法传递,改为使用下面"2)"的转发。
    2) <% request.getRequestDispatcher("testScope.jsp").forward(request, response); %>

    相当于同一个request,可以使用request的参数

    <%@ page language="java" import="java.util.*" contentType="text/html; charset=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>My JSP 'dologin.jsp' starting page</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">
        <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        -->
    
      </head>
      
      <body>
        <jsp:useBean id="myUsers" class="com.po.Users" scope="page"/>
        <h1>setProperty动作元素</h1>
        <hr>
       <!--根据表单自动匹配所有的属性 -->
       <%-- 
       <jsp:setProperty name="myUsers" property="*"/>  
       --%>
       <!--根据表单匹配所有部分的属性 -->
       <%-- 
       <jsp:setProperty name="myUsers" property="username"/>  
       --%>
       <!--根表单无关,通过手工赋值给属性 -->
       <%-- 
       <jsp:setProperty name="myUsers" property="username" value="lisi"/>
       <jsp:setProperty name="myUsers" property="password" value="888888"/>
       --%>
       <!--通过URL传参数给属性赋值 -->
       <jsp:setProperty name="myUsers" property="username"/>
       <jsp:setProperty name="myUsers" property="password" param="mypass"/>
       <!-- 使用传统的表达式方式来获取用户名和密码 -->
       <%--     
           用户名:<%=myUsers.getUsername() %><br>
           密码:<%=myUsers.getPassword() %><br> 
       --%>
       <!-- 使用getProperty方式来获取用户名和密码 -->
          用户名:<jsp:getProperty name="myUsers" property="username"/> <br>
          密码:<jsp:getProperty name="myUsers" property="password"/><br>
       <br>
       <br>
          <a href="testScope.jsp">测试javabean的四个作用域范围</a>
          <% 
             request.getRequestDispatcher("testScope.jsp").forward(request, response);
          %>
      </body>
    </html>

    testScope.jsp:

    2 <%@ page import="com.po.Users" %>导入JavaBean,在下面转换类型时使用:

    如:(Users)application.getAttribute("myUsers")).getUsername()

     1 <%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
     2 <%@ page import="com.po.Users" %>
     3 <%
     4 String path = request.getContextPath();
     5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
     6 %>
     7 
     8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
     9 <html>
    10   <head>
    11     <base href="<%=basePath%>">
    12     
    13     <title>My JSP 'testScope.jsp' starting page</title>
    14     
    15     <meta http-equiv="pragma" content="no-cache">
    16     <meta http-equiv="cache-control" content="no-cache">
    17     <meta http-equiv="expires" content="0">    
    18     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    19     <meta http-equiv="description" content="This is my page">
    20     <!--
    21     <link rel="stylesheet" type="text/css" href="styles.css">
    22     -->
    23 
    24   </head>
    25   
    26   <body>
    27     <h1>Javabean的四个作用域范围</h1>
    28     <hr>
    29     <jsp:useBean id="myUsers" class="com.po.Users" scope="page"/>
    30        用户名:<jsp:getProperty name="myUsers" property="username"/><br>
    31        密码:<jsp:getProperty name="myUsers" property="password"/><br>
    32     <!-- 使用内置对象获取用户名和密码 -->
    33     <hr>
    34    <%-- 
    35        用户名:<%=((Users)application.getAttribute("myUsers")).getUsername()%><br>
    36        密码:<%=((Users)application.getAttribute("myUsers")).getPassword() %><br>
    37    --%>
    38    <%-- 
    39        用户名:<%=((Users)session.getAttribute("myUsers")).getUsername()%><br>
    40        密码:<%=((Users)session.getAttribute("myUsers")).getPassword() %><br>
    41    --%>
    42    <%-- 
    43        用户名:<%=((Users)request.getAttribute("myUsers")).getUsername()%><br>
    44        密码:<%=((Users)request.getAttribute("myUsers")).getPassword() %><br>
    45    --%>
    46    <% 
    47        String username = "";
    48        String password = "";
    49        if(pageContext.getAttribute("myUsers")!=null )
    50        {
    51           username = ((Users)pageContext.getAttribute("myUsers")).getUsername();
    52           password = ((Users)pageContext.getAttribute("myUsers")).getPassword();
    53        }
    54        
    55    %>
    56     
    57        用户名:<%=username%><br>
    58        密码:<%=password%><br>
    59    
    60    
    61    
    62   </body>
    63 </html>

    ---5-10 Model1------------------------------------------------------------------------

    Model1模型出现前,整个Web应用的情况几乎全部由JSP页面组成,JSP页面接受处理客户端请求,对请求处理后直接作出相应。

    弊端:在界面层充斥大量的业务逻辑代码和数据访问层的代码,Web程序可扩展性和可维护性非常差。

    Javabean的出现使jsp页面中使用Javabean封装的数据或者调用Javabean的业务逻辑代码,这样大大提升了程序的可维护性。

    Model1模型:

     

     JspLoginDemoByModel1参照

    javabean:

    package com.po;
    //モテサァタ・
    public class Users {
    
        private String username;
        private String password;
        
        public Users()
        {
            
        }
    
        public String getUsername() {
            return username;
        }
    
        public void setUsername(String username) {
            this.username = username;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
        
        
    }

    DAO:

    package com.dao;
    
    import com.po.Users;
    
    //用户的业务逻辑类
    public class UsersDAO {
        
        //用户登录方法
        public boolean usersLogin(Users u)
        {
            if("admin".equals(u.getUsername())&&"admin".equals(u.getPassword()))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }

    login:

    <%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
    <%
       String path = request.getContextPath();
       String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    <html>
        <head>
            <!-- Page title -->
            <title>imooc - Login</title>
            <!-- End of Page title -->
            <!-- Libraries -->
            <link type="text/css" href="css/login.css" rel="stylesheet" />    
            <link type="text/css" href="css/smoothness/jquery-ui-1.7.2.custom.html" rel="stylesheet" />    
            <script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
            <script type="text/javascript" src="js/easyTooltip.js"></script>
            <script type="text/javascript" src="js/jquery-ui-1.7.2.custom.min.js"></script>
            <!-- End of Libraries -->    
        </head>
        <body>
        <div id="container">
            <div class="logo">
                <a href="#"><img src="assets/logo.png" alt="" /></a>
            </div>
            <div id="box">
                <form action="dologin.jsp" method="post">
                <p class="main">
                    <label>用户名: </label>
                    <input name="username" value="" /> 
                    <label>密码: </label>
                    <input type="password" name="password" value="">    
                </p>
                <p class="space">
                    <input type="submit" value="登录" class="login" style="cursor: pointer;"/>
                </p>
                </form>
            </div>
        </div>
        </body>
    </html>
    dologin.jsp
    
    <%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
    
    <% 
      request.setCharacterEncoding("utf-8");
    %>
    <jsp:useBean id="loginUser" class="com.po.Users" scope="page"/>
    <jsp:useBean id="userDAO" class="com.dao.UsersDAO" scope="page"/>
    <jsp:setProperty property="*" name="loginUser"/>
    
    <%
      String path = request.getContextPath();
      String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
      
      //如果用户和密码都等于admin,则登录成功
      if(userDAO.usersLogin(loginUser))
      {
         session.setAttribute("loginUser", loginUser.getUsername());
         request.getRequestDispatcher("login_success.jsp").forward(request, response);
         
      }
      else
      {
         response.sendRedirect("login_failure.jsp");
      }
    %>

    dologin:

    ---3-3 ------------------------------------------------------------------------

    ---3-3 ------------------------------------------------------------------------

    ---3-3 ------------------------------------------------------------------------

  • 相关阅读:
    PAT 甲级 1126 Eulerian Path (25 分)
    PAT 甲级 1126 Eulerian Path (25 分)
    PAT 甲级 1125 Chain the Ropes (25 分)
    PAT 甲级 1125 Chain the Ropes (25 分)
    PAT 甲级 1124 Raffle for Weibo Followers (20 分)
    PAT 甲级 1124 Raffle for Weibo Followers (20 分)
    PAT 甲级 1131 Subway Map (30 分)
    PAT 甲级 1131 Subway Map (30 分)
    AcWing 906. 区间分组 区间贪心
    AcWing 907. 区间覆盖 区间贪心
  • 原文地址:https://www.cnblogs.com/charles999/p/6697703.html
Copyright © 2020-2023  润新知