JSP userBean setProperty getProperty指令使用

javaBean的属性取决于get/set方法,而不是真实的属性名称。

jsp文件:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%String path = request.getContextPath();String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";pageContext.setAttribute("pca", "pageContextAttribute");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>009index</title>
</head>
<body><jsp:useBean id="person" class="com.stono.servlet.bean.Person"><jsp:setProperty property="name" name="person" value="stono" /></jsp:useBean><jsp:getProperty property="name" name="person" />
</body>
</html>

转换的java文件:

/** Generated by the Jasper component of Apache Tomcat* Version: Apache Tomcat/7.0.35* Generated at: 2015-10-20 04:32:26 UTC* Note: The last modified time of this file was set to*       the last modified time of the source file after*       generation to assist with modification tracking.*/
package org.apache.jsp;import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import java.util.*;public final class index009_jsp extends org.apache.jasper.runtime.HttpJspBaseimplements org.apache.jasper.runtime.JspSourceDependent {private static final javax.servlet.jsp.JspFactory _jspxFactory =javax.servlet.jsp.JspFactory.getDefaultFactory();private static java.util.Map<java.lang.String,java.lang.Long> _jspx_dependants;private javax.el.ExpressionFactory _el_expressionfactory;private org.apache.tomcat.InstanceManager _jsp_instancemanager;public java.util.Map<java.lang.String,java.lang.Long> getDependants() {return _jspx_dependants;}public void _jspInit() {_el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();_jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig());}public void _jspDestroy() {}public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)throws java.io.IOException, javax.servlet.ServletException {final javax.servlet.jsp.PageContext pageContext;javax.servlet.http.HttpSession session = null;final javax.servlet.ServletContext application;final javax.servlet.ServletConfig config;javax.servlet.jsp.JspWriter out = null;final java.lang.Object page = this;javax.servlet.jsp.JspWriter _jspx_out = null;javax.servlet.jsp.PageContext _jspx_page_context = null;try {response.setContentType("text/html;charset=UTF-8");pageContext = _jspxFactory.getPageContext(this, request, response,null, true, 8192, true);_jspx_page_context = pageContext;application = pageContext.getServletContext();config = pageContext.getServletConfig();session = pageContext.getSession();out = pageContext.getOut();_jspx_out = out;out.write('\r');out.write('\n');String path = request.getContextPath();String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";pageContext.setAttribute("pca", "pageContextAttribute");out.write("\r\n");out.write("<!DOCTYPE html>\r\n");out.write("<html>\r\n");out.write("<head>\r\n");out.write("<meta charset=\"UTF-8\">\r\n");out.write("<title>009index</title>\r\n");out.write("</head>\r\n");out.write("<body>\r\n");out.write("\t");com.stono.servlet.bean.Person person = null;person = (com.stono.servlet.bean.Person) _jspx_page_context.getAttribute("person", javax.servlet.jsp.PageContext.PAGE_SCOPE);if (person == null){person = new com.stono.servlet.bean.Person();_jspx_page_context.setAttribute("person", person, javax.servlet.jsp.PageContext.PAGE_SCOPE);out.write("\r\n");out.write("\t\t");org.apache.jasper.runtime.JspRuntimeLibrary.introspecthelper(_jspx_page_context.findAttribute("person"), "name", "stono", null, null, false);out.write('\r');out.write('\n');out.write('    ');}out.write('\r');out.write('\n');out.write('    ');out.write(org.apache.jasper.runtime.JspRuntimeLibrary.toString((((com.stono.servlet.bean.Person)_jspx_page_context.findAttribute("person")).getName())));out.write("\r\n");out.write("\t\r\n");out.write("</body>\r\n");out.write("</html>\r\n");} catch (java.lang.Throwable t) {if (!(t instanceof javax.servlet.jsp.SkipPageException)){out = _jspx_out;if (out != null && out.getBufferSize() != 0)try { out.clearBuffer(); } catch (java.io.IOException e) {}if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);else throw new ServletException(t);}} finally {_jspxFactory.releasePageContext(_jspx_page_context);}}
}

