<!-- 有关ajax的配置 完成请求和注解POJO的映射 --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="cacheSeconds" value="0" /> <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean> </list> </property> </bean>
发出的请求必须加上此头 req.setRequestHeader("accept","application/json"); spring才知道发出的是ajax请求
客户端代码如下:
<script> function createAjaxObj(){ var req; if(window.XMLHttpRequest){ req = new XMLHttpRequest(); }else{ req = new ActiveXObject("Msxml2.XMLHTTP"); //ie } return req; } function sendAjaxReq(){ var req = createAjaxObj(); req.open("get","ajax.do?method=test1&uname=张三"); req.setRequestHeader("accept","application/json"); //必须加上此头,spring才知道发出的是ajax请求 req.onreadystatechange = function(){ eval("var result="+req.responseText); //eval函数 document.getElementById("div1").innerHTML=result[0].uname; } req.send(null); } </script>
服务器端代码如下:
@Controller @RequestMapping("ajax.do") public class AjaxController { @RequestMapping(params="method=test1",method=RequestMethod.GET) public @ResponseBody List<User> test1(String uname) throws Exception//@ResponseBody此注解用于处理ajax请求 返回值可以是任何值,spring会自动转换为json对象 { String uname2 = new String(uname.getBytes("iso8859-1"),"utf-8"); System.out.println(uname2); System.out.println("AjaxController.test1()"); List<User> list = new ArrayList<User>(); list.add(new User("大家","123")); list.add(new User("斯柯达","456")); return list;//注意返回此时值可以不再是一般的String } }Spring中的拦截器
public class Interceptor1 implements HandlerInterceptor { @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("最后执行!!!一般用于释放资源!!"); } @Override public void postHandle(HttpServletRequest request,HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("Action执行之后,生成视图之前执行!!"); } @Override public boolean preHandle(HttpServletRequest request,HttpServletResponse response, Object handler) throws Exception { System.out.println("action之前执行!!!"); return true; //继续执行action } }
/*XML中如何配置。如下为示例代码: <mvc:interceptors> <bean class="com.sxt.interceptor.MyInterceptor"></bean> <!-- 拦截所有springmvc的url! --> <mvc:interceptor> <mvc:mapping path="/user.do" /> <!--<mvc:mapping path="/test/*" />--> <bean class="com.sxt.interceptor.MyInterceptor2"></bean> </mvc:interceptor> </mvc:interceptors> */ public class Interceptor2 extends HandlerInterceptorAdapter { @Override public boolean preHandle(HttpServletRequest request,HttpServletResponse response, Object handler) throws Exception { System.out.println("MyInterceptor2.preHandle()"); return true; //继续执行action } //下面的根据需要而定 // @Override // public void afterCompletion(HttpServletRequest request,HttpServletResponse response, Object handler, Exception ex)throws Exception { // super.afterCompletion(request, response, handler, ex); // } // // @Override // public void postHandle(HttpServletRequest request,HttpServletResponse response, Object handler,ModelAndView modelAndView) throws Exception { // super.postHandle(request, response, handler, modelAndView); // } }