java - 如何組裝模板(EL技術相關)?
問題描述
如題,在后臺我有一個頁面模板,里面有一些地方要填具體數據,調用一個方法傳入模板文件和上下文對象,然后返回一個組裝好的頁面html代碼 。請問這個用什么技術比較方便實現,考慮了Spring的EL表達式和Java EE的JSTL,覺得并不是很好用。麻煩能給個簡單的例子最好。
模板:
<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <title>Spring EL Test</title></head><body> Hello, ${user.name}!</body></html>
可能調用這樣的方法
String result = parse(File file, Object contextObj); // 得到組裝后數據
組裝后數據:
<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <title>Spring EL Test</title></head><body> Hello, ZhangSan!</body></html>
問題解答
回答1:首先糾正一下,EL表達式并不是Spring提供的,而是JSP 2.x提供的。
所謂的組裝,就是用request.setAttribute()設一個名為user的對象,在jsp中把對象解析出來。
應該這樣就可以了吧:
User user = new User('ZhangSan'); // user.getName() 應該得到'ZhangSan'request.setAttribute('user', user);// 然后是forward到這個jsp
相關文章:
1. mysql - 請問數據庫字段為年月日,傳進的參數為月,怎么查詢那個月所對應的數據2. node.js - win 下 npm install 遇到了如下錯誤 會導致 無法 run dev么?3. javascript - js判斷一個數組是否重復4. 求救一下,用新版的phpstudy,數據庫過段時間會消失是什么情況?5. [python2]local variable referenced before assignment問題6. Python2中code.co_kwonlyargcount的等效寫法7. html - 移動端radio無法選中8. php - 微信開發驗證服務器有效性9. javascript - vue+iview upload傳參失敗 跨域問題后臺已經解決 仍然報403,這是怎么回事啊?10. mysql - 如何在有自增id的情況下,讓其他某些字段能不重復插入
