下面是整个整合测试的代码:
ajax01.html
TestController
web.xml
springmvc.xml
applicationContext.xml
<!DOCTYPE html> <html> <head> <title>index.html</title> <meta name="keywords" content="keyword1,keyword2,keyword3"> <meta name="description" content="this is my page"> <meta name="content-type" content="text/html; charset=gb2312"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> <script type="text/javascript" charset="UTF-8"> var param = "ajaxget"; var xmlHttpRequest = null;//作为全局变量,否则在回调函数中报notdefined function ajaxSubmit(){ if(window.ActiveXObject){ xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP"); }else if(window.XMLHttpRequest){ xmlHttpRequest = new XMLHttpRequest(); } if(xmlHttpRequest != null){ //document.write("xxx"); xmlHttpRequest.open("GET", "./testAjax?param="+param, true); xmlHttpRequest.onreadystatechange = ajaxCallBack;//没有括号 xmlHttpRequest.send(null); } } function ajaxCallBack(){ if(xmlHttpRequest.readyState == 4){ if(xmlHttpRequest.status == 200){ var responseText = xmlHttpRequest.responseText;//没有括号 document.getElementById("div1").innerHTML = responseText;//innerHTML没有括号 } } } </script> </head> <body> <input type="button" value="check" id="check" onclick="ajaxSubmit();"> <br> <div id="div1"></div> </body> </html> |
package priv.lirenhe.js.controller; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller public class TestController { public TestController() { System.out.println("------TestController------"); } @RequestMapping(value="/testAjax",method=RequestMethod.GET) public void testAjax(HttpServletRequest req,HttpServletResponse resp){ System.out.println("testAjax"); String param = null; param = req.getParameter("param"); System.out.println(param); PrintWriter pw = null; try { pw = resp.getWriter(); pw.print(param); } catch (IOException e) { e.printStackTrace(); }finally{ pw.flush(); pw.close(); System.out.println("finally"); } } } |
web.xml配置:注意不要使用<web:xxx>的标签 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
<?xml version="1.0" encoding="UTF-8"?> <!-- 必须有带mvc的 --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="priv.lirenhe.js.*"></context:component-scan> <mvc:annotation-driven /> <mvc:resources location="/" mapping="/**"/> <bean id="modelAndView" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <constructor-arg name="prefix" value="/"/> <constructor-arg name="suffix" value=".html"/> </bean> </beans> <!-- 包扫描器 注解驱动 静态资源 前缀后缀 --> |
applicationContext.xml配置: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" |
如果不能加载的话就是web.xml配置的问题。