当前位置: 代码迷 >> Web前端 >> Web开发九:JavaBean
  详细解决方案

Web开发九:JavaBean

热度:532   发布时间:2012-11-01 11:11:33.0
Web开发9:JavaBean

?

?

具体实例:

写一个JAVABEAN:Person

package com.test.bean;

public class Person {
	private String name ="zhangsan";
	private int age =20;
	private String address = "beijing";
	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 String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
}

?

在jsp页面中使用JAVABEAN

<%@ page language="java" import="com.test.bean.Person" pageEncoding="utf-8"%>
<html>
  <body>
  	<jsp:useBean id="person" class="com.test.bean.Person"/>
  	<jsp:getProperty property="name" name="person"/><br>
  	<jsp:getProperty property="age" name="person"/><br>
  	<jsp:getProperty property="address" name="person"/><br>
  	
  	<jsp:setProperty property="name" value="lisi" name="person"/>
  	<jsp:getProperty property="name" name="person"/><br>
  	<%=person.getName() %>
  </body>
</html>

??

?

?

<jsp:setProperty property="name" name="person" param="helloworld"/>

??http://localhost:8080/servletTest/javabean.jsp?helloworld=aaaa

在地址栏中输入这个URL,在JSP中就能将helloworld参数的值取出,对应后台的源码实现是能过

request.getParameter("helloworld");实现。

?

?

上面是指如在:jsp1中定义了这样的JAVABEAN:

<jsp:useBean id=”person” class=”com.test.bean.Person” scope=”request”/>

?

jsp2中可以通过如下方式得到前一个页面定义的JAVABEAN

<%Person p = (Person)request.getAttribute(“person”);%>

?

  相关解决方案