剛過去的項目中使用了一把struts2。由於搭配jsp頁面前台展示而不是我習慣的velocity,因此不免要學一下struts2中的ONGL標籤。。。發現用起來還是很頭疼的。。經常搞不清楚%,#怎麼用。
1.先解決最基礎的溫飽問題:如何把action中的值帶到頁面展示出來
(先定義一個action,有個username欄位,假設value="song"想傳遞到頁面上)
public class Login extends ActionSupport ...{
private String username;
public String execute() throws Exception ...{
return INPUT;
}
public String getUsername() ...{
return username;
}
public void setUsername(String username) ...{
this.username = username;
}
}
下面是頁面上顯示username的一些嘗試:================直接把值顯示出來。。。=====================<br>
1.s:property標籤 <s:property value="username"/>
2.$符號運算式 $...{username}
上面的兩種方式都是可以顯示"song"出來的,如果只用來顯示,推薦$...{username} ,簡單明了。不過$...{}不可以放在struts2內建的<s:xxx > 標籤中混用,會報不支援Variant 運算式異常。
=============== 用#試一下。。。=====================<br>
1.#運算式 #username
失敗,頁面輸出"#username"
2.s:property標籤1: <s:property value="#username"/>
失敗,頁面無輸出
3.s:property標籤2: <s:property value="#request.username"/>
成功,頁面輸出"song"(這種屬性放進了request中)
4.s:property標籤3: <s:property value="#session.username"/>
失敗,頁面無輸出(沒有放到session中)
=============== 用%試一下。。。=====================<br>
1.%運算式 %...{username}
失敗,頁面輸出"%{username}"
2.%運算式 %...{'username'}
失敗,頁面輸出"%{'username'}"
3.s:property標籤4: <s:property value="%{username}"/>
成功,頁面輸出"song"
4.s:property標籤Error4: <s:property value="%{'username'}"/>
失敗,頁面輸出"username"
測試了一大把,最後總結出了4種拿到值得辦法:
<s:property value="username"/>
${username}
<s:property value="#request.username"/> (#session. #application.類似)
<s:property value="%{username}"/>
未完。。。。待續