Date: 12/13
/======================================================/
<jsp:useBean>
與 import 功能相同
<jsp:useBean id=”產生的實體物件” scope="儲存範圍" class=”套件.class name” />
※ scope => 表示這個物件的儲存範圍,一共有四個屬性: page, request, session, application
/======================================================/
<jsp: setProerty>
可以透過 * 的形式完成屬性的自動設定
一其有4種方法
自動比對: <jsp: setProperty name=”實體的名稱(id)” property=”*” />
指定屬性: <jsp: setProperty name=”實體的名稱(id)” property=”屬性明稱” />
指定參數: <jsp: setProperty name=”實體的名稱(id)” property=”屬性明稱” param=”參數名稱” />
指定內容: <jsp: setProperty name=”實體的名稱(id)” property=”屬性明稱” value=”內容” />
/======================================================/
##ReadMore##
練習JavaBean --> demo_01.jsp、demo_02.jsp、SimpleBean.java
//demo_01.jsp <%@ page language="java" import="java.util.*" pageEncoding="BIG5"%> <%@ page import="com.*"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> </head> <body> <% SimpleBean simple=new SimpleBean(); simple.setName("聯成電腦"); simple.setAge(30); %> <h3>姓名:<%=simple.getName()%></h3> <h3>年齡:<%=simple.getAge()%></h3> </body> </html>
//demo_02.jsp <%@ page language="java" import="java.util.*" pageEncoding="BIG5"%> <jsp:useBean id="simple" scope="page" class="com.SimpleBean"></jsp:useBean> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> </head> <body> <% simple.setName("Aaron"); simple.setAge(25); %> <h3>姓名:<%=simple.getName()%></h3> <h3>年齡:<%=simple.getAge()%></h3> </body> </html>
//SimpleBean.java package com; public class SimpleBean { 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; } }/======================================================/
接著練習透過JavaBean來驗證所輸入的資料是否符合所設定的範圍值:
index.jsp --> 首頁輸入介面
Register.java --> JavaBean(條件)
check.jsp --> 審核是否符合,不符合就是顯示回首頁,符合就是顯示成功頁面
success.jsp --> 顯示所輸入的正確資料頁面
//index.jsp <%@ page language="java" import="java.util.*" pageEncoding="BIG5"%> <jsp:useBean id="reg" scope="request" class="com.Register" /> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> </head> <body> <form action="check.jsp" method="post"> <center> 使用者名稱:<input type="text" name="name" value="<jsp:getProperty name="reg" property="name"/>"/><%=reg.getErrorMsg("errname")%><br> 年齡:<input type="text" name="age" value="<jsp:getProperty name="reg" property="age"/>"/><%=reg.getErrorMsg("errage")%><br> E-mail:<input type="text" name="email" value="<jsp:getProperty name="reg" property="email"/>"/><%=reg.getErrorMsg("erremail")%><br> <input type="submit" value="註冊"> <input type="reset" value="重設"> </center> </form> </body> </html>
//Register.java package com; import java.util.HashMap; import java.util.Map; public class Register { private String name; private String age; private String email; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } private Maperrors=null; public Register(){ this.name=""; this.age=""; this.email=""; this.errors=new HashMap (); } public String getErrorMsg(String key){ String value=this.errors.get(key); return (value==null) ? "" : value; } public boolean isValidate(){ boolean flage=true; if(!this.name.matches("\\w{6,15}")){ flage=false; this.name=""; //清空原本的內容 this.errors.put("errname", "請重新輸入6~15字名稱"); } if(!this.age.matches("\\d+")){ flage=false; this.age=""; this.errors.put("errage", "請重新輸入正確數字年齡"); } if(!this.email.matches("\\w+@\\w+\\.\\w+\\.?\\w*")){ flage=false; this.email=""; this.errors.put("erremail", "請重新輸入正確E-Mail"); } return flage; } }
//check.jsp <%@ page language="java" import="java.util.*" pageEncoding="BIG5"%> <jsp:useBean id="reg" scope="request" class="com.Register" /> <jsp:setProperty name="reg" property="*" /> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'check.jsp' starting page</title> </head> <body> <% if(reg.isValidate()){ //對所輸入的內容進行檢查 %> <jsp:forward page="success.jsp"/> <% }else{ %> <jsp:forward page="index.jsp"/> <% } %> </body> </html>
//success.jsp <%@ page language="java" import="java.util.*" pageEncoding="BIG5"%> <jsp:useBean id="reg" scope="request" class="com.Register" /> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'success.jsp' starting page</title> </head> <body> <center> 使用者名稱:<jsp:getProperty name="reg" property="name" /><br> 年齡:<jsp:getProperty name="reg" property="age" /><br> E-mail:<jsp:getProperty name="reg" property="email" /> </center> </body> </html>/======================================================/
練習
//input.jsp <%@ page language="java" import="java.util.*" pageEncoding="BIG5"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'input.jsp' starting page</title> </head> <body> 姓名:<input type="text" name="name" > 年齡:<input type="text" name="age" ><br> <input type="submit" value="傳送"> <input type="reset" value="重置"> </body> </html>
//input_bean.jsp <%@ page language="java" import="java.util.*" pageEncoding="BIG5"%> <jsp:useBean id="simple" scope="page" class="com.SimpleBean" /> <jsp:setProperty name="simple" property="name" param="name" /> <jsp:setProperty name="simple" property="age" param="age" /> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'input_bean.jsp' starting page</title> </head> <body> <center> <% simple.setName("aaa"); simple.setAge(12); %> <h3>姓名:<%=simple.getName()%></h3> <h3>年齡:<%=simple.getAge()%></h3> </center> </body> </html>