(先定義一個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;
}
}
在jsp頁面中用以下四種方法可以實現:
1. <s:property value="username"/>
2. ${username}
${}為EL表達式
3. <s:property value="#request.username"/> (#session. #application.類似)
(1)#相當于ActionContext().getContext
ActionContext 中的主要對象: parameters, attr, request, session, application
如:
ActionContext().getContext().getSession().get(“kkk”) |
#session.kkk |
ActionContext().getContext().get(“person”) |
#person |
(2)
Map
map.get(“foo”) |
map[‘foo’] 或 map.foo |
map.get(1) |
map[1] |
Map map = new HashMap() map.put(“k1”, “v1”); map.put(“k2”,”v2”); |
#{“k1 |
(3)篩選 與 投影
篩選 collection.{? expr }: #this 代表當前循環到的object
投影 collection.{ expr }
children.{name} |
(投影)得到 Collection<String> names, 只有孩子名字的list |
children.{?#this.age>2} |
(篩選)得到 collection<Person> age>2的記錄 |
children.{?#this.age<=2}.{name} |
先篩選再投影 |
children.{name+’->’+mother.name} |
(篩選)得到元素為 str->str 的集合 |
總結:
#的三種用途
1. 引用 ActionContext 中的object #person
2. 動態創建map #{1:’a’, 2:’b’}
3. 對集合進行篩選(#this) #this
4. <s:property value="%{username}"/>