Jfinal study note

 一、extends JFinalConfig 實現html

                                   configConstant(Constants me) :spring

開發模式常量 devMode 的配置,默認視數據庫

圖類型 ViewType 的配置,json

me.setDevMode(true);app

me.setViewType(ViewType.JSP);url

 二、configRoute(Routes me) 實現HelloController.index() 方 法htm

http://localhost/hello/methodName 將 訪 問 到對象

HelloController.methodName()blog

public void configRoute(Routes me) {繼承

me.add("/hello", HelloController.class); //map 集合

}

JFinal 路由規則以下表:

url  組成  訪問目標

controllerKey  YourController.index()

controllerKey/method  YourController.method()

controllerKey/method/v0_v1  YourController.method(),所帶 url 參數值爲:v0_v1

controllerKey/v0_v1  YourController.index(),所帶 url 參數值爲:v0_v1

下劃線「_」來分隔多個

三、 configPlugin (Plugins me)配置數據路徑

四、onfigInterceptor (Interceptors me) 攔載器、

在此處配置的攔截器將會對全部的請求進行攔截,除非使用

arp.addMapping("user", User.class);

arp.addMapping("article", Article.class);

ActiveReceord 中定義了

addMapping(String tableName, Class<? extends Model> modelClass>)方法,該方法

創建了數據庫表名到 Model 的映射關係。

public class User extends Model<User> {

public static final User dao = new User();

}

以上代碼中的 User 經過繼承 Model,便當即擁有的衆多方便的操做數據庫

的方法 添加到數據中

如下爲 Model 的一些常見用法: 在那個model裏就查那個對象的值

// 建立name屬性爲James,age屬性爲25的User對象並添加到數據庫

new User().set("name", "James").set("age", 25).save();

// 刪除id值爲25的User

User.dao.deleteById(25);

// 查詢id值爲25的User將其name屬性改成James並更新到數據庫

User.dao.findById(25).set("name", "James").update();

// 查詢id值爲25的user, 且僅僅取name與age兩個字段的值

User user = User.dao.findById(25, "name, age");

// 獲取user的name屬性

String userName = user.getStr("name");

// 獲取user的age屬性

Integer userAge = user.getInt("age");

// 查詢全部年齡大於18歲的user

List<User> users = User.dao.find("select * from user where age>18")

// 分頁查詢年齡大於18的user,當前頁號爲1,每頁10個user

Page<User> userPage = User.dao.paginate(1, 10, "select *", "from user

where age > ?", 18)





@ClearInterceptor 在 Controller 中清除 

5 Action

Controller 以及在其中定義的 public 無參方法稱爲一個 Action。Action 是請

求的最小單位。Action 方法必須在 Controller 中聲明,該方法必須是 public 可見

性且沒有形參。

public class HelloController extends Controller {

public void index() {

renderText("此方法是一個action");

}

public void test() {

renderText("此方法是一個action");

}

}

以上代碼中定義了兩個 Action: HelloController.index()、 HelloController.test()。

在Controller中提供了getPara系列方法setAttr方法以及render系列方法供Action

使用getPara 系列方法HttpServletRequest.getParameter(String name)的封裝 取值

列方法是去獲取

urlPara中所帶的參數值。 getParaMap與getParaNames分別對應HttpServletRequest

的 getParameterMap 與 getParameterNames。

getPara 使用例子:

方法調用  返回值

getPara(」title」)  返回頁面表單域名爲「title」參數值

getParaToInt(」age」)  返回頁面表單域名爲「age」的參數值並轉爲 int 型

getPara(0)  返回 url 請求中的 urlPara 參數的第一個值,如

http://localhost/controllerKey/method/v0_v1_v2  這

個請求將返回」v0」

getParaToInt(1)  返回url請求中的urlPara參數的第二個值並轉換成

int 型,如 http://localhost/controllerKey/method/2_5_9 這

個請求將返回 5

3.4 setAttr 方法

setAttr(String, Object)轉調了 HttpServletRequest.setAttribute(String, Object),

該方法能夠將各類數據傳遞給 View 並在 View 中顯示出來。

render 路徑問題

render(」test.html」)  渲染名爲 test.html 的視圖,該視圖的全路徑

爲」/path/test.html」

render(」/other_path/test.html」) 渲染名爲 test.html 的視圖,該視圖的全路徑

爲」/other_path/test.html」,即當參數以」/」開頭時將

採用絕對路徑。

renderJson()  將全部經過 Controller.setAttr(String, Object)設置

的變量轉換成 json 數據並渲染。

renderJson(「users」 userList)  以」users」爲根,僅將 userList 中的數據轉換成 json

數據並渲染。

renderJson(new

String[]{「user」, 「blog」})

僅將 setAttr(「user」, user)與 setAttr(「blog」, blog)設

置的屬性轉換成 json 並渲染。使用 setAttr 設置的

其它屬性並不轉換爲 json。

Interceptor  應用 至關於 spring 註解

相關文章
相關標籤/搜索