Как реализовать JQuery.ajax(dataType:json) для Spring mvc 2.5
Я пытаюсь сделать вызов AJAX с библиотекой jQuery, используя метод $.ajax() и Json как dataType, проблема возникает, когда я пытаюсь применить это к своему приложению в springmvc 2.5, я просто не могу найти хороший учебник по как применить это к Spring mvc 2.5, но 3.0
ура
exception
javax.servlet.ServletException: Servlet.init() для диспетчера сервлетов вызвал исключение org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462) org.apache.catalina.valves.ErrorReportValve.invoke:100) org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:563) org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:399) org.apache.coyote.http11.Httpro11 (Http11Processor.java:317) org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:204) org.apache.coyote.http11.Http11Protocol$Http11ConnectionHhttp: tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:311) java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) java.util.concurrent.ThecExecol Java:908) java.lang.Thread.run(Thread.java:662)
первопричина
org.springframework.beans.factory.BeanCreationException: Ошибка создания компонента с именем 'viewResolver', определенным в ресурсе ServletContext [/WEB-INF/dispatcher-servlet.xml]: сбой инициализации компонента; вложенное исключение - org.springframework.beans.factory.BeanCreationException: ошибка создания бина с именем 'jsonView', определенного в ресурсе ServletContext [/WEB-INF/views.xml]: сбой при создании экземпляра бина; вложенным исключением является org.springframework.beans.BeanInstantiationException: не удалось создать экземпляр класса бина [org.springframework.web.servlet.view.json.JsonView]: конструктор вызвал исключение; вложенное исключение java.lang.NoClassDefFoundError: орг / апач / Обще / BeanUtils/ConvertUtilsBean org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:480) org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409) java.security.AccessController.doPrivileged(собственный метод) org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.cory AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264) org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) org.springframework.sue.) org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185) org.springframework.beans.factory.support.AbstractApplicationContext.java:728) org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380) org.springframework.web.servlet.FrameworkServlet. FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:316) org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:282) org.springframework.web.servlet.ava.we.GenericServlet.init(GenericServlet.java:160) org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462) org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:563) org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapg).jav.apache.coyote.http11.Http11Processor.process(Http11Processor.java:317) org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:204) org.apache.ttne.Phind.Hot Http11Protocol.java:182) org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:311) java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java: java:88) concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) java.lang.Thread.run(Thread.java:662)
первопричина
org.springframework.beans.factory.BeanCreationException: ошибка создания компонента с именем 'jsonView', определенным в ресурсе ServletContext [/WEB-INF/views.xml]: сбой при создании экземпляра компонента; вложенным исключением является org.springframework.beans.BeanInstantiationException: не удалось создать экземпляр класса бина [org.springframework.web.servlet.view.json.JsonView]: конструктор вызвал исключение; вложенное исключение составляет java.lang.NoClassDefFoundError: орг / Apache/ обыкновенные / BeanUtils/ConvertUtilsBean org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:883) org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:839) org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:440) org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409) ява.security.AccessController.) org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261) org.springframework.beans.actory18ava.Bean.FB org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164) org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiate.finishBeanFactoryInitialization(AbstractApplicationContext.java:728) org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380) org.springframework.web.servlet.view.XmlViewj.web.servlet.view.XmlViewResolver.initApplicationContext(XmlViewResolver.java:89) org.springframework.context.support.ApplicationObjectSupport.initApplicationContext(ApplicationObjectSupport.java:119) org.springframework.web.context.support.WebApplicationObjectSupport.initApplicationContext(WebApplicationObjectSupport.java:69) org.springframework.conj.context.support.ApplicationContextAwareProcessor.postProcessBeforeInitialization(ApplicationContextAwareProcessor.java:70) org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:350) org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory. Java:1331) org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473) org.springframework.beans.AjBeBe va.security.AccessController.doPrivileged (собственный метод) org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:jact 264) org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185) org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164) org.springframework. 429) org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728) org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380) org.springframework.web.servlet. 316) org.springframework.web.servlet.FrameworkServlet.initServletBean (FrameworkServlet.java:282) org.springframework.web.servlet. 160) org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462) org.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.java:100) org.apache.catalina.valke.veinog AccessLogValve.java:563) org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:399) org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:317) org.aptt.co.y.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:204) org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:182) org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java. java. java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) java.lang.Thread.run(Thread.java266)
первопричина
org.springframework.beans.BeanInstantiationException: Не удалось создать экземпляр класса компонента [org.springframework.web.servlet.view.json.JsonView]: конструктор вызвал исключение; Вложенное исключение - java.lang.NoClassDefFoundError: org/apache/commons/beanutils/ConvertUtilsBean org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:115) org.springframework.guis.intiti. Java:61) org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:877) org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:839) org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:440) org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409) java.security.AccessController.doPrivileged(Native Method) org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380) org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264) org.springframework.beans.factory.support..beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261) org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java!..support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380) org.springframework.web.servlet.view.XmlViewResolver.initFactory(XmlViewResolver.java:138) org.springframework.web.servlet.view.XmlViewResolver.initApplicationContext(XmlViewResolver.java:89) org.springframework.context.support.context.support.WebApplicationObjectSupport.initApplicationContext(WebApplicationObjectSupport.java:69) org.springframework.context.support.ApplicationObjectSupport.setApplicationContext(ApplicationObjectSupport.java:73) org.springframework.context.support.ApplicationContextAwareProcessor.postProcessBeforeInitialization(ApplicationContextAwareProcessor.java:70) org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:350) org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1331) org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473) org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.рун support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380) org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264) org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261) org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFebeF5.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164) org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingle тонн (DefaultListableBeanFactory.java:429) org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728) org.springframework.context.support.Context.pp.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:402) org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet. springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:126) javax.servlet.GenericServlet.init(GenericServlet.java:160) org.apache.catalina.authenticator.Authenticator apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:563) org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:399) org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:317) org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.proap. org.jpg.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:182) org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:311) java.utilThunser (ThreadPoolExecutor.java:886) java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) java.lang.Thread.run(Thread.java:662)
первопричина
java.lang.NoClassDefFoundError: org/apache/commons/beanutils/ConvertUtilsBean org.springframework.web.servlet.view.json.filter.SimplePropertyFilter.setUpBeanUtils(SimplePropertyFilter.j.wef.web).filter.SimplePropertyFilter.(SimplePropertyFilter.java:27) org.springframework.web.servlet.view.json.JsonView.(JsonView.java:25) sun.reflect.NativeConstructorAccessorImpl.newInstance0(естественный метод) newInstance(NativeConstructorAccessorImpl.java:39) sun.reflect. Java:100) org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:61) org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.aneA BeanFactory.java:877) org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance (AbstractAutowireCapableBeanFactory.java:839) org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean (AbstractAutowireCapableBeanFactory.java:440) org.springframework. beans.factory.support. springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject (AbstractBeanFactory.java:264) org.springframework.beans.factory.support..doGetBean(AbstractBeanFactory.java:261) org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185) org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164) org.springframework. org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization (AbstractApplicationContext.java:728) org.springframework. XmlViewResolver.java:138) org.springframework.web.servlet.view.XmlViewResolver.initApplicationContext (XmlViewResolver.java:89) org.springframework.context.support. context.support.WebApplicationObjectSupport.initApplicationContext (WebApplicationObjectSupport.java:69) org.springframework.context.support.Applica tionObjectSupport.setApplicationContext (ApplicationObjectSupport.java:73) org.springframework.context.support.ApplicationContextAwareProcessor.postProcessBeforeInitialization (ApplicationContextAwareProcessor.java:70) org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization (AbstractAutowireCapableBeanFactory.java:350) орг.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1331) org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473) org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409) java.security.AccessController. s.factory.support. (AbstractBeanFactory.java:261) org.springframework.beans.factory.support.AbstractBeanFactory.getBean (AbstractBeanFactory.java:185) org.springframework.beans.factory.support.AbstractBeanFactory..beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429) org.springframework.context.support.AbstractApplicationContext.finishBeanFactory.jpg:380) org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:402) org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:316) org.springframework.web.servlet. javax.servlet.GenericServlet.init(GenericServlet.java:160) org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462) org.apache.catalina.valves.ErrorReportValjinin.port org.apache.catalina.valves.AccessLogValve.invoke (AccessLogValve.java:563) org.apache.catalina.connector.CoyoteAdapter.service (CoyoteAdapter.java:399) org.apache.coyote.http11.HttpProcessor. Processor Java:317) org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:204) org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http: 18) orgtp.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:311) java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) java.lang.Thread.run(Thread.java:662)
1 ответ
Вам нужно аннотировать методы вашего контроллера с помощью URL-адресов restful и добавить аннотацию @ResponseBody в метод контроллера.
Пример:
@Controller
@RequestMapping(value = "/pets")
@ResponseBody
public Pet getPet(@PathVariable String petId, Model model) {
//..generate some Json
// Add Json to the Response
model.addAttribute("json", json);
}
Затем в вашем js используйте jquery, чтобы сделать ajax-вызов URL
$.ajax({
type: 'POST',
url: "/pets",
data: data,
success: function(data){
alert(data.json); //alerts json object
},
dataType: 'json'
});