#include "sqlite3.h"
#include "cgic.h"
int cgiMain() {
printf("Content-type:text/html\n\n");
printf("");
sqlite3 *db=NULL;
char *zErrMsg = 0;
int rc;
rc = sqlite3_open("test.db", &db);
if(rc){
printf("Can't open database\n"); //這里改了。要是按原先的,會提示stderr未定義,我不知道為什么。哪位朋友知道一定要告訴我哦。
sqlite3_close(db);
exit(1);
}
else printf("open test.db successfully!\n");
char username[241];
cgiFormString("username", username, 241);
fprintf(cgiOut, "username: \n");
cgiHtmlEscape(username);
fprintf(cgiOut, "
\n");
char password[241];
cgiFormString("password", password, 241);
fprintf(cgiOut, "password: \n");
cgiHtmlEscape(password);
fprintf(cgiOut, "
\n");
char sql[300]={'\0'}; //不能用指針!
//插入數(shù)據(jù)
sprintf(sql, "INSERT INTO \"user\" VALUES('%s', '%s');", username,password);
//sql = "INSERT INTO \"user\" VALUES('username', 'password');" ;
sqlite3_exec( db , sql , 0 , 0 , &zErrMsg );
printf(sql);
printf("插入數(shù)據(jù)成功!\n");
int nrow = 0, ncolumn = 0;
char **azResult; //二維數(shù)組存放結(jié)果
//查詢數(shù)據(jù)
/*
int sqlite3_get_table(sqlite3*, const char *sql,char***result , int *nrow , int *ncolumn ,char **errmsg );
result中是以數(shù)組的形式存放你所查詢的數(shù)據(jù),首先是表名,再是數(shù)據(jù)。
nrow ,ncolumn分別為查詢語句返回的結(jié)果集的行數(shù),列數(shù),沒有查到結(jié)果時返回0
*/
char *sql2 = "SELECT * FROM user";
sqlite3_get_table( db , sql2 , &azResult , &nrow , &ncolumn , &zErrMsg );
int i = 0 ;
printf( "row:%d column=%d
" , nrow , ncolumn );
printf( "\nThe result of querying is : \n" );
for( i=0 ; i<( nrow + 1 ) * ncolumn ; i++ )
printf( "azResult[%d] = %s
", i , azResult[i] );
//釋放掉 azResult 的內(nèi)存空間
sqlite3_free_table( azResult );
sqlite3_close(db); //關(guān)閉數(shù)據(jù)庫
return 0;
}
請注意數(shù)據(jù)庫文件 test.db的訪問權(quán)限! 這里改成777!
posted @
2008-03-01 17:11 Super·shen BLOG 閱讀(1740) |
評論 (1) |
編輯 收藏
[轉(zhuǎn)自] http://webdn.trueself.cn/archives/107
posted @
2008-02-28 14:19 Super·shen BLOG 閱讀(745) |
評論 (0) |
編輯 收藏
◆ 使用strtok函數(shù)分割。
原型:char *strtok(char *s, char delim);
strtok在s中查找包含在delim中的字符并用NULL('\0')來替換,直到找遍整個字符串。
功能:分解字符串為一組字符串。s為要分解的字符串,delim為分隔符字符串。
說明:首次調(diào)用時,s指向要分解的字符串,之后再次調(diào)用要把s設(shè)成NULL。
strtok在s中查找包含在delim中的字符并用NULL('\0')來替換,直到找遍整個字符串。
返回值:從s開頭開始的一個個被分割的串。當(dāng)沒有被分割的串時則返回NULL。
所有delim中包含的字符都會被濾掉,并將被濾掉的地方設(shè)為一處分割的節(jié)點。
使用例:
#include <stdio.h>
#include <string.h>
#include <stdio.h>
#include <string.h>
int main(int argc,char **argv)
{
char * buf1="aaa, ,a, ,,,bbb-c,,,ee|abc";
/* Establish string and get the first token: */
char* token = strtok( buf1, ",-|");
while( token != NULL )
{
/* While there are tokens in "string" */
printf( "%s ", token );
/* Get next token: */
token = strtok( NULL, ",-|");
}
return 0;
}
OUT 值:
aaa
a
bbb
c
ee
abc
◆ 使用strstr函數(shù)分割。
原型:extern char *strstr(char *haystack,char *needle);
用法:#include <string.h>
功能:從字符串haystack中尋找needle第一次出現(xiàn)的位置(不比較結(jié)束NULL)
說明:返回指向第一次出現(xiàn)needle位置的指針,如果沒找到則返回NULL。
使用例:
#include <stdio.h>
#include <string.h>
int main(int argc,char **argv)
{
char *haystack="aaa||a||bbb||c||ee||";
char *needle="||";
char* buf = strstr( haystack, needle);
while( buf != NULL )
{
buf[0]='\0';
printf( "%s\n ", haystack);
haystack = buf + strlen(needle);
/* Get next token: */
buf = strstr( haystack, needle);
}
return 0;
}
OUT 值:
aaa
a
bbb
c
ee
◆ strtok比較適合多個字符作分隔符的場合,而strstr適合用字符串作分隔符的場合。
posted @
2008-02-27 16:35 Super·shen BLOG 閱讀(1474) |
評論 (0) |
編輯 收藏
我們來看看到底如何從POST表單收集數(shù)據(jù)到CGI程序,下面給出了一個比較簡單的C源代碼:
#include<stdio.h>
#include<stdlib.h>
#define MAXLEN 80
#define EXTRA 5
/* 4個字節(jié)留給字段的名字"data", 1個字節(jié)留給"=" */
#define MAXINPUT MAXLEN+EXTRA+2
/* 1個字節(jié)留給換行符,還有一個留給后面的NULL */
#define DATAFILE "../data/data.txt"
/* 要被添加數(shù)據(jù)的文件 */
void unencode(char *src, char *last, char *dest)
{
for(; src != last; src++, dest++)
if(*src == "+")
*dest = " ";
else if(*src == "%") {
int code;
if(sscanf(src+1,"%2x",&code)!=1)code="?";
*dest=code;
src +=2;}
else
*dest=*src;
*dest=" ";
*++dest="";
}
int main(void)
{
char *lenstr;
char input[MAXINPUT], data[MAXINPUT];
long len;
printf("%s%c%c", "Content-Type:text/html;charset=gb2312",13,10);
printf("<TITLE>Response</TITLE>");
lenstr=getenv("CONTENT_LENGTH");
if(lenstr==NULL || sscanf(lenstr,"%ld",&len)!=1 || len>MAXLEN)
printf("<P>表單提交錯誤");
else{
FILE *f;
fgets(input, len+1, stdin);
unencode(input+EXTRA, input+len, data);
f =fopen(DATAFILE, "a");
if(f == NULL)
printf("<P>對不起,意外錯誤,不能夠保存你的數(shù)據(jù)");
else
fputs(data, f);
fclose(f);
printf("<P>非常感謝,您的數(shù)據(jù)已經(jīng)被保存<BR>%s",data);
}
return 0;
}
從本質(zhì)上來看,程序先從CONTENT_LENGTH環(huán)境變量中得到數(shù)據(jù)的字長,然后讀取相應(yīng)長度的字符串。因為數(shù)據(jù)內(nèi)容在傳輸?shù)倪^程中是經(jīng)過了編碼的,所以必須進行相應(yīng)的解碼。編碼的規(guī)則很簡單,主要的有這幾條:
1. 表單中每個每個字段用字段名后跟等號,再接上上這個字段的值來表示,每個字段之間的內(nèi)容用&連結(jié); 2. 所有的空格符號用加號代替,所以在編碼碼段中出現(xiàn)空格是非法的;
3. 特殊的字符比如標(biāo)點符號,和一些有特定意義的字符如“+”,用百分號后跟其對應(yīng)的ACSII碼值來表示。
例如:如果用戶輸入的是:
Hello there!
那么數(shù)據(jù)傳送到服務(wù)器的時候經(jīng)過編碼,就變成了data=Hello+there%21 上面的unencode()函數(shù)就是用來把編碼后的數(shù)據(jù)進行解碼的。在解碼完成后,數(shù)據(jù)被添加到data.txt文件的尾部,并在瀏覽其中回顯出來。
把文件編譯完成后,把它改名為collect.cgi后放在CGI目錄中就可以被表單調(diào)用了。下面給出了其相應(yīng)的表單:
<FORM ACTION="/cgi-bin/collect.cgi" METHOD="POST" >
<P>請輸入您的留言(最多80個字符):<BR>
<INPUT NAME="data" SIZE="60" MAXLENGTH="80" ><BR>
<INPUT TYPE="SUBMIT" VALUE="確定">
</FORM >
事實上,這個程序只能作為例子,是不能夠正式的使用的。它漏掉了很關(guān)鍵的一個問題:當(dāng)有多個用戶同時像文件寫入數(shù)據(jù)是,肯定會有錯誤發(fā)生。而對于一個這樣的程序而言,文件被同時寫入的幾率是很大的。因此,在比較正式的留言版程序中,都需要做一些更多的考慮,比如加入一個信號量,或者是借助于一個鑰匙文件等。因為那只是編程的技巧問題,在這兒就不多說了。
posted @
2008-02-27 13:52 Super·shen BLOG 閱讀(2772) |
評論 (1) |
編輯 收藏
啥都不說,直接看代碼!
簡單輸出代碼
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main()
{
printf("Content-type:text/html\n\n");
printf("hello world!");
fflush(stdout);
}
處理get代碼
#include <stdio.h>
#include <stdlib.h>
int zmain(void)
{char *data;
long m,n;
printf("%s%c%c\n","Content-Type:text/html;charset=iso-8859-1",13,10);
printf("<TITLE>Multiplication results</TITLE>\n");
printf("<H3>Multiplication results</H3>\n");
data = getenv("QUERY_STRING");
if(data == NULL)
printf("<P>Error! Error in passing data from form to script.");
else if(sscanf(data,"m=%ld&n=%ld",&m,&n)!=2)
printf("<P>Error! Invalid data. Data must be numeric.");
else
printf("<P>The product of %ld and %ld is %ld.",m,n,m*n);
return 0;
}
處理post代碼
#include<stdio.h>
#include<stdlib.h>
void main()
{
int i,n;
printf("Content-type:text/html\n\n");
n=0;
if(getenv("CONTENT_LENGTH"))
n=atoi(getenv("CONTENT_LENGTH"));
printf("%d",n);
for(i=0;i<n;i++)
putchar(getchar());
putchar('\n');
fflush(stdout);
}
還是代碼
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* 轉(zhuǎn)換函數(shù)聲明 */
int htoi(char *);
/* 主函數(shù) */
void zmain() {
int i,n;
char c;
printf ("Content-type: text/html\n\n");
n=0;
if (getenv("CONTENT_LENGTH"))
n=atoi(getenv("CONTENT_LENGTH"));
for (i=0; i<n;i++){
int is_eq=0; //判斷是否有等于號。
c=getchar();
switch(c){
case '&':
c='\n';
break;
case '+':
c='+';
break;
case '%':
{
char s[3];
s[0]=getchar();
s[1]=getchar();
s[2]=0;
c=htoi(s);
i+=2;
}
break;
case '=':
c='=';
is_eq=1;
break;
};
putchar(c);
//if (is_eq) putchar(' ');
}
putchar ('\n');
fflush(stdout);
}
/* 轉(zhuǎn)換為小寫 */
int islower (int ch )
{
return (unsigned int) (ch - 'a') < 26u;
}
/* convert hex string to int 16進制轉(zhuǎn)換成10進制 */
int htoi(char *s)
{
char *digits="0123456789ABCDEF";
if(islower(s[0])) s[0]=toupper(s[0]);
if(islower(s[1])) s[1]=toupper(s[1]);
return 16 * (strchr(digits, s[0]) -strchr(digits,'0') ) +(strchr(digits,s[1])-strchr(digits,'0'));
}
#include<stdio.h>
#include<stdlib.h>
void zzzmain()
{
int i,n;
printf("Content-type:text/html\n\n");
n=0;
if(getenv("CONTENT_LENGTH"))
n=atoi(getenv("CONTENT_LENGTH"));
printf("%d",n);
for(i=0;i<n;i++)
putchar(getchar());
putchar('\n');
fflush(stdout);
}
posted @
2008-02-26 15:37 Super·shen BLOG 閱讀(750) |
評論 (0) |
編輯 收藏
http://samhe.javaeye.com/blog/142416
posted @
2008-01-18 17:14 Super·shen BLOG 閱讀(312) |
評論 (0) |
編輯 收藏
DWR(Direct Web Remoting)是一個WEB遠程調(diào)用框架.利用這個框架可以讓AJAX開發(fā)變得很簡單.利用DWR可以在客戶端利用JavaScript直接調(diào)用服務(wù)端的Java方法并返回值給JavaScript就好像直接本地客戶端調(diào)用一樣(DWR根據(jù)Java類來動態(tài)生成JavaScrip代碼).它的最新版本DWR0.6添加許多特性如:支持Dom Trees的自動配置,支持Spring(JavaScript遠程調(diào)用spring bean),更好瀏覽器支持,還支持一個可選的commons-logging日記操作.
以上摘自open-open,它通過反射,將java翻譯成javascript,然后利用回調(diào)機制,輕松實現(xiàn)了javascript調(diào)用Java代碼。
其大概開發(fā)過程如下:
1.編寫業(yè)務(wù)代碼,該代碼是和dwr無關(guān)的。
2.確認業(yè)務(wù)代碼中哪些類、哪些方法是要由javascript直接訪問的。
3.編寫dwr組件,對步驟2的方法進行封裝。
4.配置dwr組件到dwr.xml文件中,如果有必要,配置convert,進行java和javascript類型互轉(zhuǎn)。
5.通過反射機制,dwr將步驟4的類轉(zhuǎn)換成javascript代碼,提供給前臺頁面調(diào)用。
5.編寫網(wǎng)頁,調(diào)用步驟5的javascript中的相關(guān)方法(間接調(diào)用服務(wù)器端的相關(guān)類的方法),執(zhí)行業(yè)務(wù)邏輯,將執(zhí)行結(jié)果利用回調(diào)函數(shù)返回。
6.在回調(diào)函數(shù)中,得到執(zhí)行結(jié)果后,可以繼續(xù)編寫業(yè)務(wù)邏輯的相關(guān)javascript代碼。
下面以用戶注冊的例子,來說明其使用。(注意,本次例子只是用于演示,說明DWR的使用,類設(shè)計并不是最優(yōu)的)。
1.先介紹下相關(guān)的Java類
User: 用戶類,
public class User {
//登陸ID,主鍵唯一
private String id;
//姓名
private String name;
//口令
private String password;
//電子郵件
private String email;
//以下包含getXXX和setXXX方法
.......
}
UserDAO:實現(xiàn)User的數(shù)據(jù)庫訪問,這里作為一個演示,編寫測試代碼
public class UserDAO {
//存放保存的數(shù)據(jù)
private static Map dataMap = new HashMap();
//持久用戶
public boolean save(User user) {
if (dataMap.containsKey(user.getId()))
return false;
System.out.println("下面開始保存用戶");
System.out.println("id:"+user.getId());
System.out.println("password:"+user.getPassword());
System.out.println("name:"+user.getName());
System.out.println("email:"+user.getEmail());
dataMap.put(user.getId(), user);
System.out.println("用戶保存結(jié)束");
return true;
}
//查找用戶
public User find(String id) {
return (User)dataMap.get(id);
}
}
DWRUserAccess:DWR組件,提供給javascript訪問的。
public class DWRUserAccess {
UserDAO userDAO = new UserDAO();
public boolean save(User user) {
return userDAO.save(user);
}
public User find(String id) {
return userDAO.find(id);
}
}
下面說明下程序執(zhí)行的流程
1.用戶在頁面上輸入相關(guān)注冊信息,id、name、password、email,點擊“提交”按鈕
2.javascript代碼開始執(zhí)行,根據(jù)用戶填寫相關(guān)信息,通過dwr提供的DWRUserAccess.js里save的方法,調(diào)用服務(wù)器端的DWRUserAccess類save方法,將注冊信息保存。
3.通過DWRUserAccess.jsp里的find方法,調(diào)用服務(wù)器端DWRUserAccess類里的find方法,執(zhí)行用戶信息查找。
注意,在以上的執(zhí)行過程中,DWRUserAccess是供DWR調(diào)用的,是DWR組件,因此需要將DWRUserAccess類配置到dwr中。
接下來講解本次dwr測試環(huán)境的配置。
1.新建一個webapp,命名為testApp
2.將dwr.jar拷貝到testApp的WEB-INF的lib目錄下
3.編譯上面的User,UserDAO,DWRUserAccess類,放到classes目錄下
4.在web.xml中配置servlet,適配路徑到dwr目錄下,如下所示
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<display-name>DWR Servlet</display-name>
<description>Direct Web Remoter Servlet</description>
<servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>scriptCompressed</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
以上的配置可以攔截testApp下所有指向dwr的請求,關(guān)于這個攔截器,我們會在后面介紹。
5.WEB-INF下新建一個dwr.xml文件,內(nèi)容如下:
< xml version="1.0" encoding="UTF-8" >
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN" "http://www.getahead.ltd.uk/dwr/dwr10.dtd">
<dwr>
<allow>
<create creator="new" javascript="DWRUserAccess">
<param name="class" value="test.DWRUserAccess"/>
</create>
<convert converter="bean" match="test.User"/>
</allow>
</dwr>
這里我們把DWRUserAccess配置到了dwr中,create元素中,creater="new"表示每調(diào)用一次DWRUserAccess時,需要new一個這樣的類;javascript="DWRUserAccess",表示提供給前臺頁面調(diào)用的javascirpt文件是DWRUserAccess.js。
convert元素用于數(shù)據(jù)類型轉(zhuǎn)換,即java類和javascript之間相互轉(zhuǎn)換,因為和前臺交換的是User對象,因此需要對此使用bean轉(zhuǎn)換,我們將在后面介紹這個類。
4.編寫測試的HTML頁面 test.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>DWR測試</TITLE>
<meta http-equiv=Content-Type content="text/html; charset=gb2312">
<script src="/oblog312/dwr/engine.js"></script>
<script src="/oblog312/dwr/util.js"></script>
<script src="/oblog312/dwr/interface/DWRUserAccess.js"></script>
</HEAD>
<BODY>
<B>用戶注冊</B><br>
------------------------------------------------
<Br>
<form name="regForm">
登陸ID:<input type="text" name="id"><br>
口 令:<input type="password" name="password"><br>
姓 名:<input type="text" name="name"><br>
電子郵件:<input type="text" name="email"><br>
<input type="button" name="submitBtn" value="提交" onclick="OnSave()"><br>
</form>
<br>
<br><B>用戶查詢</B><br>
------------------------------------------------
<Br>
<form name="queryForm">
登陸ID:<input type="text" name="id"><br>
<input type="button" name="submitBtn" value="提交" onclick="OnFind()"><br>
</form>
<br>
</BODY>
</HTML>
<SCRIPT LANGUAGE="JavaScript">
<!--
function saveFun(data) {
if (data) {
alert("注冊成功!");
} else {
alert("登陸ID已經(jīng)存在!");
}
}
function OnSave() {
var userMap = {};
userMap.id = regForm.id.value;
userMap.password = regForm.password.value;
userMap.name = regForm.name.value;
userMap.email = regForm.email.value;
DWRUserAccess.save(userMap, saveFun);
}
function findFun(data) {
if (data == null) {
alert("無法找到用戶:"+queryForm.id.value);
return;
}
alert("找到用戶,nid:"+data.id+",npassword:"+data.password+",nname:"+data.name+",nemail:"+data.email);
}
function OnFind() {
DWRUserAccess.find(queryForm.id.value, findFun);
}
//-->
</SCRIPT>
以下對頁面的javascript進行解釋
<script src="/oblog312/dwr/engine.js"></script>
<script src="/oblog312/dwr/util.js"></script>
這兩個是dwr提供的,用戶可以不必關(guān)心,只需要導(dǎo)入即可
<script src="/oblog312/dwr/interface/DWRUserAccess.js"></script>
是我們編寫的DWRUserAccess類,經(jīng)dwr反射后,生成的javascript代碼,它和DWRUserAccess.java是對應(yīng)的,供用戶調(diào)用,實際上我們就是通過這個js文件去調(diào)用服務(wù)器端的DWRUserAccess類的。
<SCRIPT LANGUAGE="JavaScript">
<!--
function saveFun(data) {
if (data) {
alert("注冊成功!");
} else {
alert("用戶名已經(jīng)存在!");
}
}
function OnSave() {
var userMap = {};
userMap.id = regForm.id.value;
userMap.password = regForm.password.value;
userMap.name = regForm.name.value;
userMap.email = regForm.email.value;
DWRUserAccess.save(userMap, saveFun);
}
function findFun(data) {
if (data == null) {
alert("無法找到用戶:"+queryForm.id.value);
return;
}
alert("找到用戶,nid:"+data.id+",npassword:"+data.password+",nname:"+data.name+",nemail:"+data.email);
}
function OnFind() {
DWRUserAccess.find(queryForm.id.value, findFun);
}
//-->
</SCRIPT>
這段javascirpt代碼,我們來看下OnSave函數(shù),首先它構(gòu)造一個map,將表單數(shù)據(jù)都設(shè)置到map中,然后調(diào)用DWRUserAccess.save(userMap, saveFun),執(zhí)行save操作。大家可以注意到,服務(wù)器端的DWRUserAccess中的save方法是這樣的:boolean save(User user),其參數(shù)是一個User對象,返回一個boolean值;而客戶端的方法是這樣的:save(userMap,saveFun),第一個參數(shù)userMap是javascirpt中的map對象,在這里相當(dāng)于服務(wù)器端的User對象(在服務(wù)器端執(zhí)行時,會通過convert轉(zhuǎn)換成User對象),前面我們提到dwr是利用回調(diào)函數(shù)來返回執(zhí)行結(jié)果的,第二個參數(shù)saveFun即是一個回調(diào)函數(shù)。在函數(shù)function saveFun(data)中,data是執(zhí)行結(jié)果,這里是一個bool值,非常簡單的,我們通過判斷data是否為真,可以知道用戶名是否重復(fù),用戶是否注冊成功。
看一下OnFind查找函數(shù),執(zhí)行結(jié)果在回調(diào)函數(shù)findFun(data)中,因為服務(wù)器端返回的是一個User對象,通過convert,將會轉(zhuǎn)換成javascript的一個map對象,
于是在findFun中,通過data.id、data.name、data.password、data.email我們可以輕松的訪問到這個User對象。
好了配置完畢,啟動服務(wù)器,在目錄中打入localhost/testApp/test.html。
1.在“用戶注冊”表單中,id框中輸入admin,password中輸入123456,name中輸入chenbug,email中輸入chenbug@zj.com,點擊提交按鈕,彈出對話框:“注冊成功”,在服務(wù)器后臺可以看到信息如下:
下面開始保存用戶
id:admin
password:123456
name:chenbug
email:chenbug@zj.com
用戶保存結(jié)束
再次點擊提交按鈕,彈出對話框“登陸ID已經(jīng)存在”。
2.在“用戶查詢”對話框中,輸入登陸ID為admin,點擊提交按鈕,提示找到用戶,并顯示相關(guān)信息,輸入admin123,點擊提交按鈕,提示無法找到用戶。
至此,測試結(jié)束。
后續(xù):
1。攔截器 uk.ltd.getahead.dwr.DWRServlet
該類攔截所有指向dwr目錄下的請求,并調(diào)用Processor的handler方法進行處理,在uk.ltd.getahead.dwr.impl.DefaultProcessor下,我們可以看到詳細的處理過程。
if (pathInfo.length() == 0 ||
pathInfo.equals(HtmlConstants.PATH_ROOT) ||
pathInfo.equals(req.getContextPath()))
{
resp.sendRedirect(req.getContextPath() + servletPath + HtmlConstants.FILE_INDEX);
}
else if (pathInfo.startsWith(HtmlConstants.FILE_INDEX))
{
index.handle(req, resp);
}
else if (pathInfo.startsWith(HtmlConstants.PATH_TEST))
{
test.handle(req, resp);
}
else if (pathInfo.startsWith(HtmlConstants.PATH_INTERFACE))
{
iface.handle(req, resp);
}
else if (pathInfo.startsWith(HtmlConstants.PATH_EXEC))
{
exec.handle(req, resp);
}
else if (pathInfo.equalsIgnoreCase(HtmlConstants.FILE_ENGINE))
{
file.doFile(req, resp, HtmlConstants.FILE_ENGINE, HtmlConstants.MIME_JS);
}
else if (pathInfo.equalsIgnoreCase(HtmlConstants.FILE_UTIL))
{
file.doFile(req, resp, HtmlConstants.FILE_UTIL, HtmlConstants.MIME_JS);
}
else if (pathInfo.equalsIgnoreCase(HtmlConstants.FILE_DEPRECATED))
{
file.doFile(req, resp, HtmlConstants.FILE_DEPRECATED, HtmlConstants.MIME_JS);
}
else
{
log.warn("Page not found (" + pathInfo + "). In debug/test mode try viewing /[WEB-APP]/dwr/"); //$NON-NLS-1$ //$NON-NLS-2$
resp.sendError(HttpServletResponse.SC_NOT_FOUND);
}
通過判斷request請求的servlet路徑,進行處理,大家可以自己去參看,這里不詳細討論。
2.bean轉(zhuǎn)換器,<convert converter="bean" match="test.User"/>
將dwr.jar解壓縮,在路徑ukltdgetaheaddwr下可以看到dwr.xml,這里配置了系統(tǒng)默認的一些轉(zhuǎn)換器,
<converter id="bean" class="uk.ltd.getahead.dwr.convert.BeanConverter"/>即是剛才用到User類的轉(zhuǎn)換器,進入代碼我們來看看它是如何在javascript和java間進行轉(zhuǎn)換的。
打開BeanConverter代碼,定位到函數(shù)
public Object convertInbound(Class paramType, InboundVariable iv, InboundContext inctx) throws ConversionException
即是將javascript對象轉(zhuǎn)換成java對象的,其中
paramType即Class類型,在上面的例子中是test.User,
InboundVariable iv,是傳入的值,通過iv.getValue可以得到傳入的javascript值串
InboundContext inctx,是入口參數(shù)上下文,用于保存轉(zhuǎn)換的后java對象。
因為前臺傳入的是一個javascript的map類型,而map肯定是以{開始和以}結(jié)束的,于是在這個函數(shù)一開始進行了判斷
if (!value.startsWith(ConversionConstants.INBOUND_MAP_START))
{
throw new IllegalArgumentException(Messages.getString("BeanConverter.MissingOpener", ConversionConstants.INBOUND_MAP_START)); //$NON-NLS-1$
}
if (!value.endsWith(ConversionConstants.INBOUND_MAP_END))
{
throw new IllegalArgumentException(Messages.getString("BeanConverter.MissingCloser", ConversionConstants.INBOUND_MAP_START)); //$NON-NLS-1$
}
javascript中,map里各個項是用逗號連接的,如var userMap = {id:'admin',password:'123456',name:'chenbug',email:'chenbug@zj.com'};而每個項的鍵值對是用冒號連接的,
在convertInbound函數(shù)的接下來的處理中,即是通過分析map字串,通過paramType構(gòu)造java實例(即User類),然后通過反射,將這些鍵值對設(shè)置到j(luò)ava實例中,并返回。
這樣就完成了javascript到j(luò)ava的轉(zhuǎn)換。
另一個函數(shù)
public String convertOutbound(Object data, String varname, OutboundContext outctx) throws ConversionException
即是將java對象轉(zhuǎn)換為javascript對象(其實是聲明和賦值語句)。
Object data ,是待轉(zhuǎn)換的java對象
String varname,是javascript中的該對象的變量名
OutboundContext outctx,傳出參數(shù)上下文,用于保存轉(zhuǎn)換后的javascript值
StringBuffer buffer = new StringBuffer();
buffer.append("var "); //$NON-NLS-1$
buffer.append(varname);
buffer.append("={};"); //$NON-NLS-1$
這里聲明了map類型的變量。
即下來來的代碼即是通過反射進行變量賦值,如下
buffer.append(varname);
buffer.append('.');
buffer.append(name);
buffer.append('=');
buffer.append(nested.getAssignCode());
buffer.append(';');
大家可以自己去參看更多的代碼。
3.dwr本身提供了一個測試環(huán)境,大家在配置完后,可以在IE中輸入地址http://localhost/testApp/dwr/index.html,看到配置的各DWR組件,并進行相關(guān)測試。
posted @
2008-01-18 14:43 Super·shen BLOG 閱讀(41042) |
評論 (27) |
編輯 收藏
1. dwr - direct web remote
2. 推技術(shù)
3. http 長連接
4. Comet ---- HTTP長連接的“服務(wù)器推”技術(shù)
5. Jetty服務(wù)器 ---- Jetty 6 Web 服務(wù)器針對 AJAX、Comet 應(yīng)用的特點進行了很多創(chuàng)新的改進,請參考文章“AJAX,Comet and Jetty”(請參見 參考資源)。
http://wiki.javascud.org/display/dwrcn/Home
http://wiki.springside.org.cn/display/springside/DWR
http://blog.csdn.net/octverve/archive/2007/09/26/1801826.aspx
posted @
2008-01-15 10:07 Super·shen BLOG 閱讀(310) |
評論 (0) |
編輯 收藏
學(xué)習(xí)共進!
MyEclipse 5.5 開發(fā) Struts 1.2 簡單登錄的入門視頻(有聲+源碼) 2007-09-19 01:50
視頻講解: Netbeans 5.5 配置顯示中文 JavaDoc
入門視頻: 使用 MyEclipse 開發(fā) Swing 應(yīng)用
河南話講解 MyEclipse + Tomcat Servlet 開發(fā)入門視頻
MyEclipse + JPA + Toplink 開發(fā)視頻: 開發(fā)并運行第一個 JPA 項目
MyEclipse + JBoss 開發(fā)視頻: 配置,開發(fā)并運行第一個 EJB 3 項目
JDBC 入門視頻: 配置 SQL Explorer 插件, ODBC 數(shù)據(jù)源, 建表, 用 JDBC 讀取數(shù)據(jù)庫
Tomcat 入門視頻: 下載, 運行, 第一個 HelloWorld
Eclipse 入門視頻: 下載, 運行, 第一個 HelloWorld
Java 入門視頻: 下載, 安裝 JDK, 配置環(huán)境變量, HelloWorld
推薦給初學(xué)者的 Java 視頻
Netbeans 6.0 M10 開發(fā) UML 項目的入門視頻
MyEclipse UML 入門視頻2 - 根據(jù)代碼反向工程生成 UML
MyEclipse UML 入門視頻
MyEclipse + Tomcat 開發(fā)視頻: 下載,安裝,配置,開發(fā)并運行Web項目
小電影: 用 MyEclipse 開發(fā) Spring + Struts + Hibernate 的總結(jié)與操作視頻(9分鐘)
小電影: 用 MyEclipse 開發(fā) Spring + Struts 的總結(jié)與操作視頻(7分鐘)
用MyEclipse 4 分鐘開發(fā)Spring整合Hibernate應(yīng)用的視頻
在 Linux 上配置并運行 Tomcat 服務(wù)器(入門整理)(視頻)
Java 初學(xué)者入門視頻: 下載 JDK 和 Netbeans
Eclipse 配置顯示中文 javadoc 的視頻
Hibernate 英文 PPT 及 MyEclipse 操作視頻整理
推薦一點 MyEclipse 的官方Spring,Hibernate入門視頻教程
Netbeans 5.5 + JPA + Hibernate 3 + Tomcat 實例有聲視頻
推薦一些AJAX視頻和文章
夏昕 <<Spring 開發(fā)指南入門>>1 分鐘上手教程視頻(不帶解說)
AJAX 入門培訓(xùn) PPT 及示例代碼
Java EE 5 入門 PPT 講解有聲視頻 - 第二部分
Java EE 5 入門 PPT 講解有聲視頻 - 第一部分
Java EE 5 入門視頻 - 在 JSF 中使用 JPA
Jigloo 開發(fā) Swing 的入門教程
視頻:使用 Netbeans 5.5可視化開發(fā) JSF 的簡單注冊流程
Java EE 5 入門視頻 - 在 J2SE 中使用 JPA
Navicat管理Mysql 的視頻
Weblogic 9 之旅圖文視頻 2 - Portal 開發(fā)環(huán)境設(shè)置, 簡單的Portal 開發(fā)(視頻已貼上)
用 JProfiler4 調(diào)優(yōu) Weblogic 和 Tomcat 的視頻(原創(chuàng))
來自 http://m.tkk7.com/beansoft
posted @
2008-01-08 10:05 Super·shen BLOG 閱讀(693) |
評論 (0) |
編輯 收藏
String command = "cmd /c C:/Program Files/MySQL/MySQL Server 5.0/bin>mysqldump -h localhost -u root -p aijia > E:/aijia.dmp";
try {
Process process = Runtime.getRuntime().exec(command);
InputStreamReader ir = new InputStreamReader(process
.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
String line;
while ((line = input.readLine()) != null)
System.out.println(line);
input.close();
} catch (IOException e) {
e.printStackTrace();
}
另外
首先,設(shè)置mysql的環(huán)境變量(在path中添加%MYSQL_HOME%\bin),重啟電腦。
完整代碼:
/**
* @param args
*/
public static void main(String[] args) {
/*
* 備份和導(dǎo)入是一個互逆的過程。
* 備份:程序調(diào)用mysql的備份命令,讀出控制臺輸入流信息,寫入.sql文件;
* 導(dǎo)入:程序調(diào)用mysql的導(dǎo)入命令,把從.sql文件中讀出的信息寫入控制臺的輸出流
* 注意:此時定向符">"和"<"是不能用的
*/
backup();
load();
}
/**
* 備份檢驗一個sql文件是否可以做導(dǎo)入文件用的一個判斷方法:把該sql文件分別用記事本和ultra
* edit打開,如果看到的中文均正常沒有亂碼,則可以用來做導(dǎo)入的源文件(不管sql文件的編碼格式如何,也不管db的編碼格式如何)
*/
public static void backup() {
try {
Runtime rt = Runtime.getRuntime();
// 調(diào)用 mysql 的 cmd:
Process child = rt
.exec("mysqldump -u root --set-charset=utf8 bjse act_obj");// 設(shè)置導(dǎo)出編碼為utf8。這里必須是utf8
// 把進程執(zhí)行中的控制臺輸出信息寫入.sql文件,即生成了備份文件。注:如果不對控制臺信息進行讀出,則會導(dǎo)致進程堵塞無法運行
InputStream in = child.getInputStream();// 控制臺的輸出信息作為輸入流
InputStreamReader xx = new InputStreamReader(in, "utf8");// 設(shè)置輸出流編碼為utf8。這里必須是utf8,否則從流中讀入的是亂碼
String inStr;
StringBuffer sb = new StringBuffer("");
String outStr;
// 組合控制臺輸出信息字符串
BufferedReader br = new BufferedReader(xx);
while ((inStr = br.readLine()) != null) {
sb.append(inStr + "\r\n");
}
outStr = sb.toString();
// 要用來做導(dǎo)入用的sql目標(biāo)文件:
FileOutputStream fout = new FileOutputStream(
"e:/mysql-5.0.27-win32/bin/bjse22.sql");
OutputStreamWriter writer = new OutputStreamWriter(fout, "utf8");
writer.write(outStr);
// 注:這里如果用緩沖方式寫入文件的話,會導(dǎo)致中文亂碼,用flush()方法則可以避免
writer.flush();
// 別忘記關(guān)閉輸入輸出流
in.close();
xx.close();
br.close();
writer.close();
fout.close();
System.out.println("/* Output OK! */");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 導(dǎo)入
*
*/
public static void load() {
try {
String fPath = "e:/mysql-5.0.27-win32/bin/bjse22.sql";
Runtime rt = Runtime.getRuntime();
// 調(diào)用 mysql 的 cmd:
Process child = rt.exec("mysql -u root bjse ");
OutputStream out = child.getOutputStream();//控制臺的輸入信息作為輸出流
String inStr;
StringBuffer sb = new StringBuffer("");
String outStr;
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(fPath), "utf8"));
while ((inStr = br.readLine()) != null) {
sb.append(inStr + "\r\n");
}
outStr = sb.toString();
OutputStreamWriter writer = new OutputStreamWriter(out, "utf8");
writer.write(outStr);
// 注:這里如果用緩沖方式寫入文件的話,會導(dǎo)致中文亂碼,用flush()方法則可以避免
writer.flush();
// 別忘記關(guān)閉輸入輸出流
out.close();
br.close();
writer.close();
System.out.println("/* Load OK! */");
} catch (Exception e) {
e.printStackTrace();
}
}
posted @
2007-12-07 13:25 Super·shen BLOG 閱讀(5483) |
評論 (1) |
編輯 收藏
用Flex/Central/Java上傳文件
in java:
import com.oreilly.servlet.MultipartRequest;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
public class UploadServlet extends HttpServlet {
protected void doGet( HttpServletRequest req, HttpServletResponse res ) throws ServletException, IOException {;}
protected void doPost( HttpServletRequest req, HttpServletResponse res ) throws ServletException, IOException {
MultipartRequest parts = new MultipartRequest( req, "C:\\MyUploadPath" );
PrintWriter out = res.getWriter();
out.print( "SUCCESS" );
out.close();
}
}
in mxml:
< mx:Application initialize="initApp( event )" xmlns:mx="http://www.macromedia.com/2003/mxml">
< mx:Button id="btnUpload" label="Upload..." click="doUpload( event )" />
< mx:Image id="imgUpload" width="100%" height="100%" horizontalAlign="center" />
</mx:Application>
as:
private function doUpload( event:Object ):Void {
var file:FileReference = new FileReference();
// Ask the user to choose a file to upload
if( file.browse( ["JPEG Files", "*.jpg"] ) ) {
file.addListener( this );
file.upload( "http://myurl/servlet/MyUploadServlet" );
}
}
private function onUploadSuccess( ref:FileReference, response:String ):Void {
imgUpload.source = "http://myurl/myfilepath/" + ref.name;
}
private function onUploadFailed( ref:FileReference, error:String, response:String ):Void {
mx.controls.Alert.show( "Upload error: " + error );
}
servlet獲取絕對路徑方法:
ServletConfig config = this.getServletConfig();
ServletContext context = getServletContext();
String path = context.getRealPath("");
posted @
2007-12-03 14:50 Super·shen BLOG 閱讀(1928) |
評論 (1) |
編輯 收藏
用flex做即時通訊,收到的最新消息應(yīng)該在最下面,但textArea的滾動條默認在最上方,不方便查看最新消息。
可以使用 maxVerticalScrollPosition屬性可以獲取最下方的值,非常方便
另外提供類:
package Util
{
import mx.controls.TextArea;
public class ChatTextarea extends TextArea
{
public function ChatTextarea()
{
super();
}
override public function set htmlText( value:String ):void
{
super.htmlText = value;
this.validateNow();
if( textField ) verticalScrollPosition = textField.maxScrollV
}
override public function set text( value:String ):void
{
super.htmlText = value;
this.validateNow();
if( textField ) verticalScrollPosition = textField.maxScrollV;
}
}
}
posted @
2007-11-22 16:34 Super·shen BLOG 閱讀(3123) |
評論 (0) |
編輯 收藏
問題:
然后運行某Flex程序時,出現(xiàn)如下提示:
This content requires the Adobe Flash Player. Get Flash
但是,鏈接到Adobe的Flash Player下載網(wǎng)頁,重裝了N次也不行。
google到下面的信息:
"...Flex Builder 2 裏面附的 debugger player 版次比正式版的 Flash Player 9 舊一點,有時會導(dǎo)致網(wǎng)頁的自動偵測失靈(誤以為user沒裝 flash player),所以現(xiàn)在 adobe 網(wǎng)頁上已經(jīng)有新版的 debugger player ,可以先試試裝那個版本看看。"
實際解決方法:
因為本機已安裝了Flex bulider 3 , 既然知道問題所在,就不用再去down了,找到...\Flex bulider 3\Players\目錄,運行Install Flash Player 9 ActiveX.msi,選擇Repair,出錯失敗,再次選擇Remove卸載,重新安裝此ActiveX,瀏覽器中刷新mxml頁面,OK,搞定,收工。
posted @
2007-11-22 13:30 Super·shen BLOG 閱讀(1411) |
評論 (0) |
編輯 收藏
已經(jīng)很久沒摸過FLASH了,由于要接一個項目需要用的flash實現(xiàn)。 當(dāng)我使用flash cs3 寫程序時發(fā)現(xiàn)已經(jīng)和以前的大不一樣了!多年沒接觸本來還想在友人面前顯耀一下寶刀未老,天哪,好多地方不一樣了,剛接觸還真不習(xí)慣,還出丑了。
flash cs3 的改變源自于 as3的重大改變。更源于flash player AM2的重大改變。
as3功能很強大,語言和java類型,也是面向?qū)ο蟮模彩鞘褂锰摂M機解釋。 (虛擬機這個概念太強了?,F(xiàn)在的主流程序都是用類似虛擬機技術(shù),JAVA .NET FLEX)
實現(xiàn)技術(shù)原理 flash 和 java 曾經(jīng)紅火一時的applet 幾乎差不多了。但是flash更為簡單易用,易于入門!
posted @
2007-11-22 10:49 Super·shen BLOG 閱讀(314) |
評論 (0) |
編輯 收藏
摘要: 如何解決端口沖突導(dǎo)致tomcat無法啟動的問題
Tomcat在啟動時主要使用下面的3個端口
<Server port="8005" shutdown="SHUTDOWN" debug="0">
<Connector className="org.apache.coyote.tomcat4.CoyoteConnector" port="8080" minProcessors=... 閱讀全文
posted @
2007-11-22 10:00 Super·shen BLOG 閱讀(16622) |
評論 (2) |
編輯 收藏
自己在網(wǎng)上找了半天沒找到只有 “時分秒”的控件, 就自己做了個,發(fā)在這里方便有人用到

鼠標(biāo)點擊 后 的效果
SetTime.js
/**//***********************************
* 簡單時間控件: version 1.0
* 作者:李祿燊
* 時間:2007-10-31
*
* 使用說明:
* 首先把本控件包含到頁面
* <script src="XXX/setTime.js" type="text/javascript"></script>
* 控件調(diào)用函數(shù):_SetTime(field)
* 例如 <input name="time" type="text" onclick="_SetTime(this)"/>
*
************************************/
var str = "";
document.writeln("<div id=\"_contents\" style=\"padding:6px; background-color:#E3E3E3; font-size: 12px; border: 1px solid #777777; position:absolute; left:?px; top:?px; width:?px; height:?px; z-index:1; visibility:hidden\">");
str += "\u65f6<select name=\"_hour\">";
for (h = 0; h <= 9; h++) {
str += "<option value=\"0" + h + "\">0" + h + "</option>";
}
for (h = 10; h <= 23; h++) {
str += "<option value=\"" + h + "\">" + h + "</option>";
}
str += "</select> \u5206<select name=\"_minute\">";
for (m = 0; m <= 9; m++) {
str += "<option value=\"0" + m + "\">0" + m + "</option>";
}
for (m = 10; m <= 59; m++) {
str += "<option value=\"" + m + "\">" + m + "</option>";
}
str += "</select> \u79d2<select name=\"_second\">";
for (s = 0; s <= 9; s++) {
str += "<option value=\"0" + s + "\">0" + s + "</option>";
}
for (s = 10; s <= 59; s++) {
str += "<option value=\"" + s + "\">" + s + "</option>";
}
str += "</select> <input name=\"queding\" type=\"button\" onclick=\"_select()\" value=\"\u786e\u5b9a\" style=\"font-size:12px\" /></div>";
document.writeln(str);
var _fieldname;
function _SetTime(tt) {
_fieldname = tt;
var ttop = tt.offsetTop; //TT控件的定位點高
var thei = tt.clientHeight; //TT控件本身的高
var tleft = tt.offsetLeft; //TT控件的定位點寬
while (tt = tt.offsetParent) {
ttop += tt.offsetTop;
tleft += tt.offsetLeft;
}
document.all._contents.style.top = ttop + thei + 4;
document.all._contents.style.left = tleft;
document.all._contents.style.visibility = "visible";
}
function _select() {
_fieldname.value = document.all._hour.value + ":" + document.all._minute.value + ":" + document.all._second.value;
document.all._contents.style.visibility = "hidden";
}
posted @
2007-11-01 15:33 Super·shen BLOG 閱讀(11141) |
評論 (13) |
編輯 收藏
下邊的所有都是自己對JAVA的理解不知道對不對,有待于以后實踐驗證
用JAVA編程,無論是什么框架,什么庫,什么插件, 他們的也還是來自 最基本java類編程。
比如,我猜想 TOMCAT服務(wù)器,也是由一個帶MAIN方法的類來啟動的, 然后開通一個端口服務(wù)器,它的原理應(yīng)該和java socket server編程應(yīng)該是一個道理。主要是啟動一些類,來接受客戶端的請求(容器的原理應(yīng)該是這樣吧)
再說 servlet 也是基本的 JAVA類, 他們是受容器的管理,受到的是容器的調(diào)用(容器應(yīng)該也就是帶main的java類),并對客戶端產(chǎn)生相應(yīng)。
還有像 jsp 的所謂表現(xiàn)層框架,最終也是調(diào)用到帶main函數(shù)的java類。 它的原理是 jsp編譯生產(chǎn) servlet ,servlet 最終還是依靠容器。
其實我想,自己也可以開發(fā)一個表現(xiàn)層,只要能實時編譯成servlet,就能和jsp一樣的功能了。
不過jsp是sun公司的標(biāo)準(zhǔn)產(chǎn)品,它的庫已經(jīng)集合在大多數(shù)容器上了,得到廣大容器的廣泛支持,出來得也早,用人也多,很少人想到要做新的表現(xiàn)層(如果沒什么好功能,就等于重復(fù)發(fā)明車輪)。
FreeMarker 的原理也是一樣,簡單說它就是一個庫,你可以把它集合到容器里,得到容器的支持后,用FreeMarker 編寫的表現(xiàn)層就能實時編譯成servlet。 最后得到的和jsp 得到的是一個效果。
FreeMarker 最終的結(jié)果還是容器調(diào)用。
不過要做一個FreeMarker 可不簡單,要有好的想法,要不就和jsp功能重復(fù)了,沒價值。
以上都是個人想出來的 不知道是否正確 有待于個人深入學(xué)習(xí)。
posted @
2007-09-17 11:46 Super·shen BLOG 閱讀(4467) |
評論 (5) |
編輯 收藏
java 中文亂碼處理。
參考
http://china.eceel.com/article/study_for_character_encoding_java.htm
http://upurban.com/bbs/viewtopic.php?t=246
1。什么是utf-8,什么是ISO-8859-1,什么是GB2312,還有什么是unicode
2。java 程序的字符的表示格式
3。jsp 程序中文顯示處理實例
3。1
<%@ page pageEncoding="ISO-8859-1"%>和<%@ page pageEncoding="GB2312"%>和<%@ page
pageEncoding="UTF-8"%>各自的意思是什么,他們是否只對post提交有效!
request.setCharacterEncoding("UTF-8")是什么意思?有什么區(qū)別?還有
response.setCharacterEncoding("UTF-8"),優(yōu)先于下邊
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
setCharacterEncoding()該函數(shù)用來設(shè)置http請求或者相應(yīng)的編碼。
對于request,是指提交內(nèi)容的編碼,指定后可以通過getParameter()則直接獲得正確的字符串,如果不
指定,則默認使用iso8859-1編碼,需要進一步處理。參見下述"表單輸入"。值得注意的是在執(zhí)行
setCharacterEncoding()之前,不能執(zhí)行任何getParameter()。java doc上說明:This method must be
called prior to reading request parameters or reading input using getReader()。而且,該指定
只對POST方法有效,對GET方法無效。分析原因,應(yīng)該是在執(zhí)行第一個getParameter()的時候,java將會
按照編碼分析所有的提交內(nèi)容,而后續(xù)的getParameter()不再進行分析,所以setCharacterEncoding()無
效。而對于GET方法提交表單是,提交的內(nèi)容在URL中,一開始就已經(jīng)按照編碼分析所有的提交內(nèi)容,
setCharacterEncoding()自然就無效。
對于response,則是指定輸出內(nèi)容的編碼,同時,該設(shè)置會傳遞給瀏覽器,告訴瀏覽器輸出內(nèi)容所采用的
編碼。
3.2. jsp輸出
指定文件輸出到browser是使用的編碼,該設(shè)置也應(yīng)該置于文件的開頭。例如:<%@ page
contentType="text/html; charset= GBK" %>。該設(shè)置和response.setCharacterEncoding("GBK")等效。
4。java EE程序利用過濾器 處理中文問實例
提交數(shù)據(jù)的編碼格式
tomcat默認提交格式是ISO-8859-1
可以通過設(shè)置過濾器(只針對post提交)或修改server.xml 的URIencoding 編碼格式(只針對get提交)
達到你想要的 數(shù)據(jù)提交編碼格式。
總結(jié)
---by mylu 18:26 2007-5-20
posted @
2007-05-20 22:36 Super·shen BLOG 閱讀(1524) |
評論 (0) |
編輯 收藏
ORM
Object Relation Mapping
對象 關(guān)系 映射
對象 指實體域?qū)ο?br />關(guān)系 關(guān)系數(shù)據(jù)
模型
概念模型(實體-屬性)
關(guān)系數(shù)據(jù)模型(關(guān)系數(shù)據(jù)庫)
域模型(對象)
軟件分層
v - 表述層
c /
??? /業(yè)務(wù)層
m- 持久層(hibernate 技術(shù)實現(xiàn))
??? \數(shù)據(jù)層
mvc 對應(yīng) 各層次
概念實體關(guān)系
1對1
1對多
多對多
表與表之間的關(guān)系 參照完整性
外鍵
多對多
多對一
域?qū)ο笾g的 關(guān)系
關(guān)聯(lián) (一對一 一對多 多對多)
依賴 (一個類需要訪問另外一個類)
聚集 (一個類的對象是另一個類的一部分, 人和手)
一般化 (繼承關(guān)系)
域?qū)ο?br />?實體域?qū)ο? (實體EJB,POJO)
過程域?qū)ο? (會話EJB,消息驅(qū)動EJB,POJO)
事件域?qū)ο? ()
在hibernate中 一般只關(guān)注 實體域?qū)ο?和 過程域?qū)ο?/p>
域?qū)ο蟮年P(guān)系
?域?qū)ο蟮年P(guān)聯(lián)關(guān)系 是有方向的
體現(xiàn)在類的編碼不一樣的
單向關(guān)聯(lián)
雙向關(guān)聯(lián)
?
域?qū)ο蟮某志没?br />把對象從內(nèi)存中 保存到持久化設(shè)備中去
ORM 與? ORM模式
ORM模式是一種持久化技術(shù),還有其他模式的持久化技術(shù)。如主動域模式(BMP),JDO模式,CMP模式。
域模型和數(shù)據(jù)模型的各個不匹配之處
1,繼承
2,多對多
3,雙向
4。粒度
盡量少連接查詢,很消耗時間的操作
?
創(chuàng)建持久化類
1。持久化類符合javabean的規(guī)范,包含一些屬性 以及對應(yīng)的getxxx 色天下學(xué)習(xí)方法
2。持久化類有一個id屬性,用來唯一表示類的每一個對象。 也叫OID 對象表示符
3。Hibernate要求持久化類必須提供一個不帶參數(shù)的默認構(gòu)造方法
創(chuàng)建數(shù)據(jù)庫schema
創(chuàng)建對象-關(guān)系映射文件
(一般在eslispe中先創(chuàng)建數(shù)據(jù)庫 然后再創(chuàng)建持久化類以及映射文件)
hibernate 映射類型
hibernate的初始化
static{
try{
//根據(jù)默認位置的hibernate配置文件創(chuàng)建 configuration實例
Configuration config = new Configuration();
config.addClass(Customer.class);
//創(chuàng)建SessionFactory 實例
sessionFactory = config.buildSessinoFactroy();
}catch(Exception e){e.printStackTrace();}
}
SessionFactory 接口
一個SessionFactory 實例是對應(yīng)一個數(shù)據(jù)源的,應(yīng)用從SessionFactory 獲取session實例對象
1線程安全的
2重量級的,不能隨意創(chuàng)建和銷毀她的實例。
Session 接口
1 Session接口是hibernate應(yīng)用最為廣泛的接口。
2 Session也被稱為持久化管理器,它提供和持久化相關(guān)的操作
3 Session有以下特點
?a 不是線程安全的 所以應(yīng)避免多線程共用一個Session實例
?b Session實例是輕量級的,所謂輕量級是指他的創(chuàng)建和銷毀不需要消耗太多的資源。意味著程序中可以經(jīng)常創(chuàng)建和銷毀Session實例,保證不多線程使用Session對象。
Session接口的常用方法:
save()
update()
delete()
load()
Session執(zhí)行事務(wù)流程
Session session = factory.openSession();
Transaction tx;
try{
tx = session.beginTranscation();
//執(zhí)行事務(wù)
...
//提交事務(wù)
tx.commit();
}
catche(Exception e)
{//如果出現(xiàn)異常,撤消事務(wù)
if(tx!=null)tx.rollback();
throw e;
}
finally{
session.close(); //不管事務(wù)是否成功,最后都要關(guān)閉session對象
}
}
?
?
?
?
posted @
2007-02-07 14:32 Super·shen BLOG 閱讀(412) |
評論 (0) |
編輯 收藏
eXtremeComponents FAQ(中文版)
本文檔允許在遵守以下兩條原則的條件下被使用和傳播: 1)不能憑借本文檔索取任何費用 2)以任何方式(印刷物或電子版)使用和傳播時本文檔時,必須包含本版權(quán)申明
eXtremeComponents FAQ(中文)
Q: 如何使用導(dǎo)出功能
A: 為了使用導(dǎo)出功能,只需要在web.xml文件中加入eXtremeComponents的導(dǎo)出過濾器的配置,內(nèi)容如下:
<filter>
<filter-name>eXtremeExport</filter-name>
<filter-class>org.extremecomponents.table.filter.ExportFilter</filter-class>
<init-param>
<param-name>responseHeadersSetBeforeDoFilter</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>eXtremeExport</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Q: 傳入中文參數(shù)亂碼,如下頁面:
<form id="form1" name="form1" method="post" action="應(yīng)用eXtremeTable的action或是結(jié)果頁面名">
<select name="selecttype" size="6">
<option value="第一個">第一個</option>
<option value="第二個">第二個</option>
<option value="第三個">第三個</option>
</select>
<input type="text" name="username" />
<input type="submit" name="Submit" value="提交" />
</form>
當(dāng)你提交時含有eXtremeTable的結(jié)果頁面會自動取得頁面上的表單參數(shù),那怕是經(jīng)過了action的mapping.findForward("forward"),在我的試用過程中到頁面上會出現(xiàn)傳遞過去的參數(shù),但出現(xiàn)了亂碼問題,使用查詢(filter)功能是的中文參數(shù)問題類似。
A:
-
確認服務(wù)器的參數(shù)是否設(shè)置了正確的編碼,如果使用Tomcat請確認Server.xml:
<Connector port="80" URIEncoding="UTF-8" maxThreads="150" minSpareThreads="25" maxSpareThreads="75" enableLookups="false"
redirectPort="8443" acceptCount="100" debug="0" connectionTimeout="20000" disableUploadTimeout="true" />
-
添加編碼過濾器到你的應(yīng)用工程:
/*
* Copyright 1999-2001,2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package filters;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.UnavailableException;
/**
* <p>Example filter that sets the character encoding to be used in parsing the
* incoming request, either unconditionally or only if the client did not
* specify a character encoding. Configuration of this filter is based on
* the following initialization parameters:</p>
* <ul>
* <li><strong>encoding</strong> - The character encoding to be configured
* for this request, either conditionally or unconditionally based on
* the <code>ignore</code> initialization parameter. This parameter
* is required, so there is no default.</li>
* <li><strong>ignore</strong> - If set to "true", any character encoding
* specified by the client is ignored, and the value returned by the
* <code>selectEncoding()</code> method is set. If set to "false,
* <code>selectEncoding()</code> is called <strong>only</strong> if the
* client has not already specified an encoding. By default, this
* parameter is set to "true".</li>
* </ul>
*
* <p>Although this filter can be used unchanged, it is also easy to
* subclass it and make the <code>selectEncoding()</code> method more
* intelligent about what encoding to choose, based on characteristics of
* the incoming request (such as the values of the <code>Accept-Language</code>
* and <code>User-Agent</code> headers, or a value stashed in the current
* user's session.</p>
*
* @author Craig McClanahan
* @version $Revision: 1.3 $ $Date: 2004/02/28 03:35:22 $
*/
public class SetCharacterEncodingFilter implements Filter {
// ----------------------------------------------------- Instance Variables
/**
* The default character encoding to set for requests that pass through
* this filter.
*/
protected String encoding = null;
/**
* The filter configuration object we are associated with. If this value
* is null, this filter instance is not currently configured.
*/
protected FilterConfig filterConfig = null;
/**
* Should a character encoding specified by the client be ignored?
*/
protected boolean ignore = true;
// --------------------------------------------------------- Public Methods
/**
* Take this filter out of service.
*/
public void destroy() {
this.encoding = null;
this.filterConfig = null;
}
/**
* Select and set (if specified) the character encoding to be used to
* interpret request parameters for this request.
*
* @param request The servlet request we are processing
* @param result The servlet response we are creating
* @param chain The filter chain we are processing
*
* @exception IOException if an input/output error occurs
* @exception ServletException if a servlet error occurs
*/
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
// Conditionally select and set the character encoding to be used
if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null)
request.setCharacterEncoding(encoding);
}
// Pass control on to the next filter
chain.doFilter(request, response);
}
/**
* Place this filter into service.
*
* @param filterConfig The filter configuration object
*/
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
String value = filterConfig.getInitParameter("ignore");
if (value == null)
this.ignore = true;
else if (value.equalsIgnoreCase("true"))
this.ignore = true;
else if (value.equalsIgnoreCase("yes"))
this.ignore = true;
else
this.ignore = false;
}
// ------------------------------------------------------ Protected Methods
/**
* Select an appropriate character encoding to be used, based on the
* characteristics of the current request and/or filter initialization
* parameters. If no character encoding should be set, return
* <code>null</code>.
* <p>
* The default implementation unconditionally returns the value configured
* by the <strong>encoding</strong> initialization parameter for this
* filter.
*
* @param request The servlet request we are processing
*/
protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}
}
-
在web.xml中添加編碼過濾器配置:
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>filters.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>gb2312</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Q:關(guān)于導(dǎo)出時中文文件名為亂碼的問題
A: 這是個bug,建議使用英文文件名,主要原因還是編碼問題。我們現(xiàn)在正在想辦法解決。
Q:導(dǎo)出時文件內(nèi)容亂碼
A:首先請確認使用的是extremecomponents-1.0.1-M5-A4版以后的版本
- Excle: 導(dǎo)出為Excle的中文問題已經(jīng)修正,默認的情況下支持導(dǎo)出中文,用戶不需要任何改動
- PDF : 由于extremecomponents使用了FOP來生成PDF文件,F(xiàn)OP在導(dǎo)出中文內(nèi)容時會產(chǎn)生亂碼。具體的解決方案 大家可以參考最新eXtremeComponents包:支持 PDF中文導(dǎo)出
Q:當(dāng)變量名為"action",在IE下執(zhí)行產(chǎn)生javascript錯誤
A: 內(nèi)部使用了一些關(guān)鍵字,就目前我所知的為"action"、"submit"。建議大家命名時盡量避免,如果大家必須使用,則可以使用table標(biāo)簽的autoIncludeParameters參數(shù)設(shè)置為"false":
autoIncludeParameters="false"
Q:怎么樣格式化輸出表單中的數(shù)據(jù)
A: 你可以設(shè)置列的cell:
- 日期格式化: cell = " date " format = " yyyy-MM-dd "
- 數(shù)字格式化: cell="currency" format="###,###,##0.00"
詳細信息請參考指南
Q:怎么樣加入鏈接
A: 你可以參考下例:
<ec:table
var="pres"
items="presidents"
action="${pageContext.request.contextPath}/compact.run"
imagePath="${pageContext.request.contextPath}/images/table/compact/*.gif"
view="compact"
title="Compact Toolbar View"
showTooltips="false"
>
<ec:exportPdf
fileName="output.pdf"
tooltip="Export PDF"
headerColor="black"
headerBackgroundColor="#b6c2da"
headerTitle="Presidents"
text="PDF"
/>
<ec:exportXls
fileName="output.xls"
tooltip="Export Excel"
text="XLS"
/>
<ec:row>
<ec:column property="fullName" title="Name">
<a >${pres.fullName}</a>
</ec:column>
<ec:column property="nickName"/>
<ec:column property="term"/>
<ec:column property="born" cell="date"/>
<ec:column property="died" cell="date"/>
<ec:column property="career"/>
</ec:row>
</ec:table>
Q: 我想使用行的高亮顯示如何設(shè)置
A: 你只需要設(shè)置行標(biāo)簽的highlightRow屬性: highlightRow="true"。eXtremeComponents提供了很多接口允許用戶按照自己的習(xí)慣來進行定制,包括:CSS、CELL、View。相關(guān)信息請參考指南。
0
by lucky
posted @
2007-01-26 16:04 Super·shen BLOG 閱讀(280) |
評論 (0) |
編輯 收藏
》對象之間方法調(diào)用,通過傳遞消息
OOP使各個對象各司其職,分別負擔(dān)執(zhí)行一組相關(guān)的任務(wù),如果一個對象要依賴于一個不在其范圍內(nèi)的方法,它就需要訪問包含該方法的第二個對象,即第一個對象需要第二個對象執(zhí)行這個方法(或者叫方法調(diào)用) 利用OOP術(shù)語,叫做一個對象向另外一個對象發(fā)送消息。
》對象的生成: 對象是在執(zhí)行過程中由其所屬的類動態(tài)生成的。 一個類可以生成多個不同的對象。
》 消息與方法的概念
對象之間的傳遞通過消息傳遞完成
一個發(fā)送消息的對象 發(fā)送的消息包含3個方面的內(nèi)容
1,接受消息的對象
2,接受對象應(yīng)用的方法。
3,方法所需要的參數(shù)。
》面向?qū)ο笞兂傻幕咎卣?br />1 封裝性 Encapsulation 把數(shù)據(jù)和操作組織在類內(nèi)
2?繼承性 Inheritance 通過類的繼承關(guān)系
3多態(tài)性Polymophism(在運行的時候體現(xiàn)) ??A通過方法重裁 B通過方法重寫,子類覆蓋父類的方法(接口一個種特殊的類哦)
posted @
2007-01-24 17:47 Super·shen BLOG 閱讀(358) |
評論 (0) |
編輯 收藏
域?qū)ο笾g的4種關(guān)系
1。關(guān)聯(lián)
指類之間的引用關(guān)系,是實體域?qū)ο蟮淖钇毡榈年P(guān)系。
一對一、一對多、多對多關(guān)聯(lián)
如果類A和類B關(guān)聯(lián),那么被引用的B將被定義為A的屬性。
Customer? 與 Order 是一對多的關(guān)聯(lián)關(guān)系
Order 類 包含 Customer類的 屬性
Customer? 類 包含 集合 Order?
2。依賴
posted @
2006-12-30 16:30 Super·shen BLOG 閱讀(243) |
評論 (0) |
編輯 收藏
一個J2EE 工程中涉及的對象
- 數(shù)據(jù)傳輸對象DTO
- 業(yè)務(wù)對象BO (實體業(yè)務(wù)對象 過程業(yè)務(wù)對象 事件業(yè)務(wù)對象)
- 數(shù)據(jù)訪問對象DAO
?
概念
持久化框架、ORM框架、DAO設(shè)計模式
他們的關(guān)系是:ORM框架是一種持久化框架,DAO是用于實現(xiàn)持久化框架的一種設(shè)計模式。
posted @
2006-12-27 17:14 Super·shen BLOG 閱讀(413) |
評論 (0) |
編輯 收藏
?ActionErrors ? errors ? = ? null; ? ?
? errors ? = ? new ? ActionErrors(); ? ?
? errors.add("isExist", ? new ? ActionError("error.isExist"));???
// errors.add("isExist", ? new ? ActionError("error.isExist"));???等效于errors.add("isExist", ? new ? ActionMessage("error.isExist"));????
? saveErrors(request, ? errors); ?
? return ? (mapping.findForward("failure"));???
failure頁面里也定義了<html:errors?? name="isExist"/> ?
? ApplicationResources.properties里面也定義了error.isExist=user ? have ? already ? exist!!!???
? 運行結(jié)果 跳轉(zhuǎn)到failure頁面,顯示“user ? have ? already ? exist!!!???”
ActionErrors.GLOBAL_ERROR
怎么理解
它和我們使用普通的字符有什么區(qū)別啊
部分代碼如下:
err.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.logon"));
err.add("errinfo", new ActionError("error.logon"));
以上兩名有什么區(qū)別啊
沒什么區(qū)別
ActionErrors.GLOBAL_ERROR也是一個字符串。 最好寫做ActionErrors.GLOBAL_ERROR
不然的話可能會報錯。
posted @
2006-12-20 18:03 Super·shen BLOG 閱讀(2596) |
評論 (3) |
編輯 收藏
和女主角Action 對象共舞
什么是Action?
和常規(guī)的web 應(yīng)用相比,Struts Action 類工作起來就象一個小型的servlet。在大多數(shù)Java
應(yīng)用中,諸如訪問業(yè)務(wù)層的任務(wù)、錯誤處理等任務(wù)均是由servlet 承擔(dān)的。在一個 Struts 應(yīng)
用中,servlet 扮演著一個分派器的角色。而Action 對象則干實際的工作。象 servlets 一樣,
Action 對象是多線程的。每個應(yīng)用只需要一個Action 類的實例。
Action做些什么?
一個典型的Action 的職責(zé)通常是:
?? ■校驗前提條件或者聲明
?? ■調(diào)用需要的業(yè)務(wù)邏輯方法
?? ■檢測其它處理錯誤
?? ■將控制路由到相關(guān)視圖
檢驗輸入: Action 需要做的就是確認ActionForm 是否是需要的類型。
調(diào)用邏輯業(yè)務(wù):
Action 類是HTTP 與應(yīng)用系統(tǒng)中其它部分之間的適配器。最重要的是要避免將業(yè)務(wù)邏輯放入
Action 之中。Action 類應(yīng)該只是簡單地收集業(yè)務(wù)方法需要的數(shù)據(jù)并傳遞它到具體的業(yè)務(wù)對
象。如果你同時在編寫業(yè)務(wù)類和Action 類,可能會受到要將它們編寫在一起的誘惑。一定
要抵擋這種誘惑,并且將業(yè)務(wù)方法放入Action 可調(diào)用的單獨的類之中。Java 虛擬機(JVM)
針對這種方法調(diào)用作了優(yōu)化;性能損失可以忽略不計。
同時你也得到了一些設(shè)計上的優(yōu)勢
Action檢測錯誤:
Struts具有一個設(shè)計良好的錯誤處理系統(tǒng),允許你可以:
??■ 同時截獲幾個錯誤
??■ 在請求中傳遞錯誤數(shù)據(jù)包
??■ 顯示本地化信息
這個處理流程涉及到兩個對象 (ActionErrors 和 ActionError) 和一個注冊錯誤的工
具方法(saveErrors) 。其它兩個對象 (MessageResources 和 一個定制標(biāo)簽)則用來顯
示錯誤信息
注冊錯誤
總體流程歸結(jié)為:
??1 創(chuàng)建一個空的ActionErrors 實例
??2 在錯誤發(fā)生時,為錯誤信息添加關(guān)鍵字;
??3 檢查是否添加了某些信息
??4 保存ActionErrors 集合對象到請求中
??5 ?轉(zhuǎn)發(fā)控制到錯誤頁面以顯示信息
??6? 否則,正常繼續(xù)
例如
ActionErrors?errors?=?new?ActionErrors();

