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 ;
最后是設定JSP文件的編碼,
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
或者是用個Filter:
經過這樣三個步驟,就不存在亂碼問題了。
另,不推薦用 MySQL-Front 這個客戶端程序管理UTF-8編碼的數據庫。如果是GBK編碼還可以考慮,UTF-8則會出現編碼問題,導致程序不能正常工作,強烈推薦 Navicat 。 功能強大,默認使用UTF-8編碼,界面友好、美觀。
當用戶和代表命令或某操作(gesture)等控件發生交互的時候,觸發 動作事件(action event)。產生動作事件的組件也叫做動作源,包括了按鈕、超鏈接等組件。動作事件由動作監聽器(action listener)進行處理。
有兩種典型的監聽器:一種會影響到頁面間的導航(navigation),另一種不會影響到導航。影響到頁面間導航的監聽器主要是處理一些業務邏輯并返回幾個業務邏輯結果,JSF的導航系統將會根據這個來選擇恰當的下一個頁面(譯注:并非傳統所說的TRUE/FALSE這兩種“邏輯”結果,這里講的是“業務邏輯”,可以是任意的有代表意義的結果。)不影響導航的事件監聽器用來操作當前頁面內的組件,或者是進行一些業務模型上的改變,或者是修改backing bean的屬性等,并不會出現在頁面間的跳轉。
在技術上,所有頁面間導航都是由一個單一的動作監聽器來處理。這個監聽器自動處理組件所觸發的所有事件(event),因此,該監聽器并不需要手動地注冊/綁定(register)。缺省情況下,這個監聽器會將所有的工作交由你制定的backing bean中的某個方法(method)來處理。因此,你可以用不同的方法來處理你程序中的各個業務部分。典型地,你的大多數業務邏輯都將會交由這些方法來處理。
當某個組件觸發一個事件的時候,缺省的監聽器就決定了該事件的處理結果,該結果用一個字符串(string)來表示,比方說:"success"、"failure" 等。這個結果也可以分為靜態和動態兩類。靜態的是在編碼的時候就已經定義好了的例如:
<h:commandButton type="submit" value="Login" action="success" immadiate="true" />
在這個例子當中,當用戶點擊這個標為Login的按鈕的時候,會產生 "success" 這個業務結果并同時觸發該按鈕被點擊這個事件,但是,并沒有相應的方法來處理這個事件。
而動態的結果就是由處理該事件的方法所返回的,一個字符串。一個方法可能會返回多個不同的結果,這取決于業務邏輯。動作監聽器會自動查找你在該組件的 action 屬性中指定的方法,并執行。我們用 EL(expression language) 來指定該屬性值。下面是一個 HtmlCommandButton 的實際例子:
<h:commandButton type="submit" value="Login" action="#{user.login}" />
當用戶點擊該按鈕,一個代表該按鈕被點擊的事件產生,并執行下面的事件處理方法:
login方法的返回值取決于不同的業務邏輯,這里暫不討論。類User 是一個 backing bean 它的某些屬性值和頁面上的某些輸入控件的值相關聯。 而該類會在 faces-config.xml 中有相應的定義。
上面的例子中僅有 "success"、"failure" 等返回值,但是,實際應用中,你的程序可能要求你能操縱JSF組件、業務模型對象、添加消息等等。甚至是 實現頁面的直接跳轉、輸出某個適當響應、和數據庫 EJB 等組件 服務等實現交互 等等復雜的功能。動作監聽器根據方法的返回值和導航系統中的定義來決定頁面跳轉。
當你需要執行業務邏輯又不需要頁面的跳轉的時候,你可以給組件綁定一個 動作監聽器方法(action listener method),有別于動作方法(action method),該方法除了可以綁定事件,還可以操縱組件。我們看下面的例子:
<h:commandButton id="redisplayButton" type="submit" value="Redisplay" actionListener="#{loginForm.doIt()}" />
和前面的例子一樣,當用戶點擊該按鈕,有事件產生。但是,這次,動作監聽器方法得到執行,而不是先前的動作方法。
該方法會改變先前點擊的按鈕的標題,雖然這并沒有多少用處。但重要的是,這個方法的簽名。他接受了一個 ActionEvent 類的參數,并沒有返回值。這,就是 action listener method 和 action method 的主要區別!在前面的方法執行完以后,頁面會被刷新,以顯示效果。
通常,用 action listener method 方法來執行對當前頁面的改變。比方說 value-changing 事件等,你也可以用一個接口來實現,然而,在大多數情況下,用 backing bean 中的方法來實現就已經足夠了。
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.