标签:分享 范围 pre int insert targe port cte one
一、jsp javaBean组件引入
<jsp:useBean id="实例化对象名称" scope="保存范围" class="类完整名称" />
scope一共有page,request,session和application 4个属性范围,默认是page;
student类:
1 package com.java1234.model; 2 3 public class Student { 4 5 private String name; 6 private int age; 7 8 public String getName() { 9 return name; 10 } 11 public void setName(String name) { 12 this.name = name; 13 } 14 public int getAge() { 15 return age; 16 } 17 public void setAge(int age) { 18 this.age = age; 19 } 20 }
现在有一个提交表单:
1 <body> 2 <form action="javabean03.jsp" method="post"> 3 <table> 4 <tr> 5 <td>姓名:</td> 6 <td><input type="text" name="name"/></td> 7 </tr> 8 <tr> 9 <td>年龄:</td> 10 <td><input type="text" name="age"/></td> 11 </tr> 12 <tr> 13 <td colspan="2"><input type="submit" value="提交"/></td> 14 </tr> 15 </table> 16 </form> 17 </body>
以前我们使用javabean03.jsp这样来接收参数:
1 <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> 2 <%@ page import="com.java1234.model.Student" %> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <% 11 request.setCharacterEncoding("utf-8"); 12 String name=request.getParameter("name"); 13 String age=request.getParameter("age"); 14 Student student=new Student(); 15 student.setName(name); 16 student.setAge(Integer.parseInt(age)); 17 %> 18 <h1>姓名:<%=student.getName() %></h1> 19 <h1>年龄:<%=student.getAge() %></h1> 20 </body> 21 </html>
而现在我们使用javaBean:
1 <body> 2 <% 3 request.setCharacterEncoding("utf-8"); 4 %> 5 <jsp:useBean id="student" scope="page" class="com.java1234.model.Student"/> 6 <!-- 7 1.如果student.jsp中的提交参数名和student属性一致,可以这样接: 8 <jsp:setProperty property="*" name="student"/> 9 2.如果提交参数名不一致,使用param="userName"来指定参数名 10 3.value="100",不使用提交参数的age,自己来设置100这个值 11 --> 12 <jsp:setProperty property="name" name="student" param="userName"/> 13 <jsp:setProperty property="age" name="student" value="100"/> 14 <h1>姓名:<%=student.getName() %></h1> 15 <h1>年龄:<%=student.getAge() %></h1> 16 </body>
二、jsp:getProperty获取javabean属性值:
<jsp:getProperty property="属性名称" name="实例化对象的名称" />
如:先设置student的值,内部跳转到target01.jsp,然后再target01.jsp中获取值;
内部跳转同一个request,javabean的scope设置为request:
1 <body> 2 <jsp:useBean id="student" scope="request" class="com.java1234.model.Student"/> 3 <jsp:setProperty property="name" name="student" value="王八蛋"/> 4 <jsp:setProperty property="age" name="student" value="12"/> 5 <jsp:forward page="target01.jsp"/> 6 </body> 7 8 target01.jsp: 9 <body> 10 <jsp:useBean id="student" scope="request" class="com.java1234.model.Student"/> 11 <h1>姓名:<jsp:getProperty property="name" name="student"/></h1> 12 <h1>年龄:<jsp:getProperty property="age" name="student"/></h1> 13 </body>
三、javabean的保存范围:
举个session保存的范围的例子:
1 <body> 2 <jsp:useBean id="student" scope="session" class="com.java1234.model.Student"/> 3 <jsp:setProperty property="name" name="student" value="王八蛋"/> 4 <jsp:setProperty property="age" name="student" value="12"/> 5 <h1>Session数据设置完毕!</h1> 6 </body> 7 8 然后再jsp页面取值,javabean的scope是session: 9 <h1>Session中取值</h1> 10 <jsp:useBean id="student" scope="session" class="com.java1234.model.Student"/> 11 <h1>姓名:<jsp:getProperty property="name" name="student"/></h1> 12 <h1>年龄:<jsp:getProperty property="age" name="student"/></h1> 13 </body>
application的应用方法一样;
四、javaBean删除:
page范围: pageContext.removeAttribute("javaBean Name");
request范围: request.removeAttribute("javaBean Name");
session范围:session.removeAttribute("javaBean Name");
application范围:application.removeAttribute("javaBean Name");
标签:分享 范围 pre int insert targe port cte one
原文地址:http://www.cnblogs.com/tenWood/p/6493271.html