使用“&”获取数据时遇到问题特殊字符

时间:2015-01-07 06:06:45

标签: java xml velocity

我们的应用程序中有一个屏幕/页面,我们为不同的产品显示不同的列。列中的所有记录都是从数据库中获取的。

此外,我们在屏幕底部有两个导出按钮,用于显示 PDF XLS 中的所有记录格式。

除了一种情况外,这些功能正常。我们在屏幕上有一列名称,其值从数据库中获取。当名称列下的任何记录中包含& 时,导出功能将停止工作。

例如: -

名称为“BOWOG BEHEER B.V.”,出口对pdf和xls都很好。

但是对于“BOWOG& BEHEER B.V.”的名字,它停止了工作。点击导出按钮时,pdf和xls显示为空白页。

有人可以帮忙吗?

以下是一段代码: - (不是完整代码)

public class CVRExportServlet extends HttpServlet {

    private final SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyy");

    /** context. */
    private ResourceContext context = null;

    private Map createParametersFromRequest(final HttpServletRequest request) {
        // copy all request parameters
        final Map parameters = new HashMap();
        final Enumeration names = request.getParameterNames();

        while (names.hasMoreElements()) {
            final String name = (String) names.nextElement();
            final String[] values = request.getParameterValues(name);

            if (values.length > 1) {
                parameters.put(name, values);
            } else {
                parameters.put(name, values[0]);
            }
        }
        // parse request uri to get type and format
        final String requestURI = request.getRequestURI();
        String type = StringUtils.left(requestURI, requestURI.lastIndexOf('.'));
        type = StringUtils.substring(type, requestURI.lastIndexOf('/') + 1);

        final String format = StringUtils.substring(requestURI, requestURI.lastIndexOf('.') + 1);

        parameters.put(Constants.EXPORT_TYPE_PARAMETER, type);
        parameters.put(Constants.EXPORT_FORMAT_PARAMETER, format);

        // determine themeUrl
        final String requestUrl = request.getRequestURL().toString();
        final int index = requestUrl.indexOf(request.getContextPath());
        String server = "";
        if (index > -1) {
            server = requestUrl.substring(0, index);
        }

    private void fillParameters(final HttpServletRequest request, final HttpServletResponse response, final Map parameters)
      throws ApplicationException {

        parameters.put("props", ResourceBundle.getBundle("messages"));

        // Create search request using the search form
        final SearchForm form = (SearchForm) request.getSession().getAttribute(
        (String) request.getSession().getAttribute(CvrConstants.SESS_ATTR_CVR_SEARCH_FORM_NAME));
        final SearchRequest searchRequest = form.getSearchRequest();
        searchRequest.setPageNumber(1);
        searchRequest.setRowsPerPage(10000);
        parameters.put("searchRequest", searchRequest);     
    }

    public void service(final HttpServletRequest request, final HttpServletResponse response)
            throws ServletException, IOException {

        final long startTime = System.currentTimeMillis();

        // create parameters from request
        final Map parameters = this.createParametersFromRequest(request);
        parameters.put(ResourceContext.class.getName(), this.context);

        try {
            this.fillParameters(request, response, parameters);
            final SearchRequest searchRequest = (SearchRequest) parameters.get("searchRequest");
            if (searchRequest == null || searchRequest.getCounterPartyList() == null
                    || searchRequest.getCounterPartyList().isEmpty()) {
            throw new ExportException("Exception occurred while handling export: empty counterparty list");
            } else {
                if (searchRequest.getCounterPartyList().size() == 1) {
                    this.handleSingleReportExport(response, parameters);
                } else {
                    this.handleMutlipleReportExport(response, parameters);
                }
            }
        } catch (final Exception e) {
            this.handleException(e, request, response);
          }
       }

    private void handleSingleReportExport(final HttpServletResponse response, final Map parameters) throws Exception {


        final XmlExportService exportService = this.getXmlExportService();
        final ApplicationContext context = this.getApplicationContext();
        final XmlTransformationService xmlTransformationService = (XmlTransformationService) context.getBean("transformationService");

        // perform export
        exportService.export(parameters);

        // perform transformation
        final ExportResult exportResult = xmlTransformationService.transform(parameters);

        // write result to stream
        response.setContentType(exportResult.getContentType());

        response.setContentLength(exportResult.getContentLength());
        if (parameters.get("format").equals("csv")) {

            response.setContentType("text/csv");
            response.setHeader("Content-disposition", "attachment; filename=export.csv");
        } else if (parameters.get("format").equals("pdf")) {

            response.setContentType("application/pdf");
            response.setHeader("Content-disposition", "inline; filename=export.pdf");
        }

        final ServletOutputStream out = response.getOutputStream();
        out.write(exportResult.getBytes());
        out.flush();
        out.close();

    }

    private void handleMutlipleReportExport(final HttpServletResponse response, final Map parameters) throws Exception {
        final SearchRequest searchRequest = (SearchRequest) parameters.get("searchRequest");

        response.setContentType("application/force-download");
        response.setHeader("Content-Encoding" , "x-compress");
        response.setHeader("Content-Disposition", "attachment; filename=export_" + parameters.get("format") + ".zip");

        final XmlExportService exportService = this.getXmlExportService();
        final ApplicationContext context = this.getApplicationContext();
        final XmlTransformationService xmlTransformationService = (XmlTransformationService) context.getBean("transformationService");

        // start the zip process
        final ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());

        // create a file for each counterparty and add it to the zip file
        for (final String counterPartyId : searchRequest.getCounterPartyList()) {
            // make sure to reset the counterparty to the current one in the loop
            searchRequest.setCounterPartyList(Arrays.asList(new String[] {counterPartyId}));

            // perform export
            exportService.export(parameters);

            // perform transformation
            final ExportResult exportResult = xmlTransformationService.transform(parameters);

            // add the file to the zip
            final String fileName = counterPartyId + "_" + sdf.format(searchRequest.getRevaluationDate()) + "." + parameters.get("format");

            zos.putNextEntry(new ZipEntry(fileName));
            zos.write(exportResult.getBytes());
            zos.closeEntry();
        }
        // finish the zip process
        zos.flush();
        zos.close();    
    }

我现在有点想法。实际上问题是在vm(速度模板)。 “name”列是从vm文件中获取的,代码是这样的: -

$!{result.counterpartyName}

这是针对多个记录的每个循环。任何人都可以建议我如何忽略vm文件本身的特殊字符。这样即使“名称”栏中有“&”,我们也能正确导出或“ - ”等特殊字符。

1 个答案:

答案 0 :(得分:2)

根据您的代码,您似乎正在使用XML转换服务。

我说你的参数中的数据可能包含悬空&符号。要使有效的XML准备好进行转换,&应为&。但是,根据给定的代码,不可能说出XML数据的来源。你说它来自数据库,所以我的猜测是应该通过修改数据库中的数据来处理问题。

编辑:

我似乎部分正确,但数据库并不包含XML - 如果我正确地得到这个,数据来自数据库作为原始表格数据,但使用速度模板格式化为XML。如果是这样,那么应该在速度模板like this中使用XML转义。