EL表示式呼叫後臺方法並傳遞引數
阿新 • • 發佈:2018-11-13
嘗試獲取後臺物件中帶引數的get方法返回的屬性值時,發現 J2EE6 開始支援EL表示式帶引數的呼叫後臺方法。
語法格式為:
${物件名.方法名(引數)};
注意此處的方法名是方法全名,EL表示式並不會幫我們自動按照屬性名進行首字母大寫並在開頭拼接get三個字元來尋找相應的get方法獲取屬性值。
示例:
前臺程式碼為:
<html> <body> // 不帶引數的獲取屬性值方法 <p>${elTest.testProp}</p> // 帶引數的獲取屬性值方法 <p>${elTest.getTestProp("123")}</p> </body> </html>
後臺物件為:
public class ELTest {
private String testProp;
public String getTestProp() {
return testProp;
}
public String getTestProp(String test) {
return test;
}
public void setTestProp(String testProp) {
this.testProp = testProp;
}
}
Controller程式碼:
@RequestMapping("/test")
public ModelAndView doTest() {
ModelAndView mav = new ModelAndView();
mav.addObject("elTest", new ELTest());
mav.setViewName("test");
return mav;
}