SpringMVC处理ajax请求
2013-11-20 21:35
363 查看
由于之前我写过几篇关于如何搭建SpringMVC项目的日志,故在本文中不在写各种配置怎么写。只写涉及到处理ajax请求的代码。如果有想了解SpringMVC项目怎么搭建的朋友,请选择如下合适你的链接进去交流。
SpringMVC+MyBatis+Spring整合
SpringMVC+Spring+Hibernate整合
不整合任何框架的SpringMVC环境
好,正文开始
Controller的代码
网页的写法
控制台打印结果
页面加载后的效果
以上是一个非常简单的ajax操作,不包含json的传值,下面贴一个包含了json传值的代码
Controller中的代码
@RequestMapping("queryuseradvice")
@ResponseBody
public ModelMap queryUserAdvice(ModelMap model,
@RequestParam int pageNo,
@RequestParam int pageSize){
int count=this.qyjUserAdviceService.queryCount();//从数据库查满足条件的数据的数量
int totalPage=count%pageSize==0?count/pageSize:count/pageSize+1;//计算共有几页
//将总页数和所有要传回页面的数据集合存入model中
model.addAttribute("totalPage", totalPage);
model.addAttribute("adviceList", this.qyjUserAdviceService.queryAdvice(pageNo, pageSize));
return model;
}
js代码(只包含ajax部分)
function queryAdviceBy(pageNo, pageSize) {
$("#none").hide();
$.ajax({
type:"post",
url:"adviceTest/queryuseradvice",
cache:false,
dataType:"json",
data:{
pageNo:pageNo,
pageSize:pageSize
},
success:function(data) {
//省略回调函数的内容
//想得到Controller中传回的值得话像如下这样做
data.totalPage
data.adviceList
}
});
}
SpringMVC+MyBatis+Spring整合
SpringMVC+Spring+Hibernate整合
不整合任何框架的SpringMVC环境
好,正文开始
Controller的代码
package com.action; import java.io.UnsupportedEncodingException; import java.util.List; import javax.annotation.Resource; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.SessionAttributes; import com.bean.User; import com.service.IUserService; /** * action层我也是用注解配的,这里面的注解@RequestMapping的具体作用见上一篇SpringMVC入门笔记 * @author 百木森森 * */ @Controller @RequestMapping("testPath") public class TestAction { @Resource private IUserService userService; @RequestMapping("ajaxTest") @ResponseBody public ModelMap ajaxTest(){ System.out.println("进ajax的action了"); ModelMap model=new ModelMap(); model.addAttribute("key", "I'm value"); return model; } }
网页的写法
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <script type="text/javascript" src="<%=path%>/js/jquery-1.10.2.js"></script> <script type="text/javascript"> $(function(){ alert("ajax一级准备!"); $.ajax({ url:"<%=path%>/testPath/ajaxTest.action", type:"post", success:function(data){ alert(data.key); } }) }) </script> <title>Insert title here</title> </head> <body> <h1>success!!!</h1><br/> </body> </html>
控制台打印结果
页面加载后的效果
以上是一个非常简单的ajax操作,不包含json的传值,下面贴一个包含了json传值的代码
Controller中的代码
@RequestMapping("queryuseradvice")
@ResponseBody
public ModelMap queryUserAdvice(ModelMap model,
@RequestParam int pageNo,
@RequestParam int pageSize){
int count=this.qyjUserAdviceService.queryCount();//从数据库查满足条件的数据的数量
int totalPage=count%pageSize==0?count/pageSize:count/pageSize+1;//计算共有几页
//将总页数和所有要传回页面的数据集合存入model中
model.addAttribute("totalPage", totalPage);
model.addAttribute("adviceList", this.qyjUserAdviceService.queryAdvice(pageNo, pageSize));
return model;
}
js代码(只包含ajax部分)
function queryAdviceBy(pageNo, pageSize) {
$("#none").hide();
$.ajax({
type:"post",
url:"adviceTest/queryuseradvice",
cache:false,
dataType:"json",
data:{
pageNo:pageNo,
pageSize:pageSize
},
success:function(data) {
//省略回调函数的内容
//想得到Controller中传回的值得话像如下这样做
data.totalPage
data.adviceList
}
});
}
相关文章推荐
- SpringMVC3.2 处理Ajax请求时返回json字符串的中文乱码问题
- Ajax响应中文乱码 [SpringMVC使用@ResponseBody处理Ajax请求]
- Ajax响应中文乱码 [SpringMVC使用@ResponseBody处理Ajax请求]
- SpringMVC:ajax请求处理
- Ajax响应中文乱码 [SpringMVC使用@ResponseBody处理Ajax请求]
- SpringMVC对异常进行全局处理,并区分对待ajax和普通请求
- SpringMVC处理AJAX请求
- springMVC拦截器处理ajax请求及数据返回
- springmvc拦截器针对ajax()请求的带特殊请求头的处理
- Problem: springmvc 处理Ajax请求的controller中跳转语句无效
- Ajax响应中文乱码 [SpringMVC使用@ResponseBody处理Ajax请求]
- springmvc处理ajax请求
- Ajax响应中文乱码 [SpringMVC使用@ResponseBody处理Ajax请求]
- SpringMVC处理ajax请求
- springMVC处理Ajax请求的一个错误
- springmvc处理ajax请求
- SpringMVC经典系列-13使用SpringMVC处理Ajax请求---【LinusZhu】
- springmvc处理ajax请求
- springMVC拦截器处理ajax请求及数据返回
- SpringMVC经典系列-13使用SpringMVC处理Ajax请求---【LinusZhu】