JavaBean:

package com.stono.servlet.bean;
public class Person {// 对外的属性名称还是name,private String name2;public String getName() {return name2;}public void setName(String name) {this.name2 = name;}
}

jsp:userBean的使用:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%String path = request.getContextPath();String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";pageContext.setAttribute("pca", "pageContextAttribute");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>009index</title>
</head>
<body><p>jsp:useBean id="person" class="com.stono.servlet.bean.Person",如果Person是抽象类,会有异常:<pre>    严重: Servlet.service() for servlet [jsp] in context with path [/TestBootWeb] threw exception [/index013.jsp (line: 14, column: 1) The value for the useBean class attribute com.stono.servlet.bean.Person is invalid.] with root causeorg.apache.jasper.JasperException: /index013.jsp (line: 14, column: 1) The value for the useBean class attribute com.stono.servlet.bean.Person is invalid.</pre></p><p>对于抽象类需要使用jsp:useBean id="person" type="com.stono.servlet.bean.Person" class="com.stono.servlet.bean.Employee"进行使用。</p><jsp:useBean id="person" type="com.stono.servlet.bean.Person" class="com.stono.servlet.bean.Employee"><jsp:setProperty property="name" name="person" value="stono" /></jsp:useBean><jsp:getProperty property="name" name="person" /><p>直接输入jsp:useBean id="person2" type="com.stono.servlet.bean.Person"会报错:<pre>严重: Servlet.service() for servlet [jsp] in context with path [/TestBootWeb] threw exception [javax.servlet.ServletException: java.lang.InstantiationException: bean person2 not found within scope] with root causejava.lang.InstantiationException: bean person2 not found within scope        </pre>输入jsp:useBean id="person" type="com.stono.servlet.bean.Person"也是有问题的:<pre>严重: Servlet.service() for servlet [jsp] in context with path [/TestBootWeb] threw exception [/index013.jsp (line: 35, column: 1) useBean: Duplicate bean name: {0}] with root causeorg.apache.jasper.JasperException: /index013.jsp (line: 35, column: 1) useBean: Duplicate bean name: {0}        </pre></p><p>在servlet中进行request.setAttribute("person3",person3),但是在jsp中直接写:jsp:useBean id="person3" type="com.stono.servlet.bean.Person"会报错:<pre>严重: Servlet.service() for servlet jsp threw exceptionjava.lang.InstantiationException: bean person3 not found within scope        </pre>因为默认的scope是page;修改为scope="request",就可以读取到request传递过来的值;</p><p>如果希望使用jsp:useBean id="person3" type="com.stono.servlet.bean.Person",还不能在scriplet中使用名称为person3的变量,需要如下定义使用:<pre>&lt;%@ page import="com.stono.servlet.bean.*" %&gt;&lt;%Person person33 = new Employee();person33.setName("stono3inJsp");pageContext.setAttribute("person3", person33);%&gt&lt;jsp:useBean id="person3" type="com.stono.servlet.bean.Person"&gt&lt;/jsp:useBean&gt&lt;jsp:getProperty property="name" name="person3"/&gt</pre></p>
</body>
</html>

转载于:https://www.cnblogs.com/stono/p/4894445.html

JSP userBean setProperty getProperty指令使用相关推荐

  1. JAVAWEB开发之Session的追踪创建和销毁、JSP详解(指令,标签,内置对象,动作即转发和包含)、JavaBean及内省技术以及EL表达式获取内容的使用

    Session的追踪技术 已知Session是利用cookie机制的服务器端技术,当客户端第一次访问资源时 如果调用request.getSession() 就会在服务器端创建一个由浏览器独享的ses ...

  2. JAVAWEB开发之Session的追踪创建和销毁、JSP具体解释(指令,标签,内置对象,动作即转发和包括)、JavaBean及内省技术以及EL表达式获取内容的使用...

    Session的追踪技术 已知Session是利用cookie机制的server端技术.当client第一次訪问资源时 假设调用request.getSession() 就会在server端创建一个由 ...

  3. jsp动作之 getProperty

    getProperty就是用来获取(读取)实例化的内容的. 说明了就是(Techerobj实例为样,用name=张三,age=21等属性) <%=Techerobj.getName()%> ...

  4. JSP的3大指令Page,include,taglib

    一般格式是<%@ 指令名称 属性1="属性值" 属性2="属性值" -%> 指令名称有三种:page,include,taglib <%@ p ...

  5. JSP学习之include指令

    2019独角兽企业重金招聘Python工程师标准>>> 今天,看视频自学JSP的include指令,他主要有动态和静态两种包含方式,用于在一个JSP页面里包含其他jsp页面的内容. ...

  6. JSP中的include指令和include动作的区别

    我们都知道在jsp中include有两种形式,分别是Include指令:<%@ include file=""%>和include动作:<jsp:include ...

  7. JSP基础程序以及指令的使用

                                                                                    桂 林 理 工 大 学         ...

  8. JSP中的page指令常用的属性

    JSP中指令使用的格式: <%@指令名 属性名="属性值" 属性名="属性值"-%> <%@page pageEncoding="u ...

  9. JSP中的forward指令

    在页面跳转后地址栏不变. test.jsp文件: <html> <head><title>我的JSP</title></head> < ...

最新文章

  1. Oracle: SQL组合不同字段作为一个查询条件
  2. vue 组件以字符串插入_今日分享:vue3多层嵌套组件如何访问到最外层组件的数据?...
  3. BZOJ 4278 [ONTAK2015]Tasowanie (后缀数组)
  4. Unity 基本操作
  5. 在linux中 如何创建磁盘配额,如何在Linux系统中配置磁盘配额?
  6. eclipse远程连接hive
  7. Mac如何彻底卸载Mysql
  8. Lightroom Classic 教程,如何将照片从 Lightroom 移至Ps,在 Ps 中为照片应用滤镜?
  9. SQL:mysql将datetime类型数据格式化
  10. 3月21日阿里云北京峰会的注册二维码
  11. 帐号 快速看图cad_cad快速看图下载
  12. 32位java8,jdk8下载,解压版
  13. Mac 无法打开淘宝,天猫,京东等
  14. 社区服务器目录迁移实际开发运用
  15. Python实现图像直方图规定化(直方图匹配)-附完整代码
  16. 读入一幅图像,对图像分别进行高斯低通、巴特沃兹低通、高斯高通和巴特沃兹高通频域滤波,比较其锐化和平滑效果。
  17. poj 1689 zoj 1422 3002 Rubbery (Geometry + BFS)
  18. Linux || 查询类命令
  19. 依靠大数据 社会化协同
  20. 参加全国大学生智能汽车竞赛,快来申请沁恒RISC-V MCU!

热门文章

  1. x79主板bios设置中文_bios菜单
  2. 线性表、顺序表以及ArrayList、Iterable、Collection、List中重要的方法
  3. 电脑ip地址设置_路由器怎么设置静态IP 路由器设置静态IP方法【详解】
  4. log4j2.xml 文件
  5. 历史文件夹_Win10备份文件教程:备份到OneDrive,文件历史记录
  6. nfs服务器实现文件共享,NFS服务器及Samba服务配置实现文件共享(示例代码)
  7. 不用GD32?我把STM32换成了国产MM32
  8. 湖北省武汉科技大学计算机报名,湖北武汉科技大学第33次计算机等级考试报名通知...
  9. php导入关系表,PHP导入Execl表到数据库
  10. 苹果是c语言安卓是什么语言,主流app是用什么语言开发的苹果和安卓的主流开...