⑴ javabean到底有什麼用感覺它和一般java文件沒什麼區別,希望高手能拿個例子來體現javabean的作用和優勢謝
jsp的一個重要特性就是可以用javaBean實現功能的擴展。將大部分功能放在javaBean中完成,以使jsp頁面程序更干凈簡潔、利於維護。JavaBean可以很方便的用來捕獲頁面表單的輸入並完成各種業務邏輯的處理。如下就是一個Hello示例:
testA.jsp頁面:
<%@ page contentType="text/html;charset=GBK" %>
<html>
<head>
<title>示例</title>
</head>
<body scroll=no>
<form name="frma" method="post" action="testB.jsp" >
<p>
你的姓名:
<input type="text" size="15" name="yourName" value="" id=yourName>
<input type="button" align="center" name="subBtn" value="[提交]" onClick="f_check()" id=subBtn>
</p>
</form>
</body>
</html>
<script language="JavaScript" type="text/javascript">
<!--
function f_check(){
if(document.forms(0).yourName.value==""){
alert("請輸入姓名");
}else{
document.forms(0).submit();
}
}
-->
</SCRIPT>
testB.jsp頁面
<%@ page contentType="text/html;charset=GBK" %>
<html>
<head>
<title>示例</title>
</head>
<jsp:useBean id="tBean" scope="page" class="bean.TestBean" >
<jsp:setProperty name="tBean" property="*" />
</jsp:useBean>
<body scroll=no>
<form name="frmb" method="post" action="" >
<p>
<%=tBean.hello()%>
</p>
</form>
</body>
</html>
TestBean.java 代碼:
package bean;
public class TestBean{
private String yourName = "";
public void setYourName(String yourName){
this.yourName = ConvertGBK(yourName);
}
public String hello(){
String strHello = "Hello:"+yourName;
return strHello;
}
//漢字轉換方法
public String ConvertGBK(String str){
String strReturn="";
try{
strReturn=new String(str.getBytes("ISO-8859-1"),"GBK");
}catch(Exception ex){
System.out.println("TestBean.ConvertGBK():ex="+ex.toString());
}
finally{
return strReturn;
}
}
}
testA.jsp頁面上「提交」按鈕將表單提交給testB.jsp頁面,testB.jsp獲得的testA.jsp中yourName文本框的值並在實例化TestBean後,執行bean中的setYourName方法,接著執行hello方法,在頁面上輸出對你問好的語句。
這個簡單的示例體現了在jsp中使用javaBean的兩個重要方面,一個是捕獲表單的輸入並保存,一個是執行邏輯功能。所以,依此兩個功能還可以將用在jsp中的javaBean分為值Bean(value bean)和工具Bean (utility bean),如下:
值Bean
package bean;
public class TestValueBean{
private String yourName = "";
public void setYourName(String yourName){
this.yourName = ConvertGBK(yourName);
}
//漢字轉換方法
public String ConvertGBK(String str){
String strReturn="";
try{
strReturn=new String(str.getBytes("ISO-8859-1"),"GBK");
}catch(Exception ex){
System.out.println("TestBean.ConvertGBK():ex="+ex.toString());
}
finally{
return strReturn;
}
}
}
工具Bean
package bean;
public class TestUtilityBean{
public String hello(TestValueBean tvBean){
String strHello = "Hello:"+tvBean.getName();
return strHello;
}
public String hello(String yourName){
String strHello = "Hello:"+yourName;
return strHello;
}
}
當然,從這個例子看是沒有必要分開value bean和utility bean的,但在具有復雜業務邏輯的web應用程序中就可以用value bean實現對表單輸入的捕獲、保存,減少對資料庫中那些值幾乎不變的實體的訪問,或將value bean放在一定作用域內使此作用域內的多個jsp頁面共享。用utility bean完成操作資料庫、數據處理等業務邏輯,以value bean 或頁面傳遞的值為參數。
⑵ jsp文本框中的內容增多時如何自動換行
文本框不能換行的,要是文本框可以換行就成了文本域了,你要是覺得文本框不夠長,可以通過設置其width屬性改變其長度;
⑶ 我下了一個Ewebeditor,請問怎麼引用
總結了以下,你基本提出了三大問題
一:關於引用
先創建一個textarea域,示例:
<textarea name="content" style="display:none"></textarea>
其中style="display:none" 決定了這個文本域是隱藏的.
在調用eWebEditer,示例:
<iframe ID="eWebEditor1" src="/ewebeditor.htm?id=content1&style=coolblue" frameborder="0" scrolling="no" width="500" HEIGHT="350"></iframe>
其中src="/ewebeditor.htm?id=content&style=coolblue"中id=後面的第一個參數必須和textarea域的name屬性的值一致.
輸入信息頁面完整示例:
<html>
<head>
<title>Untitled</title>
</head>
<body>
<form name="myform" method="post" action="display.asp">
<textarea name="content" style="display:none"></textarea>
<iframe ID="eWebEditor1" src="/ewebeditor.htm?id=content&style=coolblue" frameborder="0" scrolling="no" width="500" HEIGHT="350"></iframe>
<input type="submit" name="btnSubmit" value="Submit">
</form>
</body>
</html>
二.關於獲取eWebEditor中輸入的值:
在上面完整代碼中<form name="myform" method="post" action="display.asp">這一段中 屬性action="display.asp"
display.asp指示了處理上面表單的程序.下面我們在display.asp中獲取eWebEditor中編輯的值,示例:
<%
sContent = Request.Form("content")
Response.Write sContent
%>
其中Request.Form("content")中參數必須和textarea域中name的屬性一致.
這樣我們在display.asp已經獲取了在eWebEditor中編輯的代碼.
三.將獲取的文本插入資料庫:
由於eWebEditor中取出的值可能太長,你既然使用的是ACCESS資料庫,那建議將保存該欄位的類型設置為備注型,這樣能存儲更長的字元傳.
在display.asp已經獲取了eWebEditor編輯框框中的值,保存在了變數sContent了
要插入資料庫的話,首先要做的就是讓當前頁面能夠連接上資料庫.請參看下面11中連接資料庫的方法,選擇合適你自己的資料庫和方法:
1.Access資料庫的DSN-less連接方法:
set adocon=Server.Createobject("adodb.connection")
adoconn.Open"Driver={Microsoft Access Driver(*.mdb)};DBQ="& _
Server.MapPath("資料庫所在路徑")
2.Access OLE DB連接方法:
set adocon=Server.Createobject("adodb.connection")
adocon.open"Provider=Microsoft.Jet.OLEDB.4.0;"& _
"Data Source=" & Server.MapPath("資料庫所在路徑")
3.SQL server連接方法:
set adocon=server.createobject("adodb.recordset")
adocon.Open"Driver={SQL Server};Server=(Local);UID=***;PWD=***;"& _
"database=資料庫名;"
4.SQL server OLE DB連接方法:
set adocon=Server.Createobject("adodb.connection")
adocon.open"provider=SQLOLEDB.1;Data Source=RITANT4;"& _
"user ID=***;Password=***;"& _
"inital Catalog=資料庫名"
5.Oracle 連接方法:
set adocon=Server.Createobject("adodb.connection")
adocon.open"Driver={microsoft odbc for oracle};server=oraclesever.world;uid=admin;pwd=pass;"
6.Oracle OLE DB 連接方法:
set adocon=Server.Createobject("adodb.connection")
adocon.open"Provider=OraOLEDB.Oracle;data source=dbname;user id=admin;password=pass;"
7.dBase 連接方法:
set adocon=Server.Createobject("adodb.connection")
adocon.open"Driver={microsoft dbase driver(*.dbf)};driverid=277;dbq=------------;"
8.mySQL 連接方法:
set adocon=Server.Createobject("adodb.connection")
adocon.open"Driver={mysql};database=yourdatabase;
uid=username;pwd=yourpassword;option=16386;"
9.Visual Foxpro 連接方法:
set adocon=Server.Createobject("adodb.connection")
adocon.open"Driver={microsoft Visual Foxpro driver};sourcetype=DBC;sourceDB=*.dbc;Exclusive=No;"
10.MS text 連接方法:
set adocon=Server.Createobject("adodb.connection")
adocon.open"Driver={microsoft text driver(*.txt; *.csv)};dbq=-----;"&_
"extensions=asc,csv,tab,txt;Persist SecurityInfo=false;"
11.MS text OLE DB 連接方法:
set adocon=Server.Createobject("adodb.connection")
adocon.open"Provider=microsof.jet.oledb.4.0;data source=your_path;"&_
"Extended Properties'text;FMT=Delimited'"
連接成功後使用插入語句添加記錄Insert
Insert語法:
Insert into table(field1,field2,....) Values (value1,value2,....)
例子:添加一作者是"cancer"的記錄入book表:
insert into book (bookno,author,bookname) values (』CF001』,』cancer』,』Cancer無組件上傳程序』)
本人會JAVA,經常使用JSP,對ASP不太了解,但原理都是一樣的,希望各位網友批評指正.
⑷ 在jsp中,如何將application對象的值賦給一個文本框
首先要獲取 application的值必須是jsp頁面
<input type="text" value="<%=application.getAttribute("name") %>">
<input type="text" value="<%=session.getAttribute("name") %>">
<input type="text" value="<%=request.getAttribute("name") %>">
這些都可以直接寫,嵌在頁面中就可以了,name是你要獲取的變數名稱
⑸ 怎麼讓textarea的寬度不能改變高度可以調整
可以給textarea加一個id這樣可以實現單獨改寬度如$("#aa")。css("width","300");也可以集體修改如$("textarea")。css("width","300"),這樣會把所有的textarea的寬度都改為300。
style="resize:none;" 這樣禁止拖拽改變大小。
style="max-width:500px,"這樣寬度固定為500px。
textarea定義和用法:
標簽定義多行的文本輸入控制項。
文本區中可容納無限數量的文本,其中的文本的默認字體是等寬字體(通常是Courier)。
可以通過cols和rows屬性來規定textarea的尺寸,不過更好的辦法是使用CSS的height和width屬性。
在文本輸入區內的文本行間,用"%OD%OA"(回車/換行)進行分隔;通過標簽的wrap屬性設置文本輸入區內的換行模式。textarea標簽是成對出現的,以開始,以結束。
定義一個文本區域(text-area)(一個多行的文本輸入區域)。用戶可在此文本區域中寫文本。在一個文本區中,您可輸入無限數量的文本。文本區中的默認字體是等寬字體(fixedpitch)。
屬性。最常用的屬性是cols和rows,用來規定textarea的尺寸。另外,還有其他一些屬性如如下:
accesskey 規定訪問元素的鍵盤快捷鍵。
class 規定元素的類名(用於規定樣式表中的類)。
contenteditable 規定是否允許用戶編輯內容。
contextmenu 規定元素的上下文菜單。
dir 規定元素中內容的文本方向。
draggable 規定是否允許用戶拖動元素。
dropzone 規定當被拖動的項目/數據被拖放到元素中時會發生什麼。
hidden 規定該元素是無關的。被隱藏的元素不會顯示。
id 規定元素的唯一ID。
lang 規定元素中內容的語言代碼。
spellcheck 規定是否必須對元素進行拼寫或語法檢查。
style 規定元素的行內樣式。
tabindex 規定元素的tab鍵控制次序。
title 規定有關元素的額外信息。
Common--一般屬性、cols--多行輸入域的列數、rows--行輸入域的行數、accesskey--表單的快捷鍵訪問方式、disabled--輸入域無法獲得焦點,無法選擇,以灰色顯示,在表單中不起任何作用、readonly--輸入域可以選擇,但是無法修改、tabindex--輸入域,使用"tab"鍵的遍歷順序。