[size=large]一、由于struts2框架的Action在值栈中,而值栈又是ognl的根,因此引用Action的属性 可以省略“[color=darkred]#[/color]”标记。如下所示:
1.User.java中有username和userpwd两个属性
2.ListUserAction.java代码
[color=green]package org.wuming.action;
import java.util.List;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("unchecked")
public class ListUserAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private List users;
public List getUsers() {
return users;
}
public void setUsers(List users) {
this.users = users;
}
public String execute() throws Exception {
setUsers(userService.findAllUser());
return SUCCESS;
}
}[/color]
2.listUser.jsp代码片段
<s:iterator value="users">
<s:property value="[color=red]username[/color]"/>
</s:iterator>
二、strust2还提供了一些命名对象,这些对象并没有保存在值栈中,而是保存在了ActionContext中,访问这些对象(Map类型的)需要使用“[color=darkred]#[/color]”标记,如下所示:
ListUserAction.java代码:
[color=green]package org.wuming.action;
import java.util.Map;
import org.wuming.service.UserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class ListUserAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private UserService userService;
public void setUserService(UserService userService) {
this.userService = userService;
}
@SuppressWarnings("unchecked")
public String execute() throws Exception {
Map session = (Map) ActionContext.getContext().getSession();
session.put("list", userService.findAllUsers());
return SUCCESS;
}
}[/color]
listUser.jsp代码片段:
<s:iterator id="sl" [color=red]value="#session.list[/color]">
<s:property [color=darkred]value="#sl.username"/[/color]>
</s:iterator>[/size][/size]
1.User.java中有username和userpwd两个属性
2.ListUserAction.java代码
[color=green]package org.wuming.action;
import java.util.List;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("unchecked")
public class ListUserAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private List users;
public List getUsers() {
return users;
}
public void setUsers(List users) {
this.users = users;
}
public String execute() throws Exception {
setUsers(userService.findAllUser());
return SUCCESS;
}
}[/color]
2.listUser.jsp代码片段
<s:iterator value="users">
<s:property value="[color=red]username[/color]"/>
</s:iterator>
二、strust2还提供了一些命名对象,这些对象并没有保存在值栈中,而是保存在了ActionContext中,访问这些对象(Map类型的)需要使用“[color=darkred]#[/color]”标记,如下所示:
ListUserAction.java代码:
[color=green]package org.wuming.action;
import java.util.Map;
import org.wuming.service.UserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class ListUserAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private UserService userService;
public void setUserService(UserService userService) {
this.userService = userService;
}
@SuppressWarnings("unchecked")
public String execute() throws Exception {
Map session = (Map) ActionContext.getContext().getSession();
session.put("list", userService.findAllUsers());
return SUCCESS;
}
}[/color]
listUser.jsp代码片段:
<s:iterator id="sl" [color=red]value="#session.list[/color]">
<s:property [color=darkred]value="#sl.username"/[/color]>
</s:iterator>[/size][/size]