1.Below打Spring MVC的控制器在我的index.jsp Ajax调用无法通过Ajax调用
<script type="text/javascript">
$(document).ready(function(){
$('#buttonDemo1').click(function(){
$.ajax({
method: "GET",
url: '${pageContext.request.contextPath}/ajax/demo1',
success: function(result){
$('#result1').html(result);
},
error: function(response){
console.log("error");
}
});
});
});
</script>
2.Below是控制器的代码
@Controller
@RequestMapping("/ajax")
public class AjaxController {
@RequestMapping(method = RequestMethod.GET)
public String index() {
return "ajax/index";
}
@RequestMapping(value="/demo1", method = RequestMethod.GET)
@ResponseBody
public String demo1() {
return "Demo 1";
}
}
3.下面是调度员的代码
<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?xml version="1.0" encoding="UTF-8"?> -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<context:component-scan base-package="demo.controller" />
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<!--
Most controllers will use the ControllerClassNameHandlerMapping above, but
for the index controller we are using ParameterizableViewController, so we must
define an explicit mapping for it.
-->
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="index.htm">indexController</prop>
</props>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
<!--
The index controller.
-->
<bean name="indexController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="index" />
下面是web.xml中
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <listener> <listener- class>org.springframework.web.context.ContextLoaderListener</listener- class> </listener> <servlet> <servlet-name>dispatcher</servlet-name> <servlet- class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>*.htm</url-pattern> </servlet-mapping> <session-config> <session-timeout> 30 </session-timeout> </session-config> <welcome-file-list> <welcome-file>redirect.jsp</welcome-file> </welcome-file-list> </web-app>
我不知道什么是错,只要我打的Ajax控制器的路上,我得到 “GET http://localhost:8080/ajax/demo1 404(未找到)” 错误。请帮我解决我错在哪里。我尝试了几个解决方案,但他们没有解决问题。
你看到日志输出控制器实例化? –
@TheHeadRush是的,我可以看到。以下是日志: – Rishabh
信息:将“{[/ ajax],methods = [GET],params = [],headers = [],consumes = [],生成= [],custom = []}”映射到public java.lang.String demo.controller.AjaxController.index() Info:Mapped“{[/ ajax/demo1],methods = [GET],params = [],headers = [],consumes = [],产生=信息:映射的URL路径[/ ajax]到处理器'ajaxController' 信息:映射的URL路径[/ ajax /] [],定制= []}“到公共java.lang.String demo.controller.AjaxController.demo1() 信息: *]到处理程序'ajaxController' – Rishabh