使用EL访问javabean

book.java

package chap08;

public class Book {
	private String bookName;
	private Publisher publisher;

	public void setBookName(String name) {
		this.bookName = name;
	}

	public String getBookName() {
		return this.bookName;
	}

	public void setPublisher(Publisher thepublisher) {
		this.publisher = thepublisher;
	}

	public Publisher getPublisher() {
		return this.publisher;
	}
}

Publisher.java

package chap08;

public class Publisher {
	private String PublisherName;
	private String address;

	public Publisher() {

	}

	public Publisher(String name, String addr) {
		this.PublisherName = name;
		this.address = addr;
	}

	public String getPublisherName() {
		return this.PublisherName;
	}

	public void setPublisherName(String name) {
		this.PublisherName = name;
	}

	public String getAddress() {
		return this.address;
	}

	public void setAddress(String addr) {
		this.address = addr;
	}
}

BeensettingServlet.java 关键代码

public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//创建出版社对象
		Publisher publisher = new Publisher("TsingHua Publisher","TsingHua University");
		//创建书籍对象
		Book book = new Book();
		//设置书籍对象属性
		book.setPublisher(publisher);
		book.setBookName("JSPProgramming");
		//将book这个javabean对象放在request作用域中
		request.setAttribute("bookBean", book);
		//跳转到jsp页面
		RequestDispatcher dis = request.getRequestDispatcher("/getBean.jsp");
		dis.forward(request, response);
	}

getBean.jsp

<%@ page contentType="text/html; charset=utf-8" language="java" import="java.util.*" errorPage="" %>
<%
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%>">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>My JSP 'MyJsp.jsp' starting page</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 bgcolor = "#ffffff">
  <dir align = "center">
  <h1>  ${bookBean.bookName}的出版者是${bookBean.publisher.publisherName}!  </h1>
  </dir>
  
  </body>
</html>

使用EL访问javabean》上有4条评论

发表评论

电子邮件地址不会被公开。 必填项已用*标注