用多了前端架構很容易忘記各種繁瑣的标簽,這裡記錄下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>