try?
{
//?*?調(diào)用業(yè)務(wù)對象?*
}

catch?(ModelException?e)?
{
errors.add(ActionErrors.GLOBAL_ERROR,
new?ActionError("error.detail",e.getMessage()));
}

if?(!errors.empty())?
{
saveErrors(Request,?errors);
return?(mapping.findForward("error"));
}
//?*?正常繼續(xù)?*
posted @
2006-12-14 10:32 Super·shen BLOG 閱讀(422) |
評論 (0) |
編輯 收藏
用Struts開發(fā)Web應(yīng)用
要使用Struts 開發(fā)web 應(yīng)用,開發(fā)人員將需要的超鏈接定義為ActionForward,HTML 表
單定義為ActionForm,定制的服務(wù)器端動作定義為Action 類。
需要訪問JDBC 和EJB 的開發(fā)人員也可通過Action 對象進行他們的工作。這樣,表現(xiàn)層不
需要和Model 層打交道。Struts Action 對象將收集View 需要的數(shù)據(jù),然后將它們轉(zhuǎn)發(fā)到
表現(xiàn)頁面。Struts 提供 JSP 標(biāo)記庫,它們將和JSP 頁面一起使用,簡化 HTML 表單和訪
問Action 要轉(zhuǎn)發(fā)的其它數(shù)據(jù)。其它表現(xiàn)機制,比如Velocity templates, 也可用來訪問
Struts 框架,以創(chuàng)建動態(tài)的web 頁面。這種處理流程入下圖:
posted @
2006-12-06 17:50 Super·shen BLOG 閱讀(289) |
評論 (0) |
編輯 收藏
?
Java本身就支持多國語言編碼,不需要寫任何程序,可以很簡單的
實現(xiàn)。
秘訣就是兩點:
1、所有HTML/JSP頁面全部采用UTF-8編碼
2、客戶端瀏覽器完全支持UTF-8編碼
步驟:
1、首先把所有的HTML/JSP的ContentType都設(shè)為UTF-8
2、然后對于JSP程序中的非ASCII碼提示信息都不應(yīng)該寫在程序里面,都應(yīng)該放在
application.properties里面統(tǒng)一管理。
3、對HTML用native2ascii工具統(tǒng)一做一次處理,把HTML中的非ASCII碼都轉(zhuǎn)換為Unicode編碼。
4、針對不同的語言,寫不同的application.properties,比如說簡體中文是
application_zh_CN.properties,繁體中文是application_zh_TW.properties這樣,然后對這些配置信
息文件同樣用native2ascii工具處理一次,把非ASCII碼統(tǒng)統(tǒng)轉(zhuǎn)為Unicode編碼。
5、在Servlet的request.getCharacterEncoding()獲得客戶端的操作系統(tǒng)默認編碼,然后set到Struts
的HTTPSession的Locale中。
OK!現(xiàn)在不同的客戶訪問,就會顯示不同的語言版本了。你可以看看此時你的瀏覽器的字符集,就是
UTF-8?,F(xiàn)在你的網(wǎng)站和Google一樣了,嘿嘿,其實你有心的話,看看你的瀏覽器訪問Google的時候是
什么字符集吧
切記:所有的HTML/JSP都要設(shè)為UTF-8編碼,所有的文件中的非ASCII碼字符都要用native2ascii工具轉(zhuǎn)
為用ASCII表示的Unicode編碼。
----------------------------------------
----------------------------------------
原創(chuàng)
----------------------------------------
上面所述是我從網(wǎng)上下的一篇于中文問題的解決方案,確切的說應(yīng)該是關(guān)于Struts的國際化問題,下面我結(jié)合我的實踐談?wù)劸唧w如何實現(xiàn)Struts的國際化問題,我對理論不是非常精通,我只能完全憑自己的理解和實踐來講述,所以下面講的內(nèi)容可能不是非常正確,還請大家原諒。但有一點可以肯定,我通過自己的努力解決了Struts的中文問題,并實現(xiàn)Struts的國際化,其實一切并不復(fù)雜,下面是具體步驟:
0.遇到的問題(這些問題也許不會同時出現(xiàn))
a.中文數(shù)據(jù)從數(shù)據(jù)庫中到j(luò)sp中后就變成了"????"
b.做好的中文properties文件,其中的中文value在頁面顯示亂碼
c.jsp文件中的中文到瀏覽器后顯示時也是亂碼(建議不要在jsp文件中輸入中文,盡量放在properties文件中)
d.由jsp傳給bean的中文值,再由bean傳回頁面又是亂碼
e.當(dāng)更換本地瀏覽器的語言選項時,Web應(yīng)用程序不能自動根據(jù)你的locale選擇合適的*.properties文件。導(dǎo)致Web應(yīng)用程序不能國際化。
1.環(huán)境:
Web服務(wù)器: Tomcat 5.0.19
操作系統(tǒng): Win2000 Server
JVM : jdk 1.4
數(shù) 據(jù) 庫: Oracle 8.1.7
開發(fā)工具: struts studio 5.2 pro for eclipse
2.先將所有*.jsp 網(wǎng)頁中開頭處加入
<%@ page language="java" contentType="text/html; charset=utf-8" %>
再設(shè)置<html:html locale = "true">
3.然后編輯好兩個*.properties文件,放在classes文件夾下你指定的地方,這里是放在/web-inf/classes/com/wiley 下,它們分別是:
ApplicationResources.properties (英文資源文件)
ApplicationResources_zh.properties (中文資源文件)
隨便用什么工具編寫都行啊!
4.將ApplicationResources_zh.properties轉(zhuǎn)碼成gb2312。上面引文說要轉(zhuǎn)成UTF-8,結(jié)果我試了,不行。轉(zhuǎn)成gb2312就行了,操作是。
將ApplicationResources_zh.properties更名為ApplicationResources_xx.properties
在DOS命令行進入ApplicationResources_xx.properties所在的文件夾
使用命令:native2ascii -encoding gb2312 ApplicationResources_xx.properties ApplicationResources_zh.properties(至于你為什么會出現(xiàn)“native2ascii不是內(nèi)部命令”,,請查其它資料,可能你要設(shè)置環(huán)境變量,因為他是jdk的文件夾bin下的一個應(yīng)用程序)
5.接下來配置struts-config.xml,很簡單,我們加入:
<message-resources parameter="com.wiley.ApplicationResources"/> 就行了;
到此已能解決大多數(shù)中文問題。如上面所說的a,b,e 現(xiàn)在打開瀏覽器,選擇菜單:工具》internet選項》語言,將“中文-中國[zh-cn]”刪掉,添加一個“英語-英國[zh-gb]”確定后,重啟Tomcat,輸入網(wǎng)址你就會發(fā)現(xiàn),你的頁面的文本信息就會用的是ApplicationResources.properties (英文資源文件)中的內(nèi)容。如果換回“中文-中國[zh-cn]”,它就會顯示ApplicationResources_zh.properties (中文資源文件)中的中文內(nèi)容。
至于問題“c.jsp文件中的中文到瀏覽器后顯示時也是亂碼” 你就要用與第4步類似的方法來重新對*.jsp 文件編碼,這時-encoding的參數(shù)就要用UTF-8了,如果你用的也是struts studio 5.2 pro for eclipse工具,這一步就免了。它會自動用UTF-8的格式存儲。
至于問題“d.由jsp傳給bean的中文值,再由bean傳回頁面又是亂碼”的解決,我只是加了個過濾器。
你可以現(xiàn)在web.xml中加入:
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>com.wiley.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<init-param>
<param-name>ignore</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<servlet-name>action</servlet-name>
</filter-mapping>
然后在你指定的包內(nèi)加個java文件 我放在了/web-inf/classes/com/wiley 里,下面是源代碼:
/*
* XP Forum
*
* Copyright (c) 2002-2003 RedSoft Group. All rights reserved.
*
*/
package com.huahang.tj.struts.filters;
import javax.servlet.*;
import java.io.IOException;
/**
* <p>Filter that sets the character encoding to be used in parsing the
* incoming request, either unconditionally or only if the client did not
* specify a character encoding. Configuration of this filter is based on
* the following initialization parameters:</p>
* <ul>
* <li><strong>encoding</strong> - The character encoding to be configured
* for this request, either conditionally or unconditionally based on
* the <code>ignore</code> initialization parameter. This parameter
* is required, so there is no default.</li>
* <li><strong>ignore</strong> - If set to "true", any character encoding
* specified by the client is ignored, and the value returned by the
* <code>selectEncoding()</code> method is set. If set to "false,
* <code>selectEncoding()</code> is called <strong>only</strong> if the
* client has not already specified an encoding. By default, this
* parameter is set to "true".</li>
* </ul>
*
* <p>Although this filter can be used unchanged, it is also easy to
* subclass it and make the <code>selectEncoding()</code> method more
* intelligent about what encoding to choose, based on characteristics of
* the incoming request (such as the values of the <code>Accept-Language</code>
* and <code>User-Agent</code> headers, or a value stashed in the current
* user′s session.</p>
*
* @author <a href="mailto:jwtronics@yahoo.com">John Wong</a>
*
* @version $Id: SetCharacterEncodingFilter.java,v 1.1 2002/04/10 13:59:27 johnwong Exp $
*/
public class SetCharacterEncodingFilter implements Filter {
// ----------------------------------------------------- Instance Variables
/**
* The default character encoding to set for requests that pass through
* this filter.
*/
protected String encoding = null;
/**
* The filter configuration object we are associated with. If this value
* is null, this filter instance is not currently configured.
*/
protected FilterConfig filterConfig = null;
/**
* Should a character encoding specified by the client be ignored?
*/
protected boolean ignore = true;
// --------------------------------------------------------- Public Methods
/**
* Take this filter out of service.
*/
public void destroy() {
this.encoding = null;
this.filterConfig = null;
}
/**
* Select and set (if specified) the character encoding to be used to
* interpret request parameters for this request.
*
* @param request The servlet request we are processing
* @param result The servlet response we are creating
* @param chain The filter chain we are processing
*
* @exception IOException if an input/output error occurs
* @exception ServletException if a servlet error occurs
*/
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
// Conditionally select and set the character encoding to be used
if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null)
request.setCharacterEncoding(encoding);
}
// Pass control on to the next filter
chain.doFilter(request, response);
}
/**
* Place this filter into service.
*
* @param filterConfig The filter configuration object
*/
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
String value = filterConfig.getInitParameter("ignore");
if (value == null)
this.ignore = true;
else if (value.equalsIgnoreCase("true"))
this.ignore = true;
else if (value.equalsIgnoreCase("yes"))
this.ignore = true;
else
this.ignore = false;
}
// ------------------------------------------------------ Protected Methods
/**
* Select an appropriate character encoding to be used, based on the
* characteristics of the current request and/or filter initialization
* parameters. If no character encoding should be set, return
* <code>null</code>.
* <p>
* The default implementation unconditionally returns the value configured
* by the <strong>encoding</strong> initialization parameter for this
* filter.
*
* @param request The servlet request we are processing
*/
protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}
}//EOC
到此我遇到的中文問題已全部得到解決,并從中理解到struts的國際化的深刻含義。
我個人覺得struts作為一個功能強大的應(yīng)用框架,應(yīng)該早就考慮到它的國際化問題,并在實際應(yīng)用中不會很復(fù)雜,只要我們遵循一些規(guī)則,就可以盡情享受struts給我們帶來的無窮樂趣。希望以上所述對大家有所幫助。
posted @
2006-12-06 16:46 Super·shen BLOG 閱讀(300) |
評論 (0) |
編輯 收藏
Cannot retrieve mapping for action
異常
javax.servlet.jsp.JspException: Cannot retrieve mapping for action /Login (/Login是你的action名字)
可能原因
action沒有再struts-config.xml 中定義,或沒有找到匹配的action,例如在JSP文件中使用 <html:form action="Login.do".將表單提交給Login.do處理,如果出現(xiàn)上述異常,請查看struts-config.xml中的定義部分,有時可能是打錯了字符或者是某些不符合規(guī)則,可以使用struts console工具來檢查。
Cannot retrieve definition for form bean null
異常
org.apache.jasper.JasperException: Cannot retrieve definition for form bean null
可能原因
這個異常是因為Struts根據(jù)struts-config.xml中的mapping沒有找到action期望的form bean。大部分的情況可能是因為在form-bean中設(shè)置的name屬性和action中設(shè)置的name屬性不匹配所致。換句話說,action和form都應(yīng)該各自有一個name屬性,并且要精確匹配,包括大小寫。這個錯誤當(dāng)沒有name屬性和action關(guān)聯(lián)時也會發(fā)生,如果沒有在action中指定name屬性,那么就沒有name屬性和action相關(guān)聯(lián)。當(dāng)然當(dāng)action制作某些控制時,譬如根據(jù)參數(shù)值跳轉(zhuǎn)到相應(yīng)的jsp頁面,而不是處理表單數(shù)據(jù),這是就不用name屬性,這也是action的使用方法之一。
No action instance for path /xxxx could be created
異常
No action instance for path /xxxx could be created
可能原因
特別提示:因為有很多中情況會導(dǎo)致這個錯誤的發(fā)生,所以推薦大家調(diào)高你的web服務(wù)器的日志/調(diào)試級別,這樣可以從更多的信息中看到潛在的、在試圖創(chuàng)建action類時發(fā)生的錯誤,這個action類你已經(jīng)在struts-config.xml中設(shè)置了關(guān)聯(lián)(即添加了<action>標(biāo)簽)。
在struts-config.xml中通過action標(biāo)簽的class屬性指定的action類不能被找到有很多種原因,例如:
定位編譯后的.class文件失敗。Failure to place compiled .class file for the action in the classpath (在web開發(fā)中,class的的位置在r WEB-INF/classes,所以你的action class必須要在這個目錄下。例如你的action類位于WEB-INF/classes/action/Login.class,那么在struts-config.xml中設(shè)置action的屬性type時就是action.Login).
拼寫錯誤,這個也時有發(fā)生,并且不易找到,特別注意第一個字母的大小寫和包的名稱。
在struts-config.xml中指定的action類沒有繼承自Stuts的Action類,或者你自定義的Action類沒有繼承自Struts提供的Action類。
你的action類必須繼承自Struts提供的Action類。
你的classpath的問題。例如web server沒有發(fā)現(xiàn)你的資源文件,資源文件必須在WEB-INF/classes/目錄下。
Problem in struts-config.xml file with action mapping.
Problem with data-sources.xml file.
相關(guān)鏈接
http://www.mail-archive.com/struts-user ... org/msg65874.html
Action Mapping mistake in struts-config.xml:
http://www.manning.com/ao/readforum.ht ... mp;readthread=177
data-sources.xml file?:
http://www.caucho.com/quercus/faq/section.xtp?section_id=30
No getter method for property XXXX of bean org.apache.struts.taglib.html.BEAN
異常
javax.servlet.jsp.JspException: No getter method for property username of bean org.apache.struts.taglib.html.BEAN
可能原因
沒有位form bean中的某個變量定義getter 方法
這個錯誤主要發(fā)生在表單提交的FormBean中,用struts標(biāo)記<html:text property=”username”>時,在FormBean中必須有一個getUsername()方法。注意字母“U”。
Related Links
Case can trip up the matching between get method's name and name specified in Struts tag
http://saloon.javaranch.com/cgi-bin/ubb/ultimate ... c&f=58&t=000163
java.lang.NoClassDefFoundError: org/apache/struts/action/ActionForm
錯誤
java.lang.NoClassDefFoundError: org/apache/struts/action/ActionForm
可能原因
這個錯誤主要發(fā)生在在classpath中找不到相應(yīng)的Java .class文件。如果這個錯誤發(fā)生在web應(yīng)用程序的運行時,主要是因為指定的class文件不在web server的classpath中(/WEB-INF/classes 和 /WEB-INF/lib)。
在上面的錯誤中,原因是找不到ActionForm類。
This error is sometimes seen when one or more ActionForm.class instances are actually in the classpath. This most often occurs when ActionForm.class is made available correctly by placing struts.jar in the /WEB-INF/lib directory. When this library has been correctly placed and it is verified that ActionForm.class actually is present in the struts.jar file, the problem is either that more than one copy of ActionForm.class is in the classpath or (more likely) that duplicate versions of class files other than ActionForm are in the same classpath, causing confusion. This is especially true if a class that extends ActionForm is made available twice, such as in an .ear file that encompasses a .war file as well as in the .war file's own classpath (/WEB-INF/classes). This problem can be resolved by guaranteeing that there are no redundant classes, especially those related to Struts (directly from Struts or extensions of Struts), in the web application's view.
相關(guān)連接
EJB and Web Shared Links:
http://forum.java.sun.com/thread.jsp?forum=26&am ... ;tstart=0&trange=15
Keep Action and ActionForm (and their children) as non-overlapping unit(s) of an application
http://www.mail-archive.com/struts-u ... ache.org/msg47466.html
http://www.mail-archive.com/struts-user ... org/msg47467.html
Exception creating bean of class org.apache.struts.action.ActionForm: {1}
異常
javax.servlet.jsp.JspException: Exception creating bean of class org.apache.struts.action.ActionForm: {1}
可能原因
Instantiating Struts-provided ActionForm class directly instead of instantiating a class derived off ActionForm. This might occur implicitly if you specify that a form-bean is this Struts ActionForm class rather than specifying a child of this class for the form-bean.
Not associating an ActionForm-descended class with an action can also lead to this error.
Related Links
Cannot find ActionMappings or ActionFormBeans collection
Exception
javax.servlet.jsp.JspException: Cannot find ActionMappings or ActionFormBeans collection
可能原因
不是標(biāo)識Struts actionServlet的<servlet>標(biāo)記就是映射.do擴展名的<sevlet-mapping>標(biāo)記或者兩者都沒有在web.xml中聲明。
在struts-config.xml中的打字或者拼寫錯誤也可導(dǎo)致這個異常的發(fā)生。例如缺少一個標(biāo)記的關(guān)閉符號/>。最好使用struts console工具檢查一下。
另外,load-on-startup必須在web.xml中聲明,這要么是一個空標(biāo)記,要么指定一個數(shù)值,這個數(shù)值用來表servlet運行的優(yōu)先級,數(shù)值越大優(yōu)先級越低。
還有一個和使用load-on-startup有關(guān)的是使用Struts預(yù)編譯JSP文件時也可能導(dǎo)致這個異常。
相關(guān)鏈接
Explicitly Define <load-on-startup>
http://saloon.javaranch.com/cgi-bin/ubb/ultim ... topic&f=50&t=001055
http://threebit.net/tutorials/ejb/general/
NullPointerException at ... RequestUtils.forwardURL
異常
java.lang.NullPointerException at org.apache.struts.util.RequestUtils.forwardURL(RequestUtils.java:1223)
可能原因
在struts-config.xml中的forward元素缺少path屬性。例如應(yīng)該是如下形式:
<forward name="userhome" path="/user/userhome.jsp"/>
Cannot find bean org.apache.struts.taglib.html.BEAN in any scope
Exception
javax.servlet.jsp.JspException: Cannot find bean org.apache.struts.taglib.html.BEAN in any scope
Probable Causes
試圖在Struts的form標(biāo)記外使用form的子元素。這常常發(fā)生在你在</html:form>后面使用Struts的html標(biāo)記。
另外要注意可能你不經(jīng)意使用的無主體的標(biāo)記,如<html:form … />,這樣web 服務(wù)器解析時就當(dāng)作一個無主體的標(biāo)記,隨后使用的所有<html>標(biāo)記都被認為是在這個標(biāo)記之外的,如又使用了<html:text property=”id”>
還有就是在使用taglib引入HTML標(biāo)記庫時,你使用的prefix的值不是html。
相關(guān)連接
Using form subelements outside of a form tag
http://forum.java.sun.com/thread.jsp?thread= ... &message=1384153
Missing message for key xx.xx.xx
Exception
javax.servlet.jsp.JspException: Missing message for key xx.xx.xx
Probable Causes
這個key的值對沒有在資源文件ApplicationResources.properties中定義。如果你使用eclipse時經(jīng)常碰到這樣的情況,當(dāng)項目重新編譯時,eclipse會自動將classes目錄下的資源文件刪除。
資源文件ApplicationResources.properties 不在classpath中 應(yīng)將資源文件放到 WEB-INF/classes 目錄下,當(dāng)然要在struts-config.xml中定義)
Cannot find message resources under key org.apache.struts.action.MESSAGE
異常
Cannot find message resources under key org.apache.struts.action.MESSAGE
可能原因
很顯然,這個錯誤是發(fā)生在使用資源文件時,而Struts沒有找到資源文件。
Implicitly trying to use message resources that are not available (such as using empty html:options tag instead of specifying the options in its body -- this assumes options are specified in ApplicationResources.properties file)
XML parser issues -- too many, too few, incorrect/incompatible versions
Related Links
Provide Struts with Resource Bundle
http://threebit.net/tutorials/ejb/general/
XML Parser Issues
http://www.mail-archive.com/struts-user ... org/msg15779.html
No input attribute for mapping path /loginAction
錯誤
No input attribute for mapping path /xxxxAction
可能原因e
No input attribute in action mapping in struts-config.xml file for the action with the name specified in the error message. An input attribute is not required if form validation is not performed (either because the validate attribute is set to false or because the validation method in the relevant form class is not implemented. The input attribute specifies the page leading to this action because that page is used to display error messages from the form validation.
Related Links
Strange Output Characters
錯誤
Strange and seemingly random characters in HTML and on screen, but not in original JSP or servlet.
可能原因
混和使用Struts的html:form標(biāo)記和標(biāo)準(zhǔn)的HTML標(biāo)記不正確。
使用的編碼樣式在本頁中不支持。
"Document contained no data" or no data rendered on page
錯誤
"Document contained no data" in Netscape
No data rendered (completely empty) page in Microsoft Internet Explorer
可能原因
使用一個Action的派生類而沒有實現(xiàn)perform()方法或execute()方法。在Struts1.0中實現(xiàn)的是perform()方法,在Struts1.1中實現(xiàn)的是execute()方法,但Struts1.1向后兼容perform()方法。
但你使用Struts1.1創(chuàng)建一個Action的派生類,并且實現(xiàn)了execute()方法,而你在Struts1.0中運行的話,就會得到"Document contained no data" error message in Netscape or a completely empty (no HTML whatsoever) page rendered in Microsoft Internet Explorer.”的錯誤信息。
posted @
2006-12-06 16:44 Super·shen BLOG 閱讀(490) |
評論 (0) |
編輯 收藏
Action類
用來訪問業(yè)務(wù)邏輯類。當(dāng) ActionServlet 從容器接收到一個請求,它使用URI (或者路徑
“path”) 來決定那個Action 將用來處理請求。一個 Action 可以校驗輸入,并且訪問業(yè)務(wù)
層以從數(shù)據(jù)庫或其他數(shù)據(jù)服務(wù)中檢索信息。
ActionForm類
為校驗輸入或者使用輸入來更新數(shù)據(jù)庫, Action 需要知道什么值被提交上來。它并不是強
制每個Action 都要從請求中抓取這些值,而是由 ActionServlet 將輸入綁定到JavaBean中。
輸入 bean 就是Struts ActionForm 類的子類。ActionServlet 通過查找請求的路徑可以決定使
用哪個ActionForm , Action 也是通過同樣的方法選取的。ActionForm 擴展了
org.apache.struts.action.ActionForm 類
每個請求都必須以HTTP 響應(yīng)進行應(yīng)答。 通常,Struts Action 并不自行渲染響應(yīng)信息,
而是將請求轉(zhuǎn)發(fā)到其他資源,比如JSP 頁面。Struts 提供一個ActionForward 類,用來將
一個頁面的路徑保存為邏輯名稱。當(dāng)完成業(yè)務(wù)邏輯后,Action 選擇并向Servlet 返回一個
ActionForward。Servlet 然后使用保存在ActionForward 對象中的路徑來調(diào)用頁面完成響
應(yīng)。
Struts 將這些細節(jié)都綁定在一個ActionMapping 對象中。每個ActionMapping 相對于一個
特定的路徑。當(dāng)某個路徑被請求時,Servlet 就查詢ActionMapping 對象。ActionMapping
對象告訴servlet,哪些個Action,ActionForm,和 ActionForward 將要被本次請求使用。
所有這些細節(jié),關(guān)于Action, ActionForm, ActionForward, ActionMapping,以及其
它一些東西,都在struts-config.xml 文件中定義。 ActionServlet 在啟動時讀取這個配置文
件,并創(chuàng)建一個配置對象數(shù)據(jù)庫
舉個例子來說,
頁面上如果有以下語句:
<html:form?action="/hello.do">
當(dāng)submit此form時,控制器從配置文件中找<action-mappings>下path="/hello"?的<action>元素
然后根據(jù)此action元素的type屬性找要執(zhí)行的action類文件
比如
<action-mappings>
????<action?path="hello"
????????????type="cn.com.mylu.Hello"
????????????name="Hellofrom"
????????????……
????</action>
其中 cn.com.mylu.Hellofrom?為ActionForm類javabean
???????????cn.com.mylu.Hello 為Action類
??? <form-bean name="Hellofrom" type="cn.com.mylu.Hellofrom">
??? </form-bean>
頁面hello.do----->?action元素的path屬性?------>?action元素的type屬性----->Hello.java---->Hello.java中的execute方法
posted @
2006-12-06 16:25 Super·shen BLOG 閱讀(351) |
評論 (0) |
編輯 收藏
在講外連接之前,先舉例介紹內(nèi)連接,也就是一般的相等連接。
select * from a, b where a.id = b.id;
對于外連接,Oracle中可以使用“(+)”來表示,9i可以使用LEFT/RIGHT/FULL OUTER JOIN,下面將配合實例一一介紹。
1. LEFT OUTER JOIN:左外關(guān)聯(lián)
SELECT e.last_name, e.department_id, d.department_name?
FROM employees e?
LEFT OUTER JOIN departments d?
ON (e.department_id = d.department_id);
?
等價于
SELECT e.last_name, e.department_id, d.department_name?
FROM employees e, departments d?
WHERE e.department_id=d.department_id(+);
?
結(jié)果為:所有員工及對應(yīng)部門的記錄,包括沒有對應(yīng)部門編號department_id的員工記錄。
2. RIGHT OUTER JOIN:右外關(guān)聯(lián)
SELECT e.last_name, e.department_id, d.department_name?
FROM employees e?
RIGHT OUTER JOIN departments d?
ON (e.department_id = d.department_id);
?
等價于
SELECT e.last_name, e.department_id, d.department_name?
FROM employees e, departments d?
WHERE e.department_id(+)=d.department_id;
?
結(jié)果為:所有員工及對應(yīng)部門的記錄,包括沒有任何員工的部門記錄。
3. FULL OUTER JOIN:全外關(guān)聯(lián)
SELECT e.last_name, e.department_id, d.department_name?
FROM employees e?
FULL OUTER JOIN departments d?
ON (e.department_id = d.department_id);
?
結(jié)果為:所有員工及對應(yīng)部門的記錄,包括沒有對應(yīng)部門編號department_id的員工記錄和沒有任何員工的部門記錄。
posted @
2006-11-29 14:32 Super·shen BLOG 閱讀(413) |
評論 (0) |
編輯 收藏
下棋沒啥長進,今天用了半個小時看看兵書,總結(jié)一下。
開局基本原理:
[盡快集結(jié)子力]
[爭奪河界]
1。 盡快集結(jié)子力
·????????如無必要,不要重復(fù)走同一子
·? ?? ???先出炮、馬 ,然后車、兵?? ?[車要通 馬要靈 炮要能呼應(yīng)]
·? ?? ???不可用一兩個棋盲目進攻
·? ?? ???子力要互相呼應(yīng),互相配合,不可互相干擾和阻塞
·? ?? ???若吃兵使用對方能力快速出子或發(fā)動攻勢,便不要吃
·? ?? ???若吃兵使用對方能力快速出子或發(fā)動攻勢,便不要吃
2.?????? 爭奪河界 :
除車以外,其他棋子在河界附近都可發(fā)揮最大威力及控制力,在中心的棋子要調(diào)動到兩翼也較靈活、快速,所以河界附近為兵家必爭之地。若河界附近被對方占領(lǐng),便得設(shè)法攻下它。反之,自己占據(jù)時,須要堅守并利用它向?qū)Ψ降木艑m進攻。
中局原理
3.? ?? ? 善用「車」:
4.????????在防守中進攻,兼顧協(xié)調(diào)
5.?????? 保護「士兵」:
關(guān)鍵還是實戰(zhàn)時眼光銳利,先人一步。
posted @
2006-11-21 17:35 Super·shen BLOG 閱讀(5838) |
評論 (2) |
編輯 收藏
String strsql = "select temp.*,rownum? from (select * from tb_awardinfo order by award_id desc )temp where rownum <=5";
//自己亂寫的,不知道效率怎么樣,那位達人幫貼過代碼來參考參考
posted @
2006-11-21 16:17 Super·shen BLOG 閱讀(848) |
評論 (0) |
編輯 收藏
oracle 分頁
1. 最好還是利用分析函數(shù)
row_number() over ( partition by col1 order by col2 )
比如想取出100-150條記錄,按照tname排序
select tname,tabtype from (
?? select tname,tabtype,row_number() over ( order by tname ) rn from tab
)
where rn between 100 and 150;
2. 直接使用rownum 虛列
select tname,tabtype from (
?? select tname,tabtype,rownum rn from tab where rownum <= 150
)
where rn >= 100;
使用序列不能基于整個記錄集合進行排序,如果指定了order by子句,排序的的是選出來的記錄集的排序.
------------------------------------------------------------------------
經(jīng)過測試,在100萬條數(shù)據(jù)的表中,檢索數(shù)據(jù)的時候,方法2的速度要比方法1要快的.
排序分頁
說明:Oracle下用rownum進行分頁時 很容易出現(xiàn)排序的錯亂。
但多套一層select 就能很好的解決該問題,特此記錄,語句如下:
select t2.* from (select t1.*, rownum rn from (select * from tb_courseinfo order by rownum? desc )t1 where rownum <=?150 )t2 where rn >100
不懂具體效率怎么樣,和上邊一樣應(yīng)該還可以。
實際例子代碼如下
int curpage=1;//當(dāng)前頁
int page_record=20;//每頁顯示的記錄數(shù)
int introwcount=0; // 記錄數(shù)
if(request.getParameter("page")==null||Integer.parseInt(request.getParameter("page"))<=0)
{
curpage = 0;
}
else
{
curpage=Integer.parseInt(request.getParameter("page"))-1;//獲取傳遞的值,需要顯示的頁
}
String sql = "select t2.* from (select t1.*, rownum rn from (select * from tb_courseinfo order by rownum? desc )t1 where rownum <= "+(curpage+1)*page_record+" )t2 where rn > "+curpage*page_record;
posted @
2006-11-21 16:11 Super·shen BLOG 閱讀(4135) |
評論 (3) |
編輯 收藏
?
sys和system用戶的區(qū)別
【system】用戶只能用normal身份登陸em。
【sys】用戶具有“SYSDBA”或者“SYSOPER”權(quán)限,登陸em也只能用這兩個身份,不能用normal。
“SYSOPER”權(quán)限,即數(shù)據(jù)庫操作員權(quán)限,權(quán)限包括:
? 打開數(shù)據(jù)庫服務(wù)器?? 關(guān)閉數(shù)據(jù)庫服務(wù)器
? 備份數(shù)據(jù)庫?????? 恢復(fù)數(shù)據(jù)庫
? 日志歸檔???????? 會話限制
“SYSDBA”權(quán)限,即數(shù)據(jù)庫管理員權(quán)限,權(quán)限包括:
? 打開數(shù)據(jù)庫服務(wù)器?? 關(guān)閉數(shù)據(jù)庫服務(wù)器
? 備份數(shù)據(jù)庫?????? 恢復(fù)數(shù)據(jù)庫
? 日志歸檔?????? 會話限制
? 管理功能?????? 創(chuàng)建數(shù)據(jù)庫
normal 、sysdba、 sysoper有什么區(qū)別
normal 是普通用戶
另外兩個,你考察他們所具有的權(quán)限就知道了
sysdba擁有最高的系統(tǒng)權(quán)限
sysoper主要用來啟動、關(guān)閉數(shù)據(jù)庫,sysoper 登陸后用戶是 public
sysdba登陸后是 sys
SQL> conn / as sysdba
已連接。
SQL> grant sysoper to test;
授權(quán)成功。
SQL> conn test/test as sysoper;
已連接。
SQL> show user
USER 為"PUBLIC"
SQL> conn test/test as sysdba
已連接。
SQL> show user
USER 為"SYS"
SQL>
?
?
dba和sysdba的區(qū)別
dba、sysdba這兩個系統(tǒng)角色有什么區(qū)別呢
在說明這一點之前我需要說一下oracle服務(wù)的創(chuàng)建過程
·創(chuàng)建實例
·啟動實例
·創(chuàng)建數(shù)據(jù)庫(system表空間是必須的)
啟動過程
·實例啟動
·裝載數(shù)據(jù)庫
·打開數(shù)據(jù)庫
sysdba,是管理oracle實例的,它的存在不依賴于整個數(shù)據(jù)庫完全啟動,
只要實例啟動了,他就已經(jīng)存在,以sysdba身份登陸,裝載數(shù)據(jù)庫、打開數(shù)據(jù)庫
只有數(shù)據(jù)庫打開了,或者說整個數(shù)據(jù)庫完全啟動后,dba角色才有了存在的基礎(chǔ)!
posted @
2006-11-20 15:37 Super·shen BLOG 閱讀(7653) |
評論 (4) |
編輯 收藏
一直都是用sql server作為開發(fā)數(shù)據(jù)庫,最近因為開發(fā)新的項目使用oracle數(shù)據(jù)庫,我才開始學(xué)習(xí)oracle.
學(xué)習(xí)了一段時間,發(fā)現(xiàn)SQL Server和Oracle還是有這很大的差別。首先,我在SQL Server中用得很順手的自增長字段就在ORACLE中找不到了,朋友說可以用序列可以實現(xiàn),于是查閱資料,整理出如下示例:
方法一
1.建立測試數(shù)據(jù)表
CREATE TABLE TEST
(
?ID?NUMBER,
?NAME?VARCHAR2(20),
?PRIMARY KEY(ID)
);
2.創(chuàng)建序列
CREATE SEQUENCE SEQ_TEST;
3.創(chuàng)建觸發(fā)器
CREATE OR REPLACE TRIGGER AUTOINCREMENT
BEFORE INSERT ON TEST
FOR EACH ROW
WHEN (NEW.ID IS NULL)
BEGIN
SELECT SEQ_TEST.NEXTVAL INTO :NEW.ID FROM DUAL;
END;
/
4.插入數(shù)據(jù)
INSERT INTO TEST(NAME) VALUES('NAME1');
5.查看插入結(jié)果
SELECT * FROM TEST;
方法二
SQL ? Server,Sybase: ?
? 有個identity屬性可以讓系統(tǒng)自動增1 ?
? create ? table ? a ?
? ( ?
? a1 ? int ? identity(1,1), ?
? a2 ? varchar(6) ?
? )???
? 然后在insert時: ?
? insert ? into ? a ? values('hello!'); --不用管a1,系統(tǒng)會自動增1 ?
? ?
? Oracle: ?
? 使用SEQUENCE(序列)可以達到要求 ?
? create ? table ? a ?
? ( ?
? a1 ? int ? , ?
? a2 ? varchar2(6) ?
? ); ?
? ?
? create ? SEQUENCE ? seq_a ? ? INCREMENT ? BY ? 1;???
????
? 然后在insert時: ?
? insert ? into ? a ? values(seq_a.nextval,'hello!'); --seq_a.nextval是該序列的下個值
posted @
2006-11-20 00:39 Super·shen BLOG 閱讀(665) |
評論 (0) |
編輯 收藏
每天都在趕項目,只能用自己認為比較快的方式制作了!
寫好幾個類,寫好幾個頁面,?每天在復(fù)制粘貼代碼??!暈死
其實我也想弄高質(zhì)量的東西,沒有時間了
我想干類似我這種活的人不少
嚴(yán)重懷疑中國軟件的質(zhì)量
周末得加一天班了
弄個效果圖上

posted @
2006-11-16 23:29 Super·shen BLOG 閱讀(430) |
評論 (0) |
編輯 收藏
來公司上班已經(jīng)兩個月了,雖然工作相對來說不累, 但工作之余,內(nèi)心總是感覺前面的路不是很清晰。
過去一兩年我一直在做WEB項目的開發(fā), 包括網(wǎng)站,自助建站系統(tǒng),OA辦公系統(tǒng)等。因為一直在小公司干,專職的只有兩個程序員,所以基本每種技術(shù)都會一些(ASP,PHP,JSP,ASP.NET,MSSQL,MYSQL,ORACLE都有用過開發(fā)有案例),但明顯深度不夠,用得最好的算是VS2003開發(fā)ASP.NET吧。
最近這個月,我們老總接了一個朋友的單子,是一所??茖W(xué)院的OA系統(tǒng), 公司有一個JAVA程序工程師(聽說這個人很牛,在IBM中國做過1億多的項目)做了一個方案,用JAVA的什么什么技術(shù)實現(xiàn)( Struts+spring+hibernate+EJB),并報了個概價約20萬,提交到學(xué)校,學(xué)校沒有回音。
老總說這個月初,學(xué)校說要做了,并簽了合同 大概15萬(具體我沒得看)。
剛好上個月月尾,公司的唯一牛人 JAVA程序工程師辭職了。
老總就把這個項目扔給我做,我表示我沒這個能力,我對JAVA 的Struts+spring+hibernate+EJB 多層架構(gòu)根本不熟悉,他以為我在推托責(zé)任,老總叫我到辦公室說了一大堆理由,最終我沒說過他!我還是得做!(補充:老總對計算機技術(shù)是個文盲,是個很厚道的很有錢人。 有一次很搞笑,他叫我?guī)退麚Q個顯示器用,正在換的時候,他給我說:“你要記得把我原來電腦桌面上的文件保留下來啊” 我汗,我差點倒地)。
現(xiàn)在我硬著頭皮在搞這個項目,由于對JAVA 的Struts+spring+hibernate+EJB 多層架構(gòu)不懂,而項目又一定要用JAVA 做,時間又緊,40天完成,只有我一個人做,我只能直接用JSP + JAVABEAN+ORACLE做了!
現(xiàn)在已經(jīng)過去10天了,估計才完成1/3。
估計以后得加班了,應(yīng)該能按時交付。
但是,總感覺我這條路走得不對,做來做去都是一些沒什么價值的東西,沒有深度的東西, 現(xiàn)在什么年代了,還用在JSP+JAVA BEAN開發(fā),沒長進啊。
?
感覺我大學(xué)畢業(yè)時基礎(chǔ)也不差,也通過了程序設(shè)計師考試,畢業(yè)時候還混得個好工作。 但是這兩年混錯了方向,這里搞搞 那里搞搞,但都不深入學(xué)習(xí),兩年就這樣過去了!現(xiàn)在再來深入學(xué)習(xí)JAVA 還行嗎。。。。
posted @
2006-11-15 16:41 Super·shen BLOG 閱讀(577) |
評論 (2) |
編輯 收藏