前台JSP页面:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <script type="text/javascript" src="js/jquery.min.js"></script> <a href="/day01/spring/mvc">submit</a> <input type="button" id="bt1" value="ajax" /> <script type="text/javascript"> var data = { "name":"jacky", "age":"20" } /* // 请求Key/Value(无需设置请求类型) $(function(){ $("#bt1").click(function(){ $.ajax({ type:"POST", url:"/day01/spring/mvc", //contentType:"application/json;charet=utf-8", data:data, success:function(data){ alert(data) } }) }) }) */ // 请求Json字符串(需要设置请求类型) $(function(){ $("#bt1").click(function(){ $.ajax({ type:"POST", url:"/day01/spring/mvc", contentType:"application/json;charet=utf-8", data:JSON.stringify(data), success:function(data){ alert(data) } }) }) }) </script>
Controller源码:
package com.spring; import java.io.PrintWriter; import java.util.HashMap; import java.util.Map; import net.sf.json.JSONObject; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView; import com.model.User; @ControllerAdvice @RequestMapping("/spring") public class TestControllers { // 无返回值的Controller结合ajax实现局部刷新,有返回值的Controller实现页面的跳转与数据的响应 /* * 将返回值已Object的形式输出到当前页面 * PrintWriter 输出对象 * */ /* @RequestMapping("/mvc") public void showPerson(PrintWriter printWriter, @RequestBody String param) { System.out.println(param); // 写入Response的Body体 String st ="{"name":"zhangsna","name1":"zhangsna"}"; Map map = JSONObject.fromObject(st); System.out.println(map); printWriter.println(map); } */ /* * POJO(JavaBean) 接收前台传递的key/value, * @ResponseBody 将User对象输出至Response的Body中 */ /* @RequestMapping("/mvc") public @ResponseBody User showPerson(User user) { System.out.println(user); return user; } */ /* * 接收前台传递的Json字符串, * @ResponseBody 将User对象输出至Response的Body中 */ /*@RequestMapping("/mvc") @ResponseBody public Map showPerson( @RequestBody String params) { @SuppressWarnings("unchecked") Map<String,Object> map = JSONObject.fromObject(params); System.out.println(map); return map; }*/ /* * ModelAndView 视图(view)和模型(数据) * Model 内置的一个数据对象 * */ @RequestMapping("/demo1") public ModelAndView execute(Model model) { // 设置数据模型 model.addAttribute("name", "Jacky"); model.addAttribute("age", "20"); model.addAttribute("school", "陕西理工大学"); //设置视图数据模型 ModelAndView mav = new ModelAndView(); mav.setViewName("return"); mav.addObject("data",model); return mav; } /* * 自定义参数 */ @RequestMapping("/demo2") public ModelAndView execute() { Map data = new HashMap(); Map map = new HashMap(); // 设置数据模型 map.put("name", "Jacky"); map.put("age", "20"); map.put("school", "陕西理工大学"); data.put("data", map); //返回视图数据模型 return new ModelAndView("return",data); } /* * @RequestParam("user") user和前台传递的值保持一致 * */ @RequestMapping("/demo3") public void execute( PrintWriter writer ,@RequestParam("user") String name) { writer.print(name); } /* * @RequestParam("user") user和前台传递的值保持一致 * RequestMethod.GET 请求方式 */ @RequestMapping(value=("/demo04"),method={RequestMethod.GET,RequestMethod.POST}) public void executes( PrintWriter writer ,@RequestParam("user") String name) { writer.print(name); } /* * method={RequestMethod.GET,RequestMethod.POST} 请求方式 * @PathVariable("testId") 将url中的变量绑定到Controller的形参中 * 可以实现rest接口 */ @RequestMapping(value=("/delete/{testId}"),method={RequestMethod.GET,RequestMethod.POST}) public void execut( PrintWriter writer ,@PathVariable("testId") String id) { writer.print("Delete ID IS :"+id); } }
响应视图JSP:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>returnJsp</title> </head> <body> This is return JSP page. <br> 返回数据:${data} </body> </html>
常见错误分析:
1.运行Tomcat时找不到类错误,去tomcat看,项目是部署到Tomcat的(MyEclipse和Idea在对应的输出路径处应该也有jar包的依赖关系)
2.处理器根据url找不到handler
3.转发jsp页面找不到
4.json数据转Java对象
5.浏览器406错误