类声明:

package test;

public class Student {
    private int age;

public int getAge() {
        return age;
    }

public void setAge(int age) {
        this.age = age;
    }
}
jsp代码:

<jsp:useBean id="student" scope="session" class="test.Student"></jsp:useBean>
<jsp:setProperty property="age" name="student"  value="12"/>
 <jsp:setProperty property="age" name="student"  param="12"/>  此处12只是一个参数名字
 <jsp:getProperty property="age" name="student"/>

=》12

源码如下:

test.Student student = null;
      synchronized (session) {
        student = (test.Student) _jspx_page_context.getAttribute("student", PageContext.SESSION_SCOPE);
        if (student == null){
          student = new test.Student();
          _jspx_page_context.setAttribute("student", student, PageContext.SESSION_SCOPE);
        }
      }
      out.write("\r\n");
      out.write("  \t");
      org.apache.jasper.runtime.JspRuntimeLibrary.introspecthelper(_jspx_page_context.findAttribute("student"), "age", "12", null, null, false);
      out.write("\r\n");
      out.write("  \t");
      org.apache.jasper.runtime.JspRuntimeLibrary.introspecthelper(_jspx_page_context.findAttribute("student"), "age", request.getParameter("12"), request, "12", false);
      out.write("\r\n");
      out.write("  \t");
      out.write(org.apache.jasper.runtime.JspRuntimeLibrary.toString((((test.Student)_jspx_page_context.findAttribute("student")).getAge())));

解释:

1.

<jsp:useBean id="student" scope="request" class="test.Student"></jsp:useBean>

<jsp:setProperty property="age" name="student"  value="12"/>必须和<jsp:useBean配套使用

2.在useBean中声明要放在哪个useBean范围内:page,request,session,application,然后指定id也就是属性名和class也就是属性类型。

3.利用setProperty往useBean声明的变量中放置值,property为属性的变量名,name为useBean中声明的id名这两者必须相同,因为是将值设置到id指定的属性中去,value直接设置属性值,param则是接受传递过来的参数值设置到属性中,如request.getParameter。

param例子:

test2.jsp:

<jsp:forward page="test3.jsp">
<jsp:param value="10" name="age"/>
</jsp:forward>

test3.jsp:

<jsp:useBean id="student" scope="request" class="test.Student"></jsp:useBean>
<jsp:setProperty property="age" name="student" param="age"/>
<jsp:getProperty property="age" name="student"/>

结果为:10

4.通过getProperty从id中取直,property指定属性名,那么指定从哪个id中取值。

注意:修改scope范围生成的servlet中的源代码只有以下部分发生改变

synchronized (session) {
        student = (test.Student) _jspx_page_context.getAttribute("student", PageContext.SESSION_SCOPE);
        if (student == null){
          student = new test.Student();
          _jspx_page_context.setAttribute("student", student, PageContext.SESSION_SCOPE);
        }
      }

此代码先判断在session中是否存在student对象不存在创建一个放入session中,如果存在则不创建。之后设置值时就是往该对象中放置。

转载于:https://www.cnblogs.com/pangblog/p/3292229.html

jsp:setProperty相关推荐

  1. jsp的jsp:setProperty动作元素的value和param属性的区别。

    转自:http://www.runoob.com/jsp/jsp-actions.html jsp:setProperty动作有下面四个属性,如下表: 属性 描述 name name属性是必需的.它表 ...

  2. jsp:setProperty和jsp:getProperty动作到底怎么用举例?

    2.jsp:setProperty和jsp:getProperty动作: jsp:useBean动作获得Bean实例之后,要设置Bean的属性可以通过jsp:setProperty动作进行.读取Bea ...

  3. jsp:setproperty详解

    转自:http://www.runoob.com/jsp/jsp-actions.html jsp:setProperty动作有下面四个属性,如下表: 属性 描述 name name属性是必需的.它表 ...

  4. jsp:setProperty中赋值机制的理解

    在jsp中页面之间参数和值的传递: 1.值和参数的传递必须保证参数名的一致 2.*<jsp:setProperty name="Name" property="*& ...

  5. jsp标签jsp:setProperty用法

    <jsp:setProperty>用来设置已经实例化的Bean对象的属性 第一种形式: <jps:setProperty name = "JavaBean实例名" ...

  6. JSP userBean setProperty getProperty指令使用

    JSP userBean setProperty getProperty指令使用 javaBean的属性取决于get/set方法,而不是真实的属性名称. jsp文件: <%@ page lang ...

  7. 2020了,还要学JSP嘛?入个门呗!毕竟Servlet亲兄弟~

    目录 1.什么是JSP 2.JSP原理 3.JSP基础语法 jsp表达式 jsp脚本片段 jsp声明 jsp注释 4.JSP指令 5.JSP行为 6.9大内置对象 7.EL表达式 格式 变量 [ ]与 ...

  8. JSP基本语法:文件结构、脚本元素、指令元素、动作元素

    JSP语法分为三种不同的类型: 脚本元素(SCRIPTING) 指令元素(DIRECTIVE) 例如: <%@ page import="java.io.*" %> 动 ...

  9. JSP复习题【侵权联系我删除】

    [题型]:单选题 1.JSP页面经过编译之后,将创建一个(      )文件. A.applet B.  servlet C.application D.  server 答案: B [题型]:单选题 ...

最新文章

  1. Delphi 复习代码
  2. 【C语言】x++与++x
  3. 利用Excel VBA实现批量数据分组转置
  4. 显卡直通_英伟达RTX 30系显卡发布:性价比打哭2080Ti
  5. 日语学习-多邻国-平假名2
  6. 许多新兴的互联网O2O企业,做的都是一些“无中生有”的事情
  7. python自学免费教程-python免费入门教程/求完整的Python入门教程
  8. C++11新特性 右值引用与移动语义
  9. 知识图谱最新研究综述
  10. 我购买了一台acer笔记本
  11. javascript 字符串分割的方法总结区分
  12. 【PostgreSQL-15的 \watch命令】
  13. 【53期分享】4款毕业答辩PPT模板免费下载
  14. 常见浏览器对URL的长度限制
  15. 求勾股数元祖(java)
  16. 计算机专业毕业设计选题推荐(建议收藏)
  17. 去除Ninja的提醒
  18. 汽车网络安全之——CAN网关测试
  19. 读《Finger Vein Verification using Intrinsic and Extrinsic Features》
  20. 百度网盘怎么分享文件(或加密或不加密)

热门文章

  1. 雷林鹏分享:codeigniter框架文件上传处理
  2. atob和btoa的趣谈
  3. Showdoc 搭建项目 API 文档系统
  4. (八)企业部分之nginx+tomcat+memcached负载均衡集群搭建
  5. [JS性能优化专篇]
  6. 如何制定好的方案之四:执行力是决定因素
  7. 业务系统里面常见的方法接口设计
  8. Class.forName和ClassLoader
  9. 日历视图的XML属性
  10. Spring AOP 代理模式