Hiển thị các bài đăng có nhãn style.. Hiển thị tất cả bài đăng
Hiển thị các bài đăng có nhãn style.. Hiển thị tất cả bài đăng

Thứ Hai, 30 tháng 12, 2013

Theme trong Spring MVC

Một ứng dụng web đôi khi chúng ta cần thay đổi lại theme của giao diện, ví dụ như color, image, style..
Spring hỗ trợ thay đổi giao diện bằng interface org.springframework.ui.context.ThemeSource và load theme được định nghĩa trong resources bằng org.springframework.ui.context.support.ResourceBundleThemeSource
trong ví dụ này ta có 2 theme là default và blue, hiển thị sẽ như sau:
default


blue
và 2 flie css 
blue.css
body{
background-color:#5cf;
}
default.css
body{
 background-color:#fff;
}

Ta sẽ tạo 2 file properties trong thư mục scr/main/resources/themes
blue.properties
theme=resources/css/themes/blue.css
default.properties
theme=resources/css/themes/default.css
Các file này sẽ chỉ ra style nào được áp dụng qua tham số key = theme
Điều quan trọng nhất là cấu hình cho spring xử lý như mong muốn của ta
vào file /WEB-INF/spring/appServlet/servlet-context.xml
thêm vào đoạn config sau:

   
  
   
    
  
 
 
 
  
   
  
  
 
Trong đoạn config

   
  
   
    
   
ta khai báo bean của lớp org.springframework.web.servlet.theme.ThemeChangeInterceptor để có thể xử lý theme, khi người dụng truyền vào paramerer có name là "theme" bean "themeSource"
 
 
 
   
sẽ load file resource từ forder themes với basenamePrefix = ""
Bạn có thể sửa lại tên file theo prefix nào đó ví dụ: xxx_blue.properties thì bạn sẽ khai báo là basenamePrefix = "xxx" Bean "themeResolver"
   
  
  
sẽ lưu theme hiện tại vào cookie, ngoài ra khai báo theme default là "default" (tùy chọn)
Cuối cùng trong file jsp ta sẽ cấu hình như sau:
createProduct.jsp:
diepviends blog
" type="text/css"/>

diepviends blog
" type="text/css"/>

Source code:
https://www.mediafire.com/?gz4eq9u94pfqt03

Thks & rgds!