jsp给bean property赋值 bean标签的使用

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>Jsp:userbean标签的使用,Jsp:setProperty标签的使用</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
<%--usebean标签的标签体只在usebean标签实例化bean时才执行--%>
<%-- <jsp:useBean id="person" class="com.lan.domain.Person" scope="page"></jsp:useBean> --%> 
<%-- <jsp:useBean id="person" class="com.lan.domain.Person" scope="session"></jsp:useBean>--%>
   <jsp:useBean id="person" class="com.lan.domain.Person" ></jsp:useBean>
    <%=person.getName() %><br/>
   
   <%--手工为bean属性赋值 --%>
   <jsp:setProperty name="person" property="name" value="xxxx"/>
   <%=person.getName() %><br/>
   
   <%--用请求参数给bean的属性赋值 http://localhost:8080/JavaBean/MyJsp.jsp?name=lll--%>
   <jsp:setProperty name="person" property="name" param="name"/>
   <%=person.getName() %><br/>
   
   <%--用请求参数给bean的属性赋值 http://localhost:8080/JavaBean/MyJsp.jsp?name=lll&age=12--%>
    <jsp:setProperty name="person" property="age" param="age"/> <%--只支持8种基本数据类型的转换 --%>
   <%=person.getAge() %><br/>
   
   <%-- http://localhost:8080/JavaBean/MyJsp.jsp?name=lll&age=12&birthday=1900-09-09--%>
   <jsp:setProperty name="person" property="birthday" value="<%=new Date() %>"/> 
  <%=person.getBirthday() %><br/>
   
   ------------------------------------------------<br/>
   <%--用所有的请求参数为bean赋值 http://localhost:8080/JavaBean/MyJsp.jsp?name=lll&age=12 --%>
   <jsp:setProperty name="person" property="*"/>
    <%=person.getName() %><br/>
    <%=person.getAge() %><br/>
   
  </body>
</html>



package com.lan.domain;

import java.util.Date;

public class Person {
	private String name = "aaa";
	private int age;
	private Date birthday;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	
}


你可能感兴趣的:(jsp给bean property赋值 bean标签的使用)