javaBean组件<1>
2014-11-13 16:46
309 查看
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"/>
相关文章推荐
- javaBean组件<2>
- c语言深入理解<1>static理解
- java-mysql异常<Data truncation: Data too long for column 'name ' at row 1>讨论
- <<展现C#>> 第八章 用C#写组件(修订)
- hadoop "File /user/<user>/input/conf/slaves could only be replicated to 0 nodes, instead of 1"问题及解决办
- 《医间道》读后感<1>
- AMP -> Chapter 3 Concurrent Objects -> Notes<1>
- LINQ to Objects<1>
- php+js+mysql设计的仿webQQ-<1>邮箱验证
- 基于i.MX51的linux时钟体系分析<1>
- Bash Shell中的> 、>> 、 1> 、 2> 、 2>>和 < 、 <<
- IM 开源项目 登录服务器 <1>
- 网络编程笔记<1>
- JavaBean中的<jsp:getProperty>标签与<jsp:setProperty>标签
- Android 学习意外第五季<1>——gps定位经纬度查询地址信息
- 数据库中SQL语句where 1=1及where 1<>1或1=0的妙用
- 进程之间<1>
- 玩转android DDMS<1>
- 2011-12-13_<1>
- &lt;展现C#&gt;第八章用C#写组件