1、Servlet是单实例多线程,也就是只用一个Servlet实例
Action是多实例多线程。在整合Spring时默认是单实例多线程的,需要修改配置。
2、表单数据
EL表达式
<span style="font-size:18px;"><input type="text" name="username" value="${username}"/></span>
struts2标签库
<span style="font-size:18px;"><input type="text" name="username" value="<s:property value="username"/>"/></span>
3、访问ActionContext中的属性,注意一定要在属性名前加“#”
<span style="font-size:18px;">public String addInput(){
Date modify = new Date();
ActionContext.getContext().put("modify", modify);
System.out.println("进入添加页面");
return "addInput";
}</span>
<span style="font-size:18px;">modify:<s:property value="#modify"/></span>
4、ServletActionContext是使用最原始的Servlet,获取Servlet API的所有对象,包括Request等
访问request对象的方法
<span style="font-size:18px;">modify:<s:property value="#request.birthday"/></span>
就是先获取request对象,然后再获取request中的属性值
5、利用ActionContext对象存取Servlet API中的数据(ActionContext的生命周期和request是一样的,所以没必要非得使用Servlet API中的request。使用ActionContext还可以使用Session对象等)
① #request.birthday == #request['birthday']
② 通过attr属性访问request、session和application中的属性
例如 #attr.birthday == #attr['birthday']
6、视图传递给Action复杂参数(即传递对象)
jsp
<span style="font-size:18px;"><s:property value="user.username"/></span>
<span style="font-size:18px;">private User user;
public void setUser(User user){
this.user = user;
}
public User getUser{
return user;
}</span>
这样在客户端写成user.username在客户端会自动将username参数自动赋给user对象。
在多个复杂对象中依然可以使用这种方式,(user.address.homeAddress)
7、Action传递参数给视图(最好使用OGNL表达式,OGNL表达式比EL强大的多)
① 取的时候和赋值的时候差不多
<span style="font-size:18px;"><s:property value="user.username"/></span>
② 将获取的属性对象在Action类中使用ActionContext将对象传递过来。使用<span style="font-size:18px;"><s:property value="#user.username"/>
//获取正常对象的属性值
<pre name="code" class="html"><s:property value="#session['user'].username"/></span>
//获取session中对象的属性值8、传递集合对象(OGNL在赋值和获取值时使用的是get和set方法)
<span style="font-size:18px;"> <s:iterator value="#list" var="user"> //使用栈,一个一个取对象
username:<s:property value="username"/> //迭代出来的User对象的username
username:<s:property value="#user.username"/>
访问外部对象使用root[i]
</s:iterator></span>