zoukankan      html  css  js  c++  java
  • JSP 动作

     动作是第三种类型的语法元素,它们被转换成Java 代码来执行操作,如访问一个Java对象或调用方法。

    一. useBean

      useBean将创建一个关联Java对象的脚本变量。这 是早期分离的表示层和业务逻辑的手段。随着其他技术 的发展,如自定义标签和表达语言,现在很少使用 useBean方式。

    例: 利用useBean给quantity赋值

    <jsp:useBean id="quantity" class="java.lang.String" >${quantity="1.25"}</jsp:useBean>
    <fmt:parseNumber var="formattedNumber"   type="number"  value="${quantity}"/>
    ${quantity}

    例:

    newFile.jsp页面

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"  %>
    <%@ page import="java.text.DateFormat" %>
    <%@ page import="java.util.*"  %>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    <p></p>
    <jsp:useBean id="today" class="java.util.Date"/>
    <%=today%>
    </body>
    </html>

    在Tomcat中,上述代码被替换成如下代码:

    <%-- 部分代码 %-->
       out.write("<title>Insert title here</title>
    ");
          out.write("</head>
    ");
          out.write("<body>
    ");
          out.write("<p></p>
    ");
          java.util.Date today = null;
          today = (java.util.Date) _jspx_page_context.getAttribute("today", javax.servlet.jsp.PageContext.PAGE_SCOPE);
          if (today == null){
            today = new java.util.Date();
            _jspx_page_context.setAttribute("today", today, javax.servlet.jsp.PageContext.PAGE_SCOPE);
          }
          out.write('
    ');
          out.write('
    ');
          out.print(today);
          out.write("
    ");
          out.write("</body>
    ");
          out.write("</html>");

    输出效果

    二. setProperty和getProperty

      setProperty动作可对一个Java对象设置属性,而 getProperty则会输出Java对象的一个属性。清单3.11中 的getSetPropertyTest.jsp页面展示如何设置和输出定义 在清单3.10中的Employee类实例的firstName属性。

    package main;
    
    public class Employee {
        private String id;
        private String firstName;
        private String lastName;
    
        public String getId() {
            return this.id;
        }
    
        public void setId(String id) {
            this.id = id;
        }
    
        public String getFirstName() {
            return this.firstName;
        }
    
        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }
        
    
        public String getLastName() {
            return this.lastName;
        }
    
        public void setLastName(String lastName) {
            this.lastName = lastName;
        }
    
    }
    <%@ page  language="java"  contentType="text/html charset=utf-8"  pageEncoding="utf-8" %>
    <html>
    <head><title>getProperty</title></head>
    <body>
    <%-- 设置id employee指向的类为main.Employee %--> <jsp:useBean id="employee" class="main.Employee"/>
    <%-- 通过java bean 设置调用setFirstName()函数 %--> <jsp:setProperty name="employee" property="firstName" value="Abigail" />
    <%--通过java bean 设置调用getFirstName()函数 %-->
    First Name: <jsp:getProperty name="employee" property="firstName" />
    </body>
    </html>

    四: include

      include动作用来动态地引入另一个资源。可以引入 另一个JSP页面,也可以引入一个Servlet或一个静态的 HTML页面 ,                 html代码会叠加............

    这里,理解include指令和include动作非常重要。对 于include指令,资源引入发生在页面转换时,即当JSP 容器将页面转换为生成的Servlet时。而对于include动 作,资源引入发生在请求页面时。因此,使用include动 作是可以传递参数的,而include指令不支持。 第二个不同是,include指令对引入的文件扩展名不 做特殊要求。但对于include动作,若引入的文件需以 JSP页面处理,则其文件扩展名必须是JSP。若使用.jspf 为扩展名,则该页面被当作静态文件

    <%@ page  language="java"  contentType="text/html charset=utf-8"  pageEncoding="utf-8" %>
    <html>
    <head><title>getProperty</title></head>
    <body>
    hello;
    <%-- 首先引入welcome.jsp页面 --%><%-- 然后传递参数text和text值 --%>
    <jsp:include page="welcome.jsp">
    <jsp:param value="How are you !"  name="text"/>
    </jsp:include>
    </body>
    </html>
    <%@page language="java"  contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8" %>
    <%@ page import="java.util.Enumeration" %>
    <html>
    <head>
    <body>
    <div>
    <%-- 获取text的值 --%>
    <%
    
        String text= request.getParameter("text");
    %>
    
    <%
        out.print(text);
    %>
    </div>
    </body>
    </head>
    </html>

    五.forward

       forward将当前页面转向到其他资源。下面代码将 从当前页转向到welcome.jsp页面:(只会输出welcome一个页面)

    <%@ page  language="java"  contentType="text/html charset=utf-8"  pageEncoding="utf-8" %>
    <html>
    <head><title>getProperty</title></head>
    <body>
    hello;
    <%-- 首先引入welcome.jsp页面 --%><%-- 然后传递参数text和text值 --%>
    <jsp:forward page="welcome.jsp">
    <jsp:param value="How are you !"  name="text"/>
    </jsp:forward>
    </body>
    </html>
    <%@page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@ page import="java.util.Enumeration"%>
    <html>
    <head>
    <body>
        <div>
            <%-- 获取text的值 --%>
            <%
                String text = request.getParameter("text");
            %>
    
            <%
                out.print(text);
            %>
        </div>
    </body>
    </head>
    </html>

    输出

  • 相关阅读:
    jmeter(八)断言
    jmeter(七)定时器
    jmeter(六)元件的作用域与执行顺序
    JS 正则详解
    表单验证
    ubuntu16.04安装Grafana
    Crontab详细用法-定时任务详解
    ubuntu16.04 安装influxdb,简单使用
    jQuery CSS操作 点赞样式
    jQuery文档处理
  • 原文地址:https://www.cnblogs.com/jiangfeilong/p/10679835.html
Copyright © 2011-2022 走看看