java web从零单排第十四期《struts2》OGNL的输出应用

上一期中同学们大概了解了什么是OGNL,这一期着重介绍OGNL的应用,说到应用,自然离不开三个符号:# $ %。

1.有关ActionContext的输出

我们可以使用“#”来访问非root对象属性,#相当于ActionContext.getContext();以第十一期的jsp代码为例:

 <s:property value="#session.sessionValue"/>

可以理解成ActionContext.getContext().getSession().getAttribute("sessionValue");

也可以简单地理解为 “#”可以调用ActionContext的put(key,value)放入的数据。

 

2.输出ArrayList中String类型变量

action代码如下:

package controller;

import java.util.ArrayList;

public class test {
	

	private ArrayList listString = new ArrayList();
	

	public ArrayList getListString() {
		return listString;
	}

	public void setListString(ArrayList listString) {
		this.listString = listString;
	}
	public String execute()
	{
		listString.add("niu");
		listString.add("bin");
		listString.add("bin");
		
		return "show";
	}

}

输出数组中的数据:

<%@ page language="java" import="java.util.*,com.opensymphony.xwork2.util.ValueStack" pageEncoding="ISO-8859-1"%>
 <%@ taglib uri="/struts-tags" prefix="s" %>
 <!DOCTYPE body PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 <html>
  <body>
    <s:property value="listString[0]" />
    <br>
    <s:property value="listString[1]" />
    <br>
     <s:property value="listString[2]" />
    <br> 
  </body>
</html>

运行结果:

java web从零单排第十四期《struts2》OGNL的输出应用_第1张图片

 

 

3.输出ArrayList中Bean类型变量实例:

action代码如下:

package controller;

import java.util.ArrayList;

import bean.UserInfo;

public class test {
	

	private ArrayList listBean = new ArrayList();

	public ArrayList getListBean() {
		return listBean;
	}

	public void setListBean(ArrayList listBean) {
		this.listBean = listBean;
	}
	
	public String execute()
	{
		UserInfo userinfo1 = new UserInfo();
		userinfo1.setPassword("aa");
		userinfo1.setUsername("bb");
		
		UserInfo userinfo2= new UserInfo();
		userinfo2.setPassword("cc");
		userinfo2.setUsername("dd");
		listBean.add(userinfo1);
		listBean.add(userinfo2);
		return "show";
	}	
}

类的实体:

package bean;

public class UserInfo {
	
	private String username;
	private String password;
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}

}

jsp代码:

<%@ page language="java" import="java.util.*,com.opensymphony.xwork2.util.ValueStack" pageEncoding="ISO-8859-1"%>
 <%@ taglib uri="/struts-tags" prefix="s" %>
 <!DOCTYPE body PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 <html>
  <body>
    <s:property value="listBean[0].username" />
    <br>
    <s:property value="listBean[0].password" />
    <br>
     <s:property value="listBean[1].username" />
    <br> 
     <s:property value="listBean[1].password" />
    <br>
  </body>
</html>


运行结果:

java web从零单排第十四期《struts2》OGNL的输出应用_第2张图片
4.输出HashMap中String类型变量实例:

action代码如下:

package controller;

import java.util.ArrayList;
import java.util.HashMap;

import bean.UserInfo;

public class test {
	

	private HashMap mapstring = new HashMap();

	
	public HashMap getMapstring() {
		return mapstring;
	}


	public void setMapstring(HashMap mapstring) {
		this.mapstring = mapstring;
	}


	public String execute()
	{
		mapstring.put("1", "niu");
		mapstring.put("2", "jia");
		mapstring.put("3", "bin");
		
		return "show";
	}	
}

jsp代码:

<%@ page language="java" import="java.util.*,com.opensymphony.xwork2.util.ValueStack" pageEncoding="ISO-8859-1"%>
 <%@ taglib uri="/struts-tags" prefix="s" %>
 <!DOCTYPE body PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 <html>
  <body>
    <s:property value="mapstring['1']" />
    <br>
    <s:property value="mapstring['2']" />
    <br>
     <s:property value="mapstring['3']" />
    <br> 
  </body>
</html>


运行结果:


5.同学可以试着自己输出HashMap中Bean类型变量的实例

 

你可能感兴趣的:(java,Web,struts2)