用多了前端框架很容易忘记各种繁琐的标签,这里记录下ongl:
后台:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
public class Ognl extends BaseAction {
private List<String> testlist=new ArrayList<String>();
public List<String> getTestlist() {
return testlist;
}
public void setTestlist(List<String> testlist) {
this.testlist = testlist;
}
public Map<String, String> getTestmap() {
return testmap;
}
public void setTestmap(Map<String, String> testmap) {
this.testmap = testmap;
}
//顶级的哦!!!不加#可以获取是处于actionContext中的
public String getTestname() {
return testname;
}
public void setTestname(String testname) {
this.testname = testname;
}
private Map<String,String> testmap=new HashMap<String,String>();
private String testname="hello,i am in valuestack";
public String ognl(){
HttpServletRequest request=ServletActionContext.getRequest(); //对应request
HttpSession session=request.getSession();//对应session
ServletContext context=ServletActionContext.getServletContext(); // 对应application
//构造普通的字符串放到三个环境中
request.setAttribute("request_name", "来自request中的name");
context.setAttribute("application_name","来自application中的name");
session.setAttribute("session_name", "来自session中的name");
//构造一个list放到request中
List<String> request_names=new ArrayList<String>();
request_names.add("reqname1");
request_names.add("reqname2");
request_names.add("reqname3");
request.setAttribute("req_names", request_names);
//构造一个map放到rquest中
Map<String,String> request_maps=new HashMap<String,String>();
request_maps.put("request_map1","request_value1");
request_maps.put("request_map2","request_value2");
request_maps.put("request_map3","request_value3");
request.setAttribute("req_maps", request_maps);
//构造list放到session中
List<String> session_names=new ArrayList<String>();
session_names.add("sessionname1");
session_names.add("sessionname2");
session_names.add("sessionname3");
session.setAttribute("session_names", session_names); //放到request中
//构造一个map放到session中
Map<String,String> session_maps=new HashMap<String,String>();
session_maps.put("session_map1","session_value1");
session_maps.put("session_map2","session_value2");
session_maps.put("session_map3","session_value3");
session.setAttribute("session_maps", session_maps);
//赋值到ActionContext值栈中的对象
//ActionContext中的testlist
testlist.add("vs144444");
testlist.add("vs24444");
testlist.add("vs34444");
testlist.add("vs44444444");
//ActionContext中的testmap
testmap.put("mp1", "vsmap1");
testmap.put("mp2", "vsmap2");
testmap.put("mp3", "vsmap3");
testmap.put("mp4", "vsmap5555");
//ActionContext中的score
// testscore.put("语文",95);
// testscore.put("数学",75);
// testscore.put("英语",55);
// testscore.put("生物",65);
// testscore.put("物理",85);
// testscore.put("化学",44);
return SUCCESS;
}
}
前台展示:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>" target="_blank" rel="external nofollow" >
<title>My JSP 'list.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css" target="_blank" rel="external nofollow" >
-->
</head>
<body>
<br><hr>显示来自各个范围中的值<br>
actionContext中的testname:<s:property value="testname" /> <br>
request中的request_name:<s:property value="#request.request_name" /><br>
application中的session_name:<s:property value="#application.application_name" /><br>
session中的session_name:<s:property value="#session.session_name" /><br>
遍历request中的list:request_names<p>
<s:iterator value="#request.req_names">
<s:property /><br>
</s:iterator>
遍历request中的map:request_maps<p>
<s:iterator value="#request.req_maps">
<s:property value="key"/>=<s:property value="value" /><br>
</s:iterator>
遍历session中的list:session_names<p>
<s:iterator value="#session.session_names">
<s:property /> <br>
</s:iterator>
遍历session中的map:session_maps<p>
<s:iterator value="#session.session_maps">
<s:property value="key"/>=<s:property value="value" /><br>
</s:iterator>
遍历ActionContext中的testlist=========================<p>
<s:iterator value="testlist" >
<s:property /><br>
</s:iterator>
遍历actionContext中的testmap<p>
<s:iterator value="testmap">
<s:property value="key"/>=<s:property value="value" /><br>
</s:iterator>
直接遍历list<p>
<s:iterator value="{'第一年','第二年','第三年'}" >
<s:property /><br>
</s:iterator>
直接遍历map <p>
<s:iterator value="#{'luo1':'罗先桥1','luo2':'罗先桥2','luo3':'罗先桥3'}">
<s:property value="key" />:<s:property value="value"/><br>
</s:iterator>
遍历actionContext中的testscore这个map<p>
<s:iterator value="testscore">
<s:property value="key"/>:
<s:if test="value<60">
不及格<br>
</s:if>
<s:elseif test="value>=60 && value<75">
及格<br>
</s:elseif>
<s:elseif test="value>=75 && value<85">
良好<br>
</s:elseif>
<s:else>
优秀<br>
</s:else>
</s:iterator>
</body>
</html>