CREATE TABLE `user` (
? `id` int(11) NOT NULL auto_increment,
?? `username` char(20) default NULL COMMENT '用戶名',
?? `password` char(20) default NULL COMMENT '密碼',
? PRIMARY KEY? (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 ;
最后是設(shè)定JSP文件的編碼,
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
或者是用個(gè)Filter:
經(jīng)過(guò)這樣三個(gè)步驟,就不存在亂碼問(wèn)題了。
另,不推薦用 MySQL-Front 這個(gè)客戶端程序管理UTF-8編碼的數(shù)據(jù)庫(kù)。如果是GBK編碼還可以考慮,UTF-8則會(huì)出現(xiàn)編碼問(wèn)題,導(dǎo)致程序不能正常工作,強(qiáng)烈推薦 Navicat 。 功能強(qiáng)大,默認(rèn)使用UTF-8編碼,界面友好、美觀。
當(dāng)用戶和代表命令或某操作(gesture)等控件發(fā)生交互的時(shí)候,觸發(fā) 動(dòng)作事件(action event)。產(chǎn)生動(dòng)作事件的組件也叫做動(dòng)作源,包括了按鈕、超鏈接等組件。動(dòng)作事件由動(dòng)作監(jiān)聽(tīng)器(action listener)進(jìn)行處理。
有兩種典型的監(jiān)聽(tīng)器:一種會(huì)影響到頁(yè)面間的導(dǎo)航(navigation),另一種不會(huì)影響到導(dǎo)航。影響到頁(yè)面間導(dǎo)航的監(jiān)聽(tīng)器主要是處理一些業(yè)務(wù)邏輯并返回幾個(gè)業(yè)務(wù)邏輯結(jié)果,JSF的導(dǎo)航系統(tǒng)將會(huì)根據(jù)這個(gè)來(lái)選擇恰當(dāng)?shù)南乱粋€(gè)頁(yè)面(譯注:并非傳統(tǒng)所說(shuō)的TRUE/FALSE這兩種“邏輯”結(jié)果,這里講的是“業(yè)務(wù)邏輯”,可以是任意的有代表意義的結(jié)果。)不影響導(dǎo)航的事件監(jiān)聽(tīng)器用來(lái)操作當(dāng)前頁(yè)面內(nèi)的組件,或者是進(jìn)行一些業(yè)務(wù)模型上的改變,或者是修改backing bean的屬性等,并不會(huì)出現(xiàn)在頁(yè)面間的跳轉(zhuǎn)。
在技術(shù)上,所有頁(yè)面間導(dǎo)航都是由一個(gè)單一的動(dòng)作監(jiān)聽(tīng)器來(lái)處理。這個(gè)監(jiān)聽(tīng)器自動(dòng)處理組件所觸發(fā)的所有事件(event),因此,該監(jiān)聽(tīng)器并不需要手動(dòng)地注冊(cè)/綁定(register)。缺省情況下,這個(gè)監(jiān)聽(tīng)器會(huì)將所有的工作交由你制定的backing bean中的某個(gè)方法(method)來(lái)處理。因此,你可以用不同的方法來(lái)處理你程序中的各個(gè)業(yè)務(wù)部分。典型地,你的大多數(shù)業(yè)務(wù)邏輯都將會(huì)交由這些方法來(lái)處理。
當(dāng)某個(gè)組件觸發(fā)一個(gè)事件的時(shí)候,缺省的監(jiān)聽(tīng)器就決定了該事件的處理結(jié)果,該結(jié)果用一個(gè)字符串(string)來(lái)表示,比方說(shuō):"success"、"failure" 等。這個(gè)結(jié)果也可以分為靜態(tài)和動(dòng)態(tài)兩類。靜態(tài)的是在編碼的時(shí)候就已經(jīng)定義好了的例如:
<h:commandButton type="submit" value="Login" action="success" immadiate="true" />
在這個(gè)例子當(dāng)中,當(dāng)用戶點(diǎn)擊這個(gè)標(biāo)為L(zhǎng)ogin的按鈕的時(shí)候,會(huì)產(chǎn)生 "success" 這個(gè)業(yè)務(wù)結(jié)果并同時(shí)觸發(fā)該按鈕被點(diǎn)擊這個(gè)事件,但是,并沒(méi)有相應(yīng)的方法來(lái)處理這個(gè)事件。
而動(dòng)態(tài)的結(jié)果就是由處理該事件的方法所返回的,一個(gè)字符串。一個(gè)方法可能會(huì)返回多個(gè)不同的結(jié)果,這取決于業(yè)務(wù)邏輯。動(dòng)作監(jiān)聽(tīng)器會(huì)自動(dòng)查找你在該組件的 action 屬性中指定的方法,并執(zhí)行。我們用 EL(expression language) 來(lái)指定該屬性值。下面是一個(gè) HtmlCommandButton 的實(shí)際例子:
<h:commandButton type="submit" value="Login" action="#{user.login}" />
當(dāng)用戶點(diǎn)擊該按鈕,一個(gè)代表該按鈕被點(diǎn)擊的事件產(chǎn)生,并執(zhí)行下面的事件處理方法:
login方法的返回值取決于不同的業(yè)務(wù)邏輯,這里暫不討論。類User 是一個(gè) backing bean 它的某些屬性值和頁(yè)面上的某些輸入控件的值相關(guān)聯(lián)。 而該類會(huì)在 faces-config.xml 中有相應(yīng)的定義。
上面的例子中僅有 "success"、"failure" 等返回值,但是,實(shí)際應(yīng)用中,你的程序可能要求你能操縱JSF組件、業(yè)務(wù)模型對(duì)象、添加消息等等。甚至是 實(shí)現(xiàn)頁(yè)面的直接跳轉(zhuǎn)、輸出某個(gè)適當(dāng)響應(yīng)、和數(shù)據(jù)庫(kù) EJB 等組件 服務(wù)等實(shí)現(xiàn)交互 等等復(fù)雜的功能。動(dòng)作監(jiān)聽(tīng)器根據(jù)方法的返回值和導(dǎo)航系統(tǒng)中的定義來(lái)決定頁(yè)面跳轉(zhuǎn)。
當(dāng)你需要執(zhí)行業(yè)務(wù)邏輯又不需要頁(yè)面的跳轉(zhuǎn)的時(shí)候,你可以給組件綁定一個(gè) 動(dòng)作監(jiān)聽(tīng)器方法(action listener method),有別于動(dòng)作方法(action method),該方法除了可以綁定事件,還可以操縱組件。我們看下面的例子:
<h:commandButton id="redisplayButton" type="submit" value="Redisplay" actionListener="#{loginForm.doIt()}" />
和前面的例子一樣,當(dāng)用戶點(diǎn)擊該按鈕,有事件產(chǎn)生。但是,這次,動(dòng)作監(jiān)聽(tīng)器方法得到執(zhí)行,而不是先前的動(dòng)作方法。
該方法會(huì)改變先前點(diǎn)擊的按鈕的標(biāo)題,雖然這并沒(méi)有多少用處。但重要的是,這個(gè)方法的簽名。他接受了一個(gè) ActionEvent 類的參數(shù),并沒(méi)有返回值。這,就是 action listener method 和 action method 的主要區(qū)別!在前面的方法執(zhí)行完以后,頁(yè)面會(huì)被刷新,以顯示效果。
通常,用 action listener method 方法來(lái)執(zhí)行對(duì)當(dāng)前頁(yè)面的改變。比方說(shuō) value-changing 事件等,你也可以用一個(gè)接口來(lái)實(shí)現(xiàn),然而,在大多數(shù)情況下,用 backing bean 中的方法來(lái)實(shí)現(xiàn)就已經(jīng)足夠了。
There are two types of action listeners:those that affect navigation,and those that don't.Action listeners that affect navigation typically perform some processing and then return a logical outcome that is used by JSF's navigation system to select the next page(which may actually be the same page that's currently being viewed). Listeners that don't affect navigation usually manipulate components in the current view,or perform some processing that changes model object or backing bean properties,but doesn't alter the current page the user is accessing.
Technically,all navigation is handled by a single action listener.This listener automaatically handles any action events fired by a component,so it doesn't need to be registered manually.By default,this action listener delegates all of its work to action methods in your backing beans,so you can have different action methods handle different parts of your application.Typically,most of your application logic will be located in these methods.(The action listener is pluggable,so it's possible to replace it with one that doesn't use action methods at all.)
When a component fires an action event,this default action listener determines its outcome string---"success"or"failure",for example.There are two basic
types of outcomes:static and dynamic.Static outcomes are hardcoded string that are declared with the component or set in code:
<h:commondButton type="submit" value="Login" action="success" immediate="true"/>
In this example,the static outcome of "success" will be generated when the user clicks on this HtmlCommandButton and generates an action event--no
action method will be called.
Dynamic outcomes are strings returned by action methods themselves---an action method might return a different outcome depending on whateever
application logic it performs.The action listener looks for an action method whenever you use a JSF EL(Expression Language) expression in the action
property.Here's an HtmlCommandButton that exectutes an action method instead:
<h:commandButton type="submit" value="Login" action="#{user.login}"/>
When a user clicks on this button,an action event is fired,and the follwing method is executed in response to the event:
Based on some voodoo application logic,this action method returns an outcome of either "success" or "failure" . User is a backing bean whose properties are wired up to input control values on the page,and is configured via the Managed Bean Creation facility.
My example has voodoo logic,but your action methods can manipulate JSF components,model objects,or add messages.They can also do other fun tasks,such sa performing a redirect,rendering a response(a graphic or some binary type of data),ading events,and talking to databases,EJB(Enterprise JavaBean) servers,or web services.The action listener uses the outcome of an action method to hook into the navigation system and determine what page to choose next.
When you need to execute application logic that is not associated with navigation you can associate an action listener method with a component.Unlike
action methods,action listener methods have access to the component that fired the event as well.Take a look at this example:
<h:commandButton id="redisplayCommand" type="submit" value="Redisplay" actionListener="#{myForm.doIt}" />
Like the previous example , when a user clicks on this HtmlCommandButton , an action event is fired. This time,however,the action listener method is
executed instead of the action method:
This method changes the value(label) of the button that fired the event---not terribly useful.What's important,however,is its method signature.Instead of accepting an ActionEvent sa a parameter and don't return an outcome at all. After this method executes,the page will be redisplayed,incorporating any changes made by the method.
Usually,you use action listener methods for changes that affect the current view. Like value-change listeners,you can also implement an action listener using a Java interface,although in most cases using a method in an existing backing bean is sufficient.