一.访问域对象(javabean对象)
例子:
1.首先我们先建立一个域对象:
package bean;
public class UserModel {
private String userId;
private String name;
public String getUserId()
{
return this.userId;
}
public void setUserId(String userId)
{
this.userId=userId;
}
public String getName()
{
return this.name;
}
public void setName(String name)
{
this.name=name;
}
}
2.然后在action的java代码中引用它:
package cn.javass.hello.struts2impl.action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import bean.UserModel;
public class HelloWorldAction extends ActionSupport{
private String account;
private String password;
private String submitFlag;
private UserModel um=new UserModel();//定义一个名为um的UserModel类型的对象,
//注意um是与jsp通信的关键
public UserModel getUm()
{
return um;
}
public void setUm(UserModel um)
{
this.um=um;
}
public String execute() throws Exception{
this.businessExecute();
System.out.println("you input userId="+um.getUserId());
System.out.println("you input userName="+um.getName());
um.setUserId("in action fixed userId");
um.setName("in action fixed name");
return this.SUCCESS;
}
public void businessExecute()
{
System.out.println("用户输入的参数为==="+"account="+account+"password="+password+"submitFlag="+submitFlag);
}
public String getAccount()
{
return account;
}
public void setAccount(String account)
{
this.account=account;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password=password;
}
public String getSubmitFlag()
{
return submitFlag;
}
public void setSubmitFlag(String submitFlag)
{
this.submitFlag=submitFlag;
}
}
3.jsp视图编码:
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="/struts2/helloworldAction.action" method="post">
<input type="hidden" name="submitFlag" value="login"/>
Account :<input type="text" name="account"/><br/>
PassWord:<input type="password" name="password"/><br/>
userId:<input type="text" name="um.userId"><br>
userName:<input type="text" name="um.name"><br/>
<input type="submit" value="submit">
</form>
</body>
</html>
welcome.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%@taglib prefix="s" uri="/struts-tags" %>
欢迎账号为<s:property value="account"/>的朋友来访<br/>
<s:property value="um.userId"/><br/>
<s:property value="um.name"/>
</body>
</html>
结果:
注意:getter/setter方法优先。就是说属性对应的getter/setter方法,在OGNL对应的时候先对应方法,若对应上方法了,就不会对应属性了。否则对应属性(对应属性的要求是该属性必须是public范围的)
二.访问List或数组
通过OGNL访问数组或List属性的时候,由于他们的多元素特性,所以我们只需指明索引即可访问。
List例如:
< s:property value="umList[0].userId"/>
但要注意,在OGNL访问数组类型之前,我们必须要初始化索引所对应的对象,最简单的方法是在定义的时候直接初始化。
三.访问Map
通过OGNL访问Map时,通常的格式为Map名称[‘键的名称’]
例如:
< s:property value="userMap['umtest'].userId"/>
表示的是在键名为umtest的userMap对象中访问getUserId方法。
四.组合使用
通过前面的介绍可以发现OGNL可以访问不同的对象,属性,方法,那么我们可能会想,可不可以把这些复杂的对象结构组合起来让OGNL去访问呢?
假如在Session中,有一个List作为Session的属性,对应的key是“users”,List里面放的全是UserModel类型的对象,现在要访问其中的第三个元素的userId属性,应该怎么办?
我们需要一层一层的来解决:
1.要获取一个Session中的key为“users”的List,对应的OGNL应为#session.users
2.要操作这个List的第三个元素,对应的OGNL应为#session.users [2].
3.要操作这个对象的userId属性,对应的OGNL应为#session.users [2].userId.
下面我们来实践验证一下:
1.首先写一个UserModel.java
package bean;
public class UserModel {
private String userId;
private String name;
public String getUserId()
{
return this.userId;
}
public void setUserId(String userId)
{
this.userId=userId;
}
public String getName()
{
return this.name;
}
public void setName(String name)
{
this.name=name;
}
}
2.然后写一个Session.java
package bean;
import java.util.ArrayList;
import java.util.List;
public class Session {
private List<UserModel> users=new ArrayList<UserModel>();
public void setUsers(UserModel user)
{
user.setUserId("1242524");
user.setName("文牧之");
this.users.add(user);
}
public List getUser()
{
return this.users;
}
}
3.然后Action代码:
package cn.javass.hello.struts2impl.action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import bean.Session;
import bean.UserModel;
public class HelloWorldAction extends ActionSupport{
private String account;
private String password;
private String submitFlag;
private Session session=new Session();
private UserModel user=new UserModel();
public Session getSession()
{
return this.session;
}
public void setSession(Session session)
{
this.session=session;
}
public String execute() throws Exception{
session.setUsers(user);
this.businessExecute();
return this.SUCCESS;
}
public void businessExecute()
{
System.out.println("用户输入的参数为==="+"account="+account+"password="+password+"submitFlag="+submitFlag);
}
public String getAccount()
{
return account;
}
public void setAccount(String account)
{
this.account=account;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password=password;
}
public String getSubmitFlag()
{
return submitFlag;
}
public void setSubmitFlag(String submitFlag)
{
this.submitFlag=submitFlag;
}
}
4.最后在jsp里使用它
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="/struts2/helloworldAction.action" method="post">
<input type="hidden" name="submitFlag" value="login"/>
Account :<input type="text" name="account"/><br/>
PassWord:<input type="password" name="password"/><br/>
<input type="submit" value="submit">
</form>
</body>
</html>
welcome.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%@taglib prefix="s" uri="/struts-tags" %>
<s:property value="session.user[0].name"/><br/>
<s:property value="session.user[0].userId"/>
</body>
</html>
结果如下: