美文网首页
JAVA Web学习(10)___第7章 JavaBean 技术

JAVA Web学习(10)___第7章 JavaBean 技术

作者: 岁月静好浅笑安然 | 来源:发表于2019-08-17 15:56 被阅读0次

第7章 JavaBean 技术

7.1 JavaBean 介绍

7.1.1 JavaBean概述

JavaBean 是特殊的 Java 类,使用 Java 语言书写,并且遵守 JavaBean API 规范。
接下来给出的是 JavaBean 与其它 Java 类相比而言独一无二的特征:
提供一个默认的无参构造函数。
需要被序列化并且实现了 Serializable 接口。可能有一系列可读写属性。可能有一系列的 getter 或 setter 方法。

7.1.2 JavaBean种类

7.2 JavaBean应用

7.2.1 获取JavaBean属性信息

7.2.2 对JavaBean属性赋值

7.2.3 如何在JSP页面应用JavaBean

示例代码
StudentsBean.java

public class StudentsBean implements Serializable{
     private String firstName = null;
       private String lastName = null;
       private int age = 0;
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    ... //省略部分setXXX()和getXXX()方法


}


java_bean.JSP

<%@ page language="java" import="java.util.*" 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>get 和 set 属性实例</title>

</head>

<body>

    <jsp:useBean id="students" class="com.hwp.bean.StudentsBean">
    <!-- 设置 -->
        <jsp:setProperty property="firstName" name="students" value="明霞" />
        <jsp:setProperty property="lastName" name="students" value="李" />
        <jsp:setProperty property="age" name="students" value="30" />


    </jsp:useBean>
    <!-- 获取值 -->
    <p>
        学生名字:
        <jsp:getProperty property="firstName" name="students" />
    </p>
    <p>
        学生姓氏:
        <jsp:getProperty property="lastName" name="students" />
    </p>
    <p>
        年纪:
        <jsp:getProperty property="age" name="students" />
    </p>
</body>
</html>

7.3 在JSP中应用JavaBean

7.3.1 解决中文乱码的JavaBean

新增一个类,使用代码转换

public class CharactorEncoding{
    public CharactorEncoding(){}
public String toString(String str){
    String text="";
    if(str!=null&&!str.equals("")){
    try{
     text=new String(str.getBytes("ISO-8859-1"),"utf-8");
    }catch(Exception e){
    }
    }
    return text;
}   
}

在jsp页面中调用,关键代码

<body>
<jsp:useBean id="students" class="com.hwp.bean.StudentsBean"></jsp:useBean>
    <jsp:useBean id="encoding" class="com.hwp.bean.CharactorEncoding"></jsp:useBean>
    <div align="center">
    <div>
        <%=encoding.toString(students.getFirstName())%>
    </div>
    </div>  
</body>

7.3.2 在jsp页面中用来显示时间的 JavaBean

  • DateBean.java
package com.hwp.bean;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class DateBean {
    private String dateTime;
    private String week;
    Calendar calendar=Calendar.getInstance();
    public String getDateTime() {
        Date time = Calendar.getInstance().getTime();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateTime=simpleDateFormat.format(time);
        return dateTime;
    }
    public void setDateTime(String dateTime) {
        this.dateTime = dateTime;
    }
    public String getWeek() {
        String weeks []={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
        int index=calendar.get(Calendar.DAY_OF_WEEK);
        week=weeks[index-1];
        return week;
    }
    public void setWeek(String week) {
        this.week = week;
    }
    
    
}

  • get_date.jsp
<%@ page language="java" import="java.util.*" 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>My JSP 'get_date.jsp' starting page</title>
    <style type="text/css">
    #show{
    width: 200px;
    height: 80px;
    background: #E0E0E0;
    font-size: 20px;
    font-weight: bold;
    border: solid 5px orange;
    padding: 20px; 
    }
    #week{
    padding-top: 10px;
    color: #0080FF;
    }
    </style>
    <meta http-equiv="Refresh" content="1">

  </head>
  
  <body >
   <jsp:useBean class="com.hwp.bean.DateBean"  id="date" scope="application"/>
   <div id="show" align="center">
   <div id="date">
   <jsp:getProperty property="dateTime" name="date"/>
   </div>
   <div  id="week">
     <jsp:getProperty property="week" name="date"/>
   </div>
   </div>
   <h6></h6>
  </body>
</html>

因为 DateBean并不涉及更多的业务逻辑,所有实例作用域设置为 application,即scope="application",只实例化一次 DateBean,不需要每次都实例化

为了时钟走动效果,在<head>标签内加入代码 <meta http-equiv="Refresh" content="1">,这样每隔一秒就刷新一次

7.3.3 数组转换字符串

示例代码

  • Paper.java
package com.hwp.bean;

import java.io.Serializable;

public class Paper implements Serializable{
    private String [] languages;
    private String [] technics;
    private String [] parts;
    public String[] getLanguages() {
        return languages;
    }
    public void setLanguages(String[] languages) {
        this.languages = languages;
    }
    public String[] getTechnics() {
        return technics;
    }
    public void setTechnics(String[] technics) {
        this.technics = technics;
    }
    public String[] getParts() {
        return parts;
    }
    public void setParts(String[] parts) {
        this.parts = parts;
    }
    
    

}
  • arr2str.jsp
<%@ page language="java" import="java.util.*" 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>
    


  </head>
  
  <body>
   <form action="arr2str_result.jsp" method="post">
   <div>
   <h6>调查问卷表单</h6>
   <ul>
   <li>你经常使用的开发语言?</li>
   <li>
   <input type="checkbox" name="languages" value="JAVA">JAVA
   <input type="checkbox" name="languages" value="C++">C++
   <input type="checkbox" name="languages" value="PHP">PHP
   </li>
   </ul>
   <ul>
   <li>你目前掌握的技术?</li>
   <li>
   <input type="checkbox" name="technics" value="JAVA">JAVA
   <input type="checkbox" name="technics" value="C++">C++
   <input type="checkbox" name="technics" value="PHP">PHP
   </li>
   </ul>
   <ul>
   <li>你认为最难的?</li>
   <li>
   <input type="checkbox" name="parts" value="ANDROID">ANDROID
   <input type="checkbox" name="parts" value="KOTLIN">KOTLIN
   <input type="checkbox" name="parts" value="JAVA">JAVA
   </li>
   </ul>
   <input value="提交" type="submit" />
   </div>
   </form>
  </body>
</html>

  • arr2str_result.jsp
<%@ page language="java" import="java.util.*" 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>My JSP 'arr2str_result.jsp' starting page</title>
    
    
  </head>
  
  <body>
   <jsp:useBean class="com.hwp.bean.Paper" id="paper"></jsp:useBean>
   <jsp:useBean class="com.hwp.util.Convert" id="conver"></jsp:useBean>
   <jsp:setProperty property="*" name="paper"/>
   <div>
   <h6>调查结果</h6>
   <ul>
   <li>
  你经常使用的开发语言 <%=conver.arr2str(paper.getLanguages()) %>
   </li>
   <li>
  你目前掌握的技术 <%=conver.arr2str(paper.getTechnics()) %>
   </li>
   <li>
  你认为最难的<%=conver.arr2str(paper.getParts()) %>
   </li>
   </ul>
   </div>
  </body>
</html>

微信截图_20190817155459.png 微信截图_20190817155515.png

相关文章

网友评论

      本文标题:JAVA Web学习(10)___第7章 JavaBean 技术

      本文链接:https://www.haomeiwen.com/subject/iyhksctx.html