[color=red][size=large]第一种:以ActionContext类访问ServletAPI[/size][/color]
示例代码: [color=red](注:request,sesssion,application用MaP集合存)
页面:用<s:property value="#request.loginCity"/> 抬头导入struts标签库
[color=red][size=x-large]二。使用Ioc方式[/size][/color]
使用哪个对象,则实现哪个接口,实现接口后实现接口的方法,把参数的值赋给成员变量
示例:action代码:
页面如第一方法所示
示例代码: [color=red](注:request,sesssion,application用MaP集合存)
[/color]
public class UserAction extends ActionSupport {
private String username;
private String password;
private Map<String ,Object> request;
private Map<String ,Object> session;
private Map<String ,Object> application;
public String login(){
//获取对象
ActionContext ac=ActionContext.getContext();
[color=red][size=large]request=(Map<String, Object>) ac.get("request");[/size][/color]//request对象必须用参数“request”获取
session=ac.getSession();
application=ac.getApplication();
//保存数据
request.put("loginCity", 北京");
session.put("username", username);
application.put("online", "在线人数:XXX");
return SUCCESS;
}
//setter/getter方法省略
}
页面:用<s:property value="#request.loginCity"/> 抬头导入struts标签库
[color=red][size=x-large]二。使用Ioc方式[/size][/color]
使用哪个对象,则实现哪个接口,实现接口后实现接口的方法,把参数的值赋给成员变量
示例:action代码:
public class UserAction extends ActionSupport implements RequestAware,SessionAware,ApplicationAware{
private String username;
private String password;
private Map<String ,Object> request;
private Map<String ,Object> session;
private Map<String ,Object> application;
public String login(){
//保存数据
request.put("loginCity", 北京");
session.put("username", username);
application.put("online", "在线人数:XXX");
return SUCCESS;
}
public void setRequest(Map<String, Object> request) {
this.request=request;
}
public void setSession(Map<String, Object> session) {
this.session=session;
}
public void setApplication(Map<String, Object> application) {
this.application=application;
}
//setter/getter方法
}
页面如第一方法所示