c:foreach jstl el表示式的用法以及c:foreach中的hidden的用法
阿新 • • 發佈:2019-02-13
>>1.JSTL的用法
<c:forEach var="varLogList" items="${logList}" varStatus="status"> <tr onmouseover="changeto()" onmouseout="changeback()"> <td height="20" bgcolor="#FFFFFF"> <div align="center"> ${status.count } </div> </td> <td height="20" bgcolor="#FFFFFF"> <div align="center"> ${varLogList.date } </div> </td> <td height="20" bgcolor="#FFFFFF" > <div align="center"> ${varLogList.fileName } </div> </td> <td height="20" bgcolor="#FFFFFF"> <div align="center"> ${varLogList.size } </div> </td> <td height="20" bgcolor="#FFFFFF"> <div align="center"> <input type="hidden" id="filePath" name="filePath${status.count }" value="${varLogList.filePath }" /> <input type="hidden" id="filePath" name="fileName${status.count }" value="${varLogList.fileName }" /> <input type="button" class="button4C" value="下 載" onclick="javascript:downfile(this.form, '${status.count }');"/> </div> </td> </tr> </c:forEach>
其中這句程式碼:
<c:forEach var="varLogList" items="${logList}" varStatus="status">
中的items="${logList }"表示直接獲取request中的logList值,在對應的Action方法裡面肯定有這麼一句程式碼:
reuqest.setAttribute("logList", logList);
然後在頁面裡面就可以直接這樣用jstl表示式獲取物件的屬性值:
<td height="20" bgcolor="#FFFFFF"> <div align="center"> ${varLogList.date } </div> </td>
>>2.EL表示式
在jsp檔案中引入實體類,如下所示:
<%@ page import="Config.application.ServiceInstance,Config.application.AppInfo,Config.application.ApplicationGrp" %>
然後在頁面中可以這樣引用值:
<tr class='<%=i%2==0 ? "light" : "" %>' onmouseover="this.className='on_5'" onmouseout=<%=i%2==0 ? "this.className='light'" : "this.className='on_1'" %> > <td nowrap="nowrap"><%=service.name %></td> <td nowrap="nowrap"><%=service.port %></td> <td nowrap="nowrap"><%=service.path %></td> <td nowrap="nowrap"><%=service.startCMD %></td> <td nowrap="nowrap"><%=service.elementId %></td> <td nowrap="nowrap"><%=service.osId %></td> <td id="monitor_td_id_<%=i %>" style="text-align: left"> <script> try{ outPutLimitStr('<%=service.desc %>','monitor_td_id_<%=i %>',75); }catch(e){ $("#monitor_td_id_<%=i %>").text('<%=service.desc %>'); } </script> </td> <td nowrap="nowrap"> <input type="button" class="button4C" value="修改" onclick="javascript:modifyService('<%=service.name %>');"/> <input type="button" class="button4C" value="刪除" onclick="javascript:deleteService(this.form,'<%=service.name %>');"/> </td> </tr>
不過這有個缺點,就是在jsp頁面裡面會有java程式碼。
整個檔案的程式碼如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="Config.application.ServiceInstance,Config.application.AppInfo,Config.application.ApplicationGrp" %>
<%@ include file="/pages/common/global.jsp" %>
<script type="text/javascript">
function modifyService(idName){
var service = encodeURIComponent(idName);
var url = "${contextPath }/serviceAction.do?method=addService";
if(service){
url+= "&serviceName=" + service;
}
retrieveURL(url,'serviceDiv');
$("#chance_search_div").hide();
}
</script>
<html:form action="/serviceAction.do?method=serviceList">
<table class="list_t" id="page_table">
<c:if test="${!empty divPage.data}">
<thead>
<tr>
<th nowrap="nowrap">名稱</th>
<th nowrap="nowrap">埠</th>
<th nowrap="nowrap">目錄</th>
<th nowrap="nowrap">啟動指令</th>
<th nowrap="nowrap">網元編號</th>
<th nowrap="nowrap">作業系統編號</th>
<th>描述</th>
<th nowrap="nowrap">操作</th>
</tr>
</thead>
<tbody>
<%
List serviceList = (List) request.getAttribute("pageServiceList");
if(serviceList!=null&&serviceList.size()>0){
for(int i=0;i<serviceList.size();i++){
ServiceInstance service = (ServiceInstance)serviceList.get(i);
%>
<tr class='<%=i%2==0 ? "light" : "" %>' onmouseover="this.className='on_5'" onmouseout=<%=i%2==0 ? "this.className='light'" : "this.className='on_1'" %> >
<td nowrap="nowrap"><%=service.name %></td>
<td nowrap="nowrap"><%=service.port %></td>
<td nowrap="nowrap"><%=service.path %></td>
<td nowrap="nowrap"><%=service.startCMD %></td>
<td nowrap="nowrap"><%=service.elementId %></td>
<td nowrap="nowrap"><%=service.osId %></td>
<td id="monitor_td_id_<%=i %>" style="text-align: left">
<script>
try{
outPutLimitStr('<%=service.desc %>','monitor_td_id_<%=i %>',75);
}catch(e){
$("#monitor_td_id_<%=i %>").text('<%=service.desc %>');
}
</script>
</td>
<td nowrap="nowrap">
<input type="button" class="button4C" value="修改"
onclick="javascript:modifyService('<%=service.name %>');"/>
<input type="button" class="button4C" value="刪除"
onclick="javascript:deleteService(this.form,'<%=service.name %>');"/>
</td>
</tr>
<%
}
}
%>
</tbody>
</c:if>
<tr>
<td colspan="8" style="text-align:right;">
</td>
</tr>
<tr>
<td colspan="8" style="text-align:right;">
<%@ include file="/pages/common/pagefooter.jsp"%>
</td>
</tr>
<c:if test="${empty divPage.data}">
<tr>
<th class="tablelogo" colspan="10">
服務列表
</th>
</tr>
<tr>
<td colspan="10" style="text-align:center;">
請<a href="javascript:addService();"> 新增 </a>服務
</td>
</tr>
</c:if>
</table>
</html:form>
>>3.c:foreach中的hidden的用法
之前就用過hidden屬性,提交form的隱式提交一個值作為標識,程式碼如下:
<input type="hidden" id="filePath" name="filePath" value="${varLogList.filePath }" />
現在要放在c:foreach裡面,c:foreach是一個迴圈,如果程式碼還跟上面一樣的話,每次在Action裡面得到的值都是第一個值,不能起到迴圈呼叫的效果,所以改成了下面這樣的程式碼:
<input type="hidden" id="filePath" name="filePath${status.count }" value="${varLogList.filePath }" />
提交的時候把status.count引數通過url傳遞過去,因為status.count是一個數字,所以不會出現特特字元的問題,程式碼如下:
<input type="button" class="button4C" value="下 載" onclick="javascript:downfile(this.form, '${status.count }');"/>
對應的downfile函式為:
function downfile(thisForm, count) {
//submitForm(thisForm); //這種方式提交不行,retrieveURL也不行,必須以下面這種方式
thisForm.action = "${contextPath }/logAction.do?method=downFile&count=" + count + "&rondom=" + Math.random();
thisForm.submit();
}
然後在後臺這樣獲得傳遞過去的值:
String count = request.getParameter("count");
String filePath = request.getParameter("filePath" + count);
String fileName = request.getParameter("fileName" + count);
整體程式碼:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/pages/common/global.jsp"%>
<html>
<head>
<title>高德軟體</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="Content-Type" content="text/html; charset=UTF-8"/>
<%@ include file="/pages/common/jquery.jsp"%>
<script src="${contextPath }/js/simpleajax.js"></script>
<script src="${contextPath }/js/datepicker/WdatePicker.js" defer="defer"></script>
<style type="text/css">
<!--
body {
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
}
.STYLE1 {
font-size: 12px;
}
.STYLE3 {
font-size: 12px;
font-weight: bold;
}
.STYLE4 {
color: #03515d;
font-size: 12px;
}
-->
</style>
<script type="text/javascript">
function gotoPage(currentPage,form) {
alert("page");
goto_Page(currentPage,form,"userDiv");
}
function downfile(thisForm, count) {
//submitForm(thisForm); //這種方式提交不行,retrieveURL也不行,必須以下面這種方式
thisForm.action = "${contextPath }/logAction.do?method=downFile&count=" + count + "&rondom=" + Math.random();
thisForm.submit();
}
</script>
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<html:form action="/logAction.do?method=downFile">
<table width="100%" border="0" cellpadding="0" cellspacing="1"
bgcolor="b5d6e6" >
<tr>
<td width="3%" height="22" colspan="7" background="${contextPath }/images/tab/bg_15.jpg"
bgcolor="#FFFFFF">
<table width="100%" border="0" cellspacing="0"
cellpadding="0">
<tr>
<td width="3%">
<div align="center">
<img src="${contextPath }/images/tab/vmware16.png" width="16" height="16" />
</div>
</td>
<td width="97%" class="STYLE1">
<span class="STYLE3">日誌下載
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td width="5%" height="22" background="${contextPath }/images/tab/bg.gif"
bgcolor="#FFFFFF">
<div align="center">
<span class="STYLE1">序號</span>
</div>
</td>
<td width="20%" height="22" background="${contextPath }/images/tab/bg.gif"
bgcolor="#FFFFFF">
<div align="center">
<span class="STYLE1">日誌日期</span>
</div>
</td>
<td width="40%" height="22" background="${contextPath }/images/tab/bg.gif"
bgcolor="#FFFFFF">
<div align="center">
<span class="STYLE1">日誌名稱</span>
</div>
</td>
<td width="20%" height="22" background="${contextPath }/images/tab/bg.gif"
bgcolor="#FFFFFF">
<div align="center">
<span class="STYLE1">檔案體積</span>
</div>
</td>
<td width="15%" height="22" background="${contextPath }/images/tab/bg.gif"
bgcolor="#FFFFFF">
<div align="center">
<span class="STYLE1">日誌下載</span>
</div>
</td>
</tr>
<c:forEach var="varLogList" items="${logList}" varStatus="status">
<tr onmouseover="changeto()" onmouseout="changeback()">
<td height="20" bgcolor="#FFFFFF">
<div align="center">
${status.count }
</div>
</td>
<td height="20" bgcolor="#FFFFFF">
<div align="center">
${varLogList.date }
</div>
</td>
<td height="20" bgcolor="#FFFFFF" >
<div align="center">
${varLogList.fileName }
</div>
</td>
<td height="20" bgcolor="#FFFFFF">
<div align="center">
${varLogList.size }
</div>
</td>
<td height="20" bgcolor="#FFFFFF">
<div align="center">
<input type="hidden" id="filePath" name="filePath${status.count }" value="${varLogList.filePath }" />
<input type="hidden" id="filePath" name="fileName${status.count }" value="${varLogList.fileName }" />
<input type="button" class="button4C" value="下 載" onclick="javascript:downfile(this.form, '${status.count }');"/>
</div>
</td>
</tr>
</c:forEach>
<c:if test="${empty logList }">
<tr>
<td align="center" colspan="6" height="20" bgcolor="#FFFFFF">
沒有找到相關資料。
</td>
</tr>
</c:if>
</table>
</html:form>
</td>
</tr>
</table>
</td>
</tr>
</table>
<script>
try{
//$("#infoDiv").load("${contextPath }/appGrpAction.do?method=basepage");
changeButLi(1);
monitorTree();
tree.closeAllItems();
tree.openItem("-1");
}catch(e){}
$('#container-1').tabs(1,{remote:true,spinner:'',onHide:function(clicked,show,hide){
$(hide).empty();
}});
//refreshTree();
</script>
</body>
</html>
<script><!--
function deleteUser(url){
if(confirm("確實要刪除該使用者麼?")){
retrieveURL(url,null,null,afterDelteUser);
}
}
function afterDelteUser(content){
if(content!=null&&content!=""){
var arr = content.split(",");
alert(arr[1]);
if(arr[0]=="true"){
//retrieveURL("${contextPath}/userAction.do?method=userList","actionDiv");
var url = "${contextPath}/pages/user/userbasepage.jsp";
window.parent.frames["rightFrame"].location = url;
}
}
}
function toModifyUser(url,div){
$("#container").hide();
retrieveURL(url,div,null,after2ModifyUser);
}
function after2ModifyUser(content){
if(content!=null&&content!=""){
var arr = content.split(",");
if(arr[0]=="false"){
$("#chance_search_div").show();
alert(arr[1]);
retrieveURL("${contextPath}/userAction.do?method=userList","actionDiv");
}
}
}
function addUser(url) {
window.parent.frames["rightFrame"].location = url;
}
function gotoPage(currentPage,form) {
goto_Page(currentPage,form,"actionDiv");
}
--></script>
對應的Action程式碼如下:
package com.autonavi.monitor.action;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.autonavi.core.web.struts.action.BaseDispatchAction;
import com.autonavi.download.bean.FileBean;
import com.autonavi.monitor.exception.NotLoginException;
import com.autonavi.monitor.model.User;
import com.autonavi.monitor.service.StatisticsService;
public class LogAction extends BaseDispatchAction {
private StatisticsService statisticsService;
Map<String, List<FileBean>> mapCache = null;
/**
* 獲得快取裡面的資料
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws Exception
*/
public ActionForward getCacheLog(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
User user = (User) getUser();
if (user == null) {
throw new NotLoginException();
}
String channelURLKey = request.getParameter("channelURL");
Map<String, List<FileBean>> fileListMapCache = null;
List<FileBean> logList = null;
if(mapCache != null) {
fileListMapCache = mapCache;
logList = fileListMapCache.get(channelURLKey);
}
if(logList != null) {
for(FileBean fileBean: logList) {
System.out.println("日期:" + fileBean.getDate() + " 名稱:" + fileBean.getFileName() + "路徑:" + fileBean.getFilePath());
}
}
request.setAttribute("logList", logList);
return mapping.findForward("logList");
}
/**
* 實現檔案的下載
*
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws Exception
*/
public ActionForward downFile(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
User user = (User) getUser();
if (user == null) {
throw new NotLoginException();
}
String count = request.getParameter("count");
String filePath = request.getParameter("filePath" + count);
String fileName = request.getParameter("fileName" + count);
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
OutputStream fos = null;
InputStream fis = null;
File uploadFile = new File(filePath);
fis = new FileInputStream(uploadFile);
bis = new BufferedInputStream(fis);
fos = response.getOutputStream();
bos = new BufferedOutputStream(fos);
// 這個就就是彈出下載對話方塊的關鍵程式碼
response.setHeader("Content-disposition", "attachment;filename=" + fileName);
int bytesRead = 0;
// 這個地方的同上傳的一樣。我就不多說了,都是用輸入流進行先讀,然後用輸出流去寫,唯一不同的是我用的是緩衝輸入輸出流
byte[] buffer = new byte[8192];
while ((bytesRead = bis.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);
}
bos.flush();
fis.close();
bis.close();
fos.close();
bos.close();
return null;
}
/**
* 獲得頻道列表
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws Exception
*/
public ActionForward logBase(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
User user = (User) getUser();
if (user == null) {
throw new NotLoginException();
}
Map<String, List<FileBean>> fileListMap = statisticsService.getFileList();
if(mapCache != null) {
//清除快取
mapCache.clear();
}
//把查出的資料加入快取
mapCache = fileListMap;
List<String> channelURLList = new ArrayList<String>();
if(fileListMap != null) {
Set<String> key = fileListMap.keySet();
for(Iterator it = key.iterator(); it.hasNext();) {
String s = (String) it.next();
channelURLList.add(s);
}
}
request.setAttribute("channelURLList", channelURLList);
return mapping.findForward("channelList");
}
public StatisticsService getStatisticsService() {
return statisticsService;
}
public void setStatisticsService(StatisticsService statisticsService) {
this.statisticsService = statisticsService;
}
}