jsp动作
useBean动作
a. 作用
在指定的域范围内实例化或查找JavaBean对象:如果存在则直接返回该JavaBean对象的引用。如果不存在则实例化一个JavaBean对象并将它以指定的名称存储到指定的域范围中。用于连接servlet和html文件
b. 语法格式
<jsp:useBean
id="beanName"
class="package.class"
scope="page|request|application|seesion"
type="class_or_interface_name"
beanName="ser_filename"
></jsp:useBean>
实例:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>useBean标签</title>
</head>
<body>
1.<jsp:useBean id="clock" type="java.io.Serializable" beanName="java.util.Date"></jsp:useBean>
2.<jsp:useBean id="clock" class="java.util.Date"></jsp:useBean>
现在的时间为:<%=clock %>
</body>
</html>
c. scop范围属性
2、getProperty动作
a. 作用
调用get方法,读取属性值并转化成字符串后输出
b. 语法格式
<jsp:getProperty name="bName" property="pName"></jsp:getProperty>
name属性指定了JavaBean对象名,也就是JavaBean的id值
property属性指定JavaBean对象的属性名
如果JavaBean对象的某个属性的值为null,则读取的属性值是一个“null”的字符串
3、setProperty动作
a. 作用
通过setProperty动作设置JavaBean对象的属性值
b. 语法格式
1. <jsp:setPropetty name="bName" property="pName" value="{String|<%= expression %>}"></jsp:setPropetty>
//使用value值表示字符串或表达式
2. <jsp:setProperty name="bName" property="pName" param="paraName(表单元素名)"></jsp:setProperty>
//使用表单元素的值为JavaBean属性赋值
3. <jsp:setProperty name="bName" property="*"></jsp:setProperty>
//通配符(*),使用同名表单元素的值为属性赋值
c. 三种语法格式的应用
1. <jsp:setPropetty name="bName" property="pName" value="{String|<%= expression %>}"></jsp:setPropetty>
/**Book**/
package myClass;
public class Book {
private String bn;
public Book() {
super();
}
public String getBn() {
return bn;
}
public void setBn(String bn) {
this.bn = bn;
}
}
/**book.jsp**/
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<%-- <jsp:useBean id="book" class="myClass.Book" scope="request"></jsp:useBean>--%>
</head>
<body>
<jsp:useBean id="BeanID" class="myClass.Book" scope="page"></jsp:useBean>
<jsp:setProperty name="BeanID" property="bn" value="程序设计"></jsp:setProperty>
<jsp:getProperty name="BeanID" property="bn"/>
</body>
</html>
2. <jsp:setProperty name="bName" property="pName" param="paraName(表单元素名)"></jsp:setProperty>
/**book.html**/
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>setProperty</title>
</head>
<body>
<form action="dispBook.jsp">
<ul>
<li>ISBN :<input type="text" name="bn"></li>
<li>书 名:<input type="text" name="book"></li>
<li>作 者:<input type="text" name="auth"></li>
<li>是否售出:<input type="radio" name="sta" value="true">是
<input type="radio" name="sta" value="false">否
</li>
<li>
<input type="submit" value="提交">
<input type="reset" value="取消">
</li>
</ul>
</form>
</body>
</html>
/**dispBook.jsp**/
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>setProperty</title>
</head>
<body>
<%--利用useBean动作实例化对象book--%>
<jsp:useBean id="book" class="myClass.BooksOne" scope="application"></jsp:useBean>
<%--使用setProperty动作利用表单元素参数book属性赋值--%>
<jsp:setProperty name="book" property="isbn" param="bn"></jsp:setProperty>
<jsp:setProperty name="book" property="bookName" param="book"></jsp:setProperty>
<jsp:setProperty name="book" property="bookAuthor" param="auth"></jsp:setProperty>
<jsp:setProperty name="book" property="salaStatus" param="sta"></jsp:setProperty>
<%--使用geyProperty动作获取对象book的属性值--%>
国际标准书号:<jsp:getProperty name="book" property="isbn"/><br>
书名:<jsp:getProperty name="book" property="bookName"/><br>
作者:<jsp:getProperty name="book" property="bookAuthor"/><br>
销售状态:<jsp:getProperty name="book" property="salaStatus"/>
</body>
</html>
/**BooksOne**/
package myClass;
public class BooksOne {
private String isbn;
private String bookName;
private String bookAuthor;
private String salaStatus;
public BooksOne() {
super();
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getBookAuthor() {
return bookAuthor;
}
public void setBookAuthor(String bookAuthor) {
this.bookAuthor = bookAuthor;
}
public String getSalaStatus() {
return salaStatus;
}
public void setSalaStatus(String salaStatus) {
this.salaStatus = salaStatus;
}
}
3. <jsp:setProperty name="bName" property="*"></jsp:setProperty> //个人感觉没啥用
/**book2.html**/
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="dispBook.jsp">
<ul>
<li>ISBN :<input type="text" name="bn"></li>
<li>书 名:<input type="text" name="book"></li>
<li>作 者:<input type="text" name="auth"></li>
<li>是否售出:<input type="radio" name="sta" value="true">是
<input type="radio" name="sta" value="false">否
</li>
<li>
<input type="submit" value="提交">
<input type="reset" value="取消">
</li>
</ul>
</form>
</body>
</html>
/**dispBook2.jsp**/
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%--利用useBean动作实例化对象book--%>
<jsp:useBean id="book" class="myClass.BooksOne" scope="application"></jsp:useBean>
<%--表单元素的自动给同名的book属性赋值--%>
<jsp:setProperty name="book" property="*"></jsp:setProperty>
<%--使用geyProperty动作获取对象book的属性值--%>
国际标准书号:<jsp:getProperty name="book" property="isbn"/><br>
书名:<jsp:getProperty name="book" property="bookName"/><br>
作者:<jsp:getProperty name="book" property="bookAuthor"/><br>
销售状态:<jsp:getProperty name="book" property="salaStatus"/>
</body>
</html>
d. 作业
创建一个类,并封装书名和书号。再创建一个jsp页面,实例化此类,并为书名和书号分别赋值,再获取书名和书号相应的值显示出来。
/**mybook.java**/
package myClass;
public class myBook {
private String bn; //书号
private String bookName; //书名
public myBook() {
super();
}
public String getBn() {
return bn;
}
public void setBn(String bn) {
this.bn = bn;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
}
/**mybook.jsp**/
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<jsp:useBean id="mybook" class="myClass.myBook" scope="application"></jsp:useBean>
<%--<jsp:setProperty name="mybook" property="bn" value="2243"></jsp:setProperty>--%>
<%--<jsp:setProperty name="mybook" property="bookName" value="C语言"></jsp:setProperty>--%>
<%--书号:<jsp:getProperty name="mybook" property="bn"--%>
<%--<br>--%>
<%--书名:<jsp:getProperty name="mybook" property="bookName"/>--%>
<%
mybook.setBn("2243");
mybook.setBookName("C语言");
%>
<%
out.println("书号:"+mybook.getBn()+"<br>"+"书名:"+mybook.getBookName());
%>
</body>
</html>