1. Trang chủ
  2. » Công Nghệ Thông Tin

Spring MVC EL JSTL

36 232 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 36
Dung lượng 2,54 MB

Nội dung

Nắm vứng kỹ thuật lập trình giao diện trong JSP Expression Language (EL) Java Standard Tag Library (JSTL) EL Truy xuất attribute trong các scope Truy xuất thuộc tính bean Truy xuất phần tử mảng và tập hợp Truy xuất phần tử của map Truy xuất tham số, cookie JSTL Core Format Function

LẬP TRÌNH JAVA BÀI 4: EL & JSTL MỤC TIÊU  Nắm vứng kỹ thuật lập trình giao diện JSP  Expression Language (EL)  Java Standard Tag Library (JSTL) EXPRESSION LANGUAGE  EL rút ngắn tuyệt vời việc viết mã làm việc với attribute đặt scope (page, request, session application)  EL giới thiệu phiên JSP 2.0  Trong phần nghiên cứu sử dụng EL để truy xuất Attribute scope Thuộc tính bean Phần tử Collection Phần tử Map Tham số, cookie header ${biểu thức} EXPRESSION LANGUAGE  Cú pháp: ${}  biểu thức cho giá trị kết xuất vị trí đặt biểu thức EL Trong biểu thức có thể chứa attribute, parameter, cookie hay header  Vídụ: ${salary*2}: nhân đôi giá trị attribute salary kết xuất giá trị biểu thức ${sessionScope[‘salary']}: kết xuất giá trị attribute salary đặt session ${param.salary}: kết xuất giá trị tham số salary VÍ DỤ EL Controller @RequestMapping(“/el/demo1”) public String sayHello(ModelMap model, HttpSession session){ session.setAttribute(“name”, “Tèo”); model.addAttribute(“salary”, 2000) } JSP
  • name: ${name}
  • salary: ${salary}
  • requestScope.name: ${requestScope.name}
  • requestScope.salary: ${requestScope.salary}
  • sessionScope.name: ${sessionScope.name}
  • sessionScope.salary: ${sessionScope.salary}
  • Why? SCOPE API  Như biết JSP có scope chia sẻ liệu Page: pageScope Request: requestScope Session: sessionScope Application: applicationScope  Scope API gồm setAttribute(name, value) getAttribute(name) removeAttribute(name) getAttributeNames() Các phương thức hữu dụng với viết mã Java Tuy nhiên JSP, theo phong cách lập trình viên sử dụng EL JSTL TRUY XUẤT ATTRIBUTE  Các biểu thức EL sau kết xuất attribute x đặt scope cụ thể ${pageScope[‘x’]} ${pageScope.x} ${requestScope[‘x’]} ${requestScope.x} ${sessionScope[‘x’]} ${sessionScope.x} ${applicationScope[‘x’]} $ {applicationScope.x}  Biểu thức EL sau truy tìm kết xuất giá trị attribute message tất Scope ${message} Trình tự tìm kiếm attribute message pageScope->requestScope->sessionScope->applicationScope Nếu tìm thấy dừng lại, ngược lại cho giá trị rỗng TRUY XUẤT THUỘC TÍNH CỦA BEAN  Nếu attribute bean EL cho phép truy xuất thuộc tính cách đơn giản  Lớp JavaBean lớp Phải khai báo public Có Constructor mặc định không tham số Đọc/ghi liệu thông qua phương thức getter/setter  Cú pháp truy xuất thuộc tính bean: ${bean.property} Kết xuất giá trị thuộc tính property attribute bean Có nghĩa kết xuất kết phương thức bean.getProperty()  Vídụ: ${student.mark} ~ xuất student.getMark() VÍ DỤ TRUY XUẤT THUỘC TÍNH BEAN Controller @RequestMapping(“/el/demo2") public String demo2(ModelMap model) { Student student = new Student("Phương", 10.0, "APP"); model.addAttribute("student", student); return "el/demo2"; } JSP
  • name: ${student.name}
  • mark: ${student.mark}
  • TRUY XUẤT MẢNG VÀ TẬP HỢP  Nếu attribute mảng tập hợp EL cho phép sử dụng số để truy xuất phần tử Controller @RequestMapping(“/el/demo3") public String demo3(ModelMap model) { List list = new ArrayList(); list.add("Phương"); list.add("Cường"); model.addAttribute("items", list); return "el/demo3"; } JSP
  • ${items[0]}
  • ${items[1]}
  • THẺ  Cú pháp Nội dung  Diễn giải Duyệt phần tử từ vị trí begin đến end tập hợp items Var nắm phần tử varStatus nắm thơng tin trạng thái phần tử VÍ DỤ -  Vòng lặp kết xuất Hello Hello Hello Hello Hello Hello World World World World World World VÍ DỤ -  Ví dụ kết xuất thông tin phần tử từ 10 đến 25 tập hợp products Mỗi phần từ xuất vị trí tên sản phẩm: 
  • Vị trí: 10
  • Tên hàng hóa: Abc
  • Vị trí: 11
  • Tên hàng hóa: Xyz
  • … VÍ DỤ - &  sử dụng để tạo attribute tương tự .setAttribute(“name”, “value”) java  Cú pháp  value  được sử dụng để xóa attribute tương tự .removeAttribute(“name”) java  Cú pháp  DEMO Giải thích + jstl/core-if.htm + jstl/core-choose.htm + jstl/core-foreach.htm THƯ VIỆN ĐỊNH DẠNG  Định dạng số   Value: số cần định dạng  Type: kiểu định dạng Ví dụ  Định dạng thời gian   Value: thời gian cần định dạng  Pattern: mẫu định dạng thời gian Ví dụ VÍ DỤ ĐỊNH DẠNG Bean Class Controller Class VÍ DỤ ĐỊNH DẠNG DEMO Giải thích + jstl/format.htm THƯ VIỆN HÀM  JSTL cung cấp tập hàm hỗ trợ xử lý cho biểu thức EL Các hàm tập trung xử lý chuỗi tập hợp  Ví dụ 1: ${fn:substring(0, 100, description)}… Ví dụ hiển thị 100 ký tự attribute description  Ví dụ 2: Người họ Nguyễn Ví dụ kiểm tra attribute fullname có phải bắt đầu chữ “Nguyễn ” hay không THƯ VIỆN HÀM THƯ VIỆN HÀM TỔNG KẾT NỘI DUNG BÀI HỌC  EL Truy Truy Truy Truy Truy xuất xuất xuất xuất xuất  JSTL Core Format Function attribute scope thuộc tính bean phần tử mảng tập hợp phần tử map tham số, cookie Cảm ơn ... phần tử VÍ DỤ -  Vòng lặp kết xuất Hello Hello Hello Hello Hello Hello World World World World World World VÍ DỤ - ... el/ login.htm LẬP TRÌNH JAVA PHẦN JAVA STANDARD TAG LIBRARY  JSTL thư viện thẻ chuẩn bổ sung với mục đích tối ưu lập trình giao diện JSP  Các thư viện cần thiết cho JSTL gồm jstl- api.jar jstl- impl.jar... trị tham số salary VÍ DỤ EL Controller @RequestMapping(“ /el/ demo1”) public String sayHello(ModelMap model, HttpSession session){ session.setAttribute(“name”, “Tèo”); model.addAttribute(“salary”,

    Ngày đăng: 05/01/2020, 18:34

    TỪ KHÓA LIÊN QUAN

    w