【发布时间】:2016-01-14 21:56:10
【问题描述】:
我想要什么:
如果有人访问该页面,可以选择两个日期并单击一个(单个)按钮以下载数据 em> 在所选的两个日期之间。
我已经有的工作:
JSP/web 开发对我来说是新的。这是我的场景,使用datepicker 和JSP 有一个页面,该页面对数据库进行查询,返回两个日期之间的某些数据,并使用此数据创建一个.txt 文件以保存本地。
创建文件后,可以按下另一个按钮下载文件。
我需要做什么:
我需要只需要一个按钮来完成两个动作,所以一旦文件保存在本地,下载提示就会出现,并且访问者可以下载了。
由于这些词很常见,很难在搜索引擎上找到我需要的内容。
我需要一个按钮。我不想要看起来像按钮的链接或锚点,这对某些人来说可能是一件容易的事,但我已经失去了两天
信息:
Apache Tomcat:8.0.27 && Netbeans 8.1 && Ubuntu 14.04
JSP:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<%@taglib prefix="sj" uri="/struts-jquery-tags"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="resources/css/jtable.css" type="text/css">
<link rel="stylesheet" href="resources/css/style.css" type="text/css">
<link rel="stylesheet" href="resources/css/jquery-ui-1.10.3.custom.css" type="text/css">
<link href="resources/css/jquery-ui-1.10.3.custom.css" rel="stylesheet" type="text/css" />
<script src="resources/js/jquery-1.11.3.js" type="text/javascript"></script>
<script src="resources/js/jquery-ui-1.10.3.custom.js" type="text/javascript"></script>
<script src="resources/js/recuperacion-datos.js" type="text/javascript"></script>
<title></title>
<script>
$(document).ready(function(){
$("#datepicker1").datepicker({
maxDate: 0
});
$("#datepicker2").datepicker({
maxDate: 0,
onSelect: function(selected) {
$("#datepicker1").datepicker("option","maxDate", selected);
}
});
});
</script>
</head>
<body>
<center>
<div class="jtable-main-container" style="width: 60%;">
<div class="jtable-title">
<div class="jtable-title-text">
Recuperación de Datos
</div>
</div>
<div id="LecturasTableContainer" style="position: relative; text-align: center; font-size: 17px; top: 10px;">
Fecha Inicio: <input type="text" name="fechaInicio" id="datepicker1"> <span> </span><span> </span>
Fecha Fin: <input type="text" name="fechaFin" id="datepicker2">
<!-- Button who generate the file -->
<button type="submit" id="LoadRecordsButton" onclick="return confirm('Datos Recuperados');">Generar</button>
</div>
<br>
<!-- Button who download the file -->
<s:form action="download" method="POST">
<s:submit value="Descargar" type="button"/>
</s:form>
</div>
</center>
</body>
</html>
“通用”按钮的动作类:
package com.raspberry.struts.action;
import static com.opensymphony.xwork2.Action.SUCCESS;
import com.opensymphony.xwork2.ActionSupport;
import com.raspberry.dao.control.DBControl;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.interceptor.ServletRequestAware;
import java.sql.Connection;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class DataRecovery extends ActionSupport implements ServletRequestAware {
public HttpSession session;
public Connection c;
public String fechaFin;// = null;
public String fechaInicio; // = null;
public String goDataRecovery(){
session.setAttribute("mainopt", "dataRecovery");
return SUCCESS;
}
public String doDataRecovery() throws ParseException, FileNotFoundException{
DBControl dato = new DBControl();
fechaInicio = getFechaInicio();
fechaFin = getFechaFin();
DateFormat df = new SimpleDateFormat("yyyMMMddkkmm");
String fecha = df.format(Calendar.getInstance().getTime());
String lectura = dato.selecAlltLecturasFecha(fechaInicio, fechaFin);
File archivo = new File ("/media/recovery"+fecha+".txt");
archivo.getParentFile().mkdirs();
PrintWriter printWriter;
printWriter = new PrintWriter(archivo);
printWriter.println (lectura);
printWriter.close ();
return SUCCESS;
}
public String getFechaFin() {
return fechaFin;
}
public String getFechaInicio() {
return fechaInicio;
}
@Override
public void setServletRequest(HttpServletRequest hsr) {
session = hsr.getSession();
}
}
“Descargar”按钮的操作类:
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import com.opensymphony.xwork2.ActionSupport;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class DownloadAction extends ActionSupport{
private InputStream fileInputStream;
public InputStream getFileInputStream() throws Exception {
return fileInputStream;
}
public String execute() throws Exception {
DateFormat df = new SimpleDateFormat("yyyMMMddkkmm");
String fecha = df.format(Calendar.getInstance().getTime());
fileInputStream = new FileInputStream(new File ("/media/recovery"+fecha+".txt"));
return SUCCESS;
}
}
Struts.xml:
<action name="GetDataRecovery" class="com.raspberry.struts.action.DataRecovery"
method="doDataRecovery">
<interceptor-ref name="SessionValidationStack" />
<result name="success">main.jsp</result>
<result name="sessionexpired">index.jsp</result>
</action>
<action name="download" class="com.raspberry.struts.action.DownloadAction">
<result name="success" type="stream">
<param name="contentType">application/octet-stream</param>
<param name="inputName">fileInputStream</param>
<param name="contentDisposition">attachment;filename="recovery.txt"</param>
<param name="bufferSize">1024</param>
</result>
</action>
【问题讨论】:
-
@user3659052 感谢您的评论,我的按钮已经用于在本地创建 .txt 文件。所以你的参考很有帮助,但那部分已经准备好了。
-
你说的Servlet类不是Servlet(最后代码sn-p)。它是一个 Struts 动作类(
Action、ActionSupport和ServletRequestAware是 Struts 框架的组件)。 -
要下载,您需要另一个操作,该操作映射到另一个将执行下载的方法。阅读this答案。
-
@ElberCM S1 和 S2 是基于 servlet 技术构建的不同框架。