Export Excel 我尝试通过在 jsp 上执行此操作来导入类 java ExportQP: 这是我-6ren">
gpt4 book ai didi

java - 无法解析为变量exportStr

转载 作者:行者123 更新时间:2023-12-01 04:38:49 25 4
gpt4 key购买 nike

我在jsp中这一行有一个错误 <a href="<%=response.encodeURL(request.getContextPath()+"/ExportQP")%>?typeFichier=xls&exportStr=<%=exportStr%>"> Export Excel </a>

我尝试通过在 jsp 上执行此操作来导入类 java ExportQP:

<%@ page import="com.export.servlet.ExportQP"%>

这是我的 ExportQP 类

public class ExportQP extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
public ExportQP() {
super();
}

/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

System.out.println("Export Excel GET");
//On récupère les patramètres
String typeFichier = request.getParameter("typeFichier");
String exportStr = request.getParameter("exportStr");

//String exportTab1[] = exportStr.split("$$");
//int nbLignes = exportTab1.length;

//Preparation de la date pour le nom du fichier
Date dateExport = new Date();
Timestamp timeStampDate = new Timestamp(dateExport.getTime());
DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy-HH-mm");
String affDate = formatter.format(timeStampDate);

response.setHeader("Cache-Control", "public");
response.setHeader("Pragma", "public");
//On donne le nom du fichier
response.setHeader("Content-Disposition", "attachment; filename=\"Export"+affDate+"."+typeFichier+"\"");

//On récupère le flux de sortie
ServletOutputStream out = response.getOutputStream();

//On indique le type d'export que l'on va faire
if(typeFichier.equals("xls")){
System.out.println("Type XLS");
response.setContentType("application/vnd.ms-excel");
// On crée l'Excel Workbook et le Sheet
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("Annuaire CHIELVR");
//HSSFHeader header = sheet.getHeader();
//header.setCenter("Annuaire CHIELVR header");

HSSFRow row = null;
HSSFCell cell = null;
String entreeStr = "";
//String[] exportTab2 = null;
//On récupère le nombre de lignes et on boucle dessus
StringTokenizer st = new StringTokenizer(exportStr,"$$");
//for(int i = 0; i < st.countTokens(); i++ )
int nbLignes = st.countTokens();
for(int i = 0; i < nbLignes; i++ )
{
//On creer la ligne
row = sheet.createRow(i);
//On récupère l'entrée
entreeStr = st.nextToken();
//On coupe la ligne pour obtenir les différents champs de l'entrée
StringTokenizer st2 = new StringTokenizer(entreeStr,";");
String token = "";
for(int j = 0; j < st2.countTokens(); j++ ) {
cell = row.createCell(j);
token = st2.nextToken();
if(token != null){
cell.setCellValue(token);
}else{
cell.setCellValue(" ");
}
}
}
//On écris le contenu du workbook sur le flux de sortie
wb.write(out);
out.close();
}else if(typeFichier.equals("csv")){

System.out.println("Type CSV");
String print = "";
response.setContentType("application/CSV");
StringTokenizer st = new StringTokenizer(exportStr,"$$");
int cptToken = st.countTokens();
String content = "" ;
for(int i = 0; i < cptToken; i++ ) {
print = st.nextToken();
content += print;
content += "\n";
}
out.print(content);
}
}

/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("Export Excel POST");
}

}

所以我的错误是我的jsp告诉我exportStr无法解析为变量”。

最佳答案

<%@ page import="com.export.servlet.ExportQP"%>

您是否将 Servlet 导入到 JSP 中? Servlet 不应被导入。

Servlets receive and respond to requests from Web clients, usually across HTTP, the HyperText Transfer Protocol.

它们被称为由Servlet映射名称定义,在您的情况下似乎是ExportQP

该错误意味着您的 JSP 中应该有一个名为 exportStr 的变量,例如

<%
String exportStr="Hello World";
%>

关于java - 无法解析为变量exportStr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16951748/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com