为什么找不到getservletcontext?

edd*_*ddy 8 java servlets spring-mvc

我正在尝试使用getServletContext().getRealPath("/"),但我不断收到此错误:

找不到符号符号:方法getServletContext()location:interface javax.servlet.http.HttpSession String path = session.getServletContext().getRealPath("/")+"layout/tiles /"+ reportPath; 在此输入图像描述

public ModelAndView handleRequest( HttpServletRequest request, HttpServletResponse response ) throws Exception {

        session = request.getSession();
        Map params = new HashMap();
        String reportPath = "maintenance/jasper/report01.jasper";
        exportToPDF( reportPath , response, params );

        return null;
    }

    protected void exportToPDF( String reportPath , HttpServletResponse response, Map jasperParams ) throws Exception {

            String path = session.getServletContext().getRealPath( "/" ) + "layout/tiles/" + reportPath ;

            if ( !new File( path ).exists() ) {
                throw new Exception( "The path doesn''t exist. </br>" + path );
            }
            InputStream input = new FileInputStream( path );

            jasperParams.put( "REPORT_LOCALE", Locale.US );

            JasperPrint jasper = JasperFillManager.fillReport( input , jasperParams, new JRBeanCollectionDataSource(Vehicles) );

            response.setContentType( "application/pdf" );
            ServletOutputStream output = response.getOutputStream();

            JRExporter exporter = new JRPdfExporter();

            exporter.setParameter( JRExporterParameter.JASPER_PRINT, jasper );
            exporter.setParameter( JRExporterParameter.OUTPUT_STREAM, output );

            exporter.exportReport();
            output.close();


    }
Run Code Online (Sandbox Code Playgroud)

你知道为什么会这样吗?

谢谢Ritesh,我做了你告诉我的事,但现在我收到了一条新消息

在此输入图像描述

- - - 编辑 - - - -

检查我的dispatcher-servlet.xml我发现它与这个网站上显示的代码有所不同.我不知道它会如何影响我的项目,但我想知道是否有不同的方法来获得与使用会话相同的结果.getServletContext(). getRealPath("/")

<bean id="viewResolver"
      class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass"
              value="org.springframework.web.servlet.view.tiles2.TilesView" />
</bean>
<bean id="tilesConfigurer"
      class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
    <property name="definitions">
        <list>
            <value>/WEB-INF/tiles.xml</value>
        </list>
    </property>
</bean>
Run Code Online (Sandbox Code Playgroud)

Rit*_*esh 5

getServletContext()是在 Servlet 2.3 中添加的。它在 2.2 中不存在,请参阅Servlet 2.2 javadoc

所以唯一的解释是您的项目正在针对旧版本验证代码。

getServletContext()您似乎正在使用的 Spring 的 Controller 类中也有。所以,而不是session.getServletContext().getRealPath( "/" ),你会没事的getServletContext().getRealPath( "/" )

1 月 30 日编辑:Jasper Reports jar 文件清理

我已经验证 jasperreports-3.7.1-project.zip 有旧版本的 servlet.jar。我推荐以下:

  1. 删除您从 jasperreports-3.7.1-project.zip 的 lib 文件夹中添加的所有 jar 文件,但将 jar 文件保留在“dist”文件夹中。

  2. 根据编译错误信息一一添加jar文件。请不要添加任何在 TOMCAT-HOME/lib 文件夹中也可用的 jar 文件,也不要添加任何 Spring jar 文件。由于您知道 jasper 报告项目有旧的 jar 文件,首先查看 netbeans 是否提供这些 jar,如果没有,则尝试使用其他存储库中的最新版本,例如http://repo1.maven.org/maven2/。带有依赖项的 Spring 框架下载也有几个可以使用的常用文件。

  3. 检查任何在线资源以获取有关所需 jar 文件的更多信息。以下链接描述了在 netbeans 中与 jasper 报告 1.2.5 版的集成:http : //developers.sun.com/jsenterprise/archive/reference/techart/jse8/jasper_reports.html但是您需要与 3.7.1 版本相关的内容。