1

I have a web application connected to a MySQL database.

This application implements the Frameworks Spring and Hibernate, and it has the following architecture:

enter image description here

In the file getGraph.jsp I have this code:

<%@page import="org.springframework.beans.factory.annotation.Autowired"%> <%@page import="com.google.gson.*"%> <%@page import="com.fdl.entity.*"%> <%@page import="com.fdl.dao.*"%> <%@page import="java.io.PrintWriter"%> <%@page import="org.springframework.context.support.ClassPathXmlApplicationContext" %> <%@page import="org.springframework.context.ApplicationContext" %> <% response.setContentType("application/json;charset=UTF-8"); Gson gson = new Gson(); ApplicationContext context = new ClassPathXmlApplicationContext("/applicationContext.xml"); FdlDao fdlDao=(FdlDao) context.getBean("fdlDao"); Graph graph = fdlDao.getGraph2(); out.print(gson.toJson(graph)); %> 

But when I run the code I get this error:

7 mars 2014 23:27:54 org.springframework.context.support.AbstractApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@15b4b49: startup date [Fri Mar 07 23:27:54 WET 2014]; root of context hierarchy 7 mars 2014 23:27:54 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [applicationContext.xml] 7 mars 2014 23:27:54 org.apache.catalina.core.StandardWrapperValve invoke GRAVE: Servlet.service() for servlet [jsp] in context with path [/FDL] threw exception [An exception occurred processing JSP page /getGraph.jsp at line 14 11: 12: response.setContentType("application/json;charset=UTF-8"); 13: Gson gson = new Gson(); 14: ApplicationContext context = new ClassPathXmlApplicationContext("/applicationContext.xml"); 15: FdlDao fdlDao=(FdlDao) context.getBean("fdlDao"); 16: Graph graph = fdlDao.getGraph2(); 17: out.print(gson.toJson(graph)); Stacktrace:] with root cause java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:157) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302) at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174) at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209) at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180) at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:243) at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:127) at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:93) at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:130) at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:537) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:451) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83) at org.apache.jsp.getGraph_jsp._jspService(getGraph_jsp.java:82) at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) at javax.servlet.http.HttpServlet.service(HttpServlet.java:728) at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432) at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390) at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334) at javax.servlet.http.HttpServlet.service(HttpServlet.java:728) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1146) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at java.lang.Thread.run(Thread.java:679) 

What is the problem? And how can I solve it?

1
  • Spring with scriplets? Commented Mar 8, 2014 at 0:03

1 Answer 1

2

The message explains it:

class path resource [applicationContext.xml] cannot be opened because it does not exist

The classpath of a webapp is made of the WEB-INF/classes folder and of all the jars under WEB-INF/lib. The applicationContext.xml is under WEB-INF, so it's not in the classpath.

Note that this is not how you should use Spring:

  • scriptlets shouldn't be used
  • calling s DAO should not be done from a JSP, which is a view component which should only generate HTML. Calling a DAO is the job of a controller, written in Java
  • JSPs are view components and should never be accessed directly. Always through a controller.
  • creating a new Spring context at each request is extremely costly. The context should be started when the application is deployed, once and for all.

The way to use Spring in a webapp is decribed in the Spring documentation. Read it, and use Spring MVC as documented.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.