JSP userBean setProperty getProperty指令使用
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.HttpJspBase
implements 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 cause
org.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 cause
java.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 cause
org.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 exception
java.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>
<%@ page import="com.stono.servlet.bean.*" %>
<%
Person person33 = new Employee();
person33.setName("stono3inJsp");
pageContext.setAttribute("person3", person33);
%>
<jsp:useBean id="person3" type="com.stono.servlet.bean.Person"></jsp:useBean>
<jsp:getProperty property="name" name="person3"/>
</pre>
</p>
</body>
</html>
JSP userBean setProperty getProperty指令使用的更多相关文章
- JSP userBean setProperty直接从request中获取参数
JSP userBean setProperty直接从request中获取参数: form表单Jsp: <%@ page language="java" import=&qu ...
- JSP动作元素之useBean、setProperty、getProperty指令
简介 这三个指令都是与JavaBean相关的指令,其中useBean指令用于在JSP页面中初始化一个Java实例:setProperty指令用于为JavaBean实例的属性设置值:getPropert ...
- JSP之使用useBean、setProperty、getProperty指令
useBean指令用于在JSP页面中初始化一个Java实例,setProperty指令用于为JavaBean实例的属性设置值,getProperty指令用于输出JavaBean实例的属性. 例子: J ...
- JavaWeb(三)JSP之3个指令、6个动作、9个内置对象和4大作用域
前言 前面大概介绍了什么是JSP,今天我给大家介绍一下JSP的三个指令.6个动作以及它的9大内置对象.接下来我们就直接进入正题 一.JSP的3个指令 JSP指令(directive)是为JSP引擎而设 ...
- jsp笔记2(编译指令与动作指令)
一.jsp的编译指令是通知jsp引擎的消息,不会生成输出. jsp的3个编译指令: page:针对当前页面的指令 include:包含另一个页面的指令 taglib:用于定义和访问自定义标签 ...
- JAVAWEB开发之Session的追踪创建和销毁、JSP具体解释(指令,标签,内置对象,动作即转发和包括)、JavaBean及内省技术以及EL表达式获取内容的使用
Session的追踪技术 已知Session是利用cookie机制的server端技术.当client第一次訪问资源时 假设调用request.getSession() 就会在server端创建一个由 ...
- JSP基本语法--Page指令 <%@page 属性=”内容“%>
page指令语法:<%@page 属性=”内容“%> 常用:contentType,import,pageEncoding 例子,设置MIME属性,如果使用一些高版本的tomcat,可能自 ...
- JSP中的编译指令和动作指令的区别
JSP中的编译指令和动作指令的区别 1.编译指令是通知Servlet引擎的处理消息,而动作指令只是运行时的脚本动作 2.编译指令是在将JSP编译成Servlet时起作用,而动作指令可替换成JSP脚本, ...
- jsp 基础知识之指令元素
由于考研和结业的事情,这里荒废了许久,而如今重新捡起来,是因为带到公司的碳素笔没有油了...... jsp的指令元素:通常以<%@开始,以%>结尾. jsp主要包括三种指令元素:pa ...
随机推荐
- 行内元素为何不能设置margin-top、margin-bottom;padding-top、padding-bottom值
曾经学过的教程中写明:行内元素的特点有: 1.与其他元素在同一行 2.宽度(width).高度(height).内边距的top/bottom(padding-top/padding-bottom)和外 ...
- zepto学习之路--源代码提取
最近在看zepto的源代码,把一些有用的函数摘出来,看看zepto是怎么实现的,自己做的时候也可以用.说实话,zepto的实现有一些看起来还是很晦涩的,可能是自己的水平不够,看不透作者的真正的意图. ...
- HDU 5438 拓扑排序+DFS
Ponds Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Sub ...
- 如何在Android中添加系统服务
一,在frameworks/base/core/java/android/content/Context.java中添加 public static final String RADIO_SERVIC ...
- JOptionPane的使用
最近在做swing程序中遇到使用消息提示框的,JOptionPane类其中封装了很多的方法. 很方便的,于是就简单的整理了一下. 1.1 showMessageDialog 显示一个带有OK 按钮的模 ...
- mysql的数据类型int、bigint、smallint 和 tinyint取值范围
使用整数数据的精确数字数据类型. bigint 从 -2^63 (-9223372036854775808) 到 2^63-1 (9223372036854775807) 的整型数据(所有数字 ...
- nginx location配置(URL)
语法规则: location [=|~|~*|^~] /uri/ { … }= 表示精确匹配,这个优先级也是最高的^~ 表示uri以某个常规字符串开头,理解为匹配 url路径即可.nginx不对url ...
- [Unity]Unity开发NGUI代码实现ScrollView(放大视图)
Unity开发NGUI代码实现ScrollView(放大视图) 下载NGUI包 导入NGUI3.9.1版本package 创建MainCameraScript.cs脚本 MainCameraScrip ...
- CodeForces 626B Cards
瞎搞题...凭直觉+猜测写了一发,居然AC了.. #include<cstdio> #include<cstring> #include<cmath> #inclu ...
- SVN简单流程---以公司的使用方法为例
一 名词解释 svn一般包括 branch 每个branch版本都要生成对应的tag.例如 branch版本号为1.0.0,那么提交后生成的tag版本号为1.0.0.0;当branch修 ...