JavaBean 是使用 Java 语言开发的一个可重用的组件,在 JSP 开发中可以使用 JavaBean 减少重复代码,使整个
JSP 代码的开发更简洁。
jsp:useBean 创建 javabean
<jsp:useBean id="实例化对象名称" scope="保存范围" class="类完整名称"/>
Scope,一共有 page,request,session 和 application4 个属性范围,默认是 page;
Student.java
package com.ruanku.model;
public class Student {
private String name;
private int age;
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;
}
}
最原始的方法
javabean01.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<span style="color:#ff0000;"><%@ page import="com.ruanku.model.Student" %></span>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<%
Student student=new Student();
student.setName("王大锤");
student.setAge(18);
%>
<h1>姓名:<%=student.getName() %></h1>
<h1>年龄:<%=student.getAge() %></h1>
</body>
</html>
使用javabean后的javabean02.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<jsp:useBean id="student" scope="page" class="com.ruanku.model.Student"/>
<%
student.setName("王大锤");
student.setAge(18);
%>
<h1>姓名:<%=student.getName() %></h1>
<h1>年龄:<%=student.getAge() %></h1>
</body>
</html>
scope为当前的范围 默认是page就是当前页 class引入的包
page,request,session,application 四种
jsp:setProperty 设置 javabean 属性名
<jsp:setProperty property="属性名称" name="实例化对象的名称" value="属性值" param="参数名称"/>
Property=”*” 自动匹配所有
student.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import="com.ruanku.model.Student"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<form action="javabean03.jsp" method="post">
<table>
<tr>
<td>姓名:</td>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<td>年龄:</td>
<td><input type="text" name="age"/></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="提交"></td>
</tr>
</table>
</form>
</body>
</html>
javabean03.jsp
<%@page import="org.omg.CORBA.Request"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import="com.ruanku.model.Student" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
String name=request.getParameter("name");
String age=request.getParameter("age");
Student student=new Student();
student.setName(name);
student.setAge(Integer.parseInt(age));
%>
<h1>姓名:<%=student.getName() %></h1>
<h1>年龄:<%=student.getAge() %></h1>
</body>
</html>
javabean03-1.jsp
<%@page import="org.omg.CORBA.Request"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
%>
<jsp:useBean id="student" scope="page" class="com.ruanku.model.Student" />
<jsp:setProperty property="*" name="student"/>
<h1>姓名:<%=student.getName() %></h1>
<h1>年龄:<%=student.getAge() %></h1>
</body>
</html>
对某个属性自动设置值
<jsp:setProperty property="age" name="student" value" 12"/>
把年龄设为12
param="参数名称" 匹配 如果表单传来的名称为userName 则
<jsp:setProperty property="name" name="student" param="userName"/>
JSP 代码的开发更简洁。
jsp:useBean 创建 javabean
<jsp:useBean id="实例化对象名称" scope="保存范围" class="类完整名称"/>
Scope,一共有 page,request,session 和 application4 个属性范围,默认是 page;
Student.java
package com.ruanku.model;
public class Student {
private String name;
private int age;
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;
}
}
最原始的方法
javabean01.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<span style="color:#ff0000;"><%@ page import="com.ruanku.model.Student" %></span>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<%
Student student=new Student();
student.setName("王大锤");
student.setAge(18);
%>
<h1>姓名:<%=student.getName() %></h1>
<h1>年龄:<%=student.getAge() %></h1>
</body>
</html>
使用javabean后的javabean02.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<jsp:useBean id="student" scope="page" class="com.ruanku.model.Student"/>
<%
student.setName("王大锤");
student.setAge(18);
%>
<h1>姓名:<%=student.getName() %></h1>
<h1>年龄:<%=student.getAge() %></h1>
</body>
</html>
scope为当前的范围 默认是page就是当前页 class引入的包
page,request,session,application 四种
jsp:setProperty 设置 javabean 属性名
<jsp:setProperty property="属性名称" name="实例化对象的名称" value="属性值" param="参数名称"/>
Property=”*” 自动匹配所有
student.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import="com.ruanku.model.Student"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<form action="javabean03.jsp" method="post">
<table>
<tr>
<td>姓名:</td>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<td>年龄:</td>
<td><input type="text" name="age"/></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="提交"></td>
</tr>
</table>
</form>
</body>
</html>
javabean03.jsp
<%@page import="org.omg.CORBA.Request"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import="com.ruanku.model.Student" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
String name=request.getParameter("name");
String age=request.getParameter("age");
Student student=new Student();
student.setName(name);
student.setAge(Integer.parseInt(age));
%>
<h1>姓名:<%=student.getName() %></h1>
<h1>年龄:<%=student.getAge() %></h1>
</body>
</html>
javabean03-1.jsp
<%@page import="org.omg.CORBA.Request"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
%>
<jsp:useBean id="student" scope="page" class="com.ruanku.model.Student" />
<jsp:setProperty property="*" name="student"/>
<h1>姓名:<%=student.getName() %></h1>
<h1>年龄:<%=student.getAge() %></h1>
</body>
</html>
对某个属性自动设置值
<jsp:setProperty property="age" name="student" value" 12"/>
把年龄设为12
param="参数名称" 匹配 如果表单传来的名称为userName 则
<jsp:setProperty property="name" name="student" param="userName"/>