美文网首页
JavaBean在Jsp页面中显示时钟

JavaBean在Jsp页面中显示时钟

作者: Jianbaozi | 来源:发表于2020-11-13 15:22 被阅读0次

DateBean 只需提供get方法

package com.baozi;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateBean {
    private String datetime;
    private String week;
    private Calendar calendar=Calendar.getInstance();
    public String getdatetime(){
        Date currDate=Calendar.getInstance().getTime();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月 dd日 HH点mm分ss秒");
        datetime=sdf.format(currDate);
        return datetime;
    }
    public String getWeek(){
        String []weeks={"星期一","星期二","星期三","星期四","星期五","星期六","星期天"};
        int index=calendar.get(Calendar.DAY_OF_WEEK);
        week= weeks[index-1];
        return week;
    }

}

由于DateBean不涉及业务逻辑,所以作用域设置scope="application"

<%@ page language="java" contentType="text/html; charset=gb2312"
    pageEncoding="gb2312" isErrorPage="true"%>
<%request.setCharacterEncoding("gb2312"); %>
<%@page import="com.baozi.*" %>
<!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=gb2312">
<meta http-equiv="Refresh" content="1">
<title>时钟</title>
</head>
<body>
<jsp:useBean id="date" class="com.baozi.DateBean" scope="application"></jsp:useBean>
<jsp:getProperty property="datetime" name="date"/>
<br>
<jsp:useBean id="week" class="com.baozi.DateBean" scope="application"></jsp:useBean>
<jsp:getProperty property="week" name="date"/>
</body>
</html>
image.png

相关文章

网友评论

      本文标题:JavaBean在Jsp页面中显示时钟

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