jsp需要用到EL表达式
@RequestMapping("/teststring") public String testString(Model model){ System.out.println("testString方法执行了。。"); User user = new User(); user.setUsername("李"); user.setPassword("2001"); user.setAge(20); model.addAttribute("user",user); return "success"; }jsp用到EL表达式
@RequestMapping("/testModelView") public ModelAndView testModelView(){ ModelAndView mv = new ModelAndView(); System.out.println("testString方法执行了。。"); User user = new User(); user.setUsername("李"); user.setPassword("2001"); user.setAge(20); mv.addObject("user",user); mv.setViewName("success"); return mv ; }首先导入jQuery.min.js 注:jQuery API 中文版 点击此处查看
<script src="js/jquery.min.js"></script> <script> $(function(){ $("#btn").click(function () { //alert("Hello ajax"); $.ajax({ url:"user/testAjax", contentType:"application/json;charset=UTF-8", data:'{"username":"呵呵","password":"123e","age":12}', dataType:"json", type:"post", success:function (data) { //data服务器响应的json数据,进行解析 alert(data); alert(data.username); alert(data.age); } }); }); }); </script> url:一个用来包含发送请求的URL字符串contentType:(默认: “application/x-www-form-urlencoded”) 发送信息至服务器时内容编码类型。默认值适合大多数情况。如果你明确地传递了一个content-type给 $.ajax() 那么他必定会发送给服务器(即使没有数据要发送)data:发送到服务器的数据。将自动转换为请求字符串格式。GET 请求中将附加在 URL 后。查看 processData 选项说明以禁止此自动转换。必须为 Key/Value 格式。如果为数组,jQuery 将自动为不同值对应同一个名称。如 {foo:[“bar1”, “bar2”]} 转换为 “&foo=bar1&foo=bar2”。dataType:预期服务器返回的数据类型。如果不指定,jQuery 将自动根据 HTTP 包 MIME 信息来智能判断,比如XML MIME类型就被识别为XML。在1.4中,JSON就会生成一个JavaScript对象,而script则会执行这个脚本。随后服务器端返回的数据会根据这个值解析后,传递给回调函数。可用值: “xml”: 返回 XML 文档,可用 jQuery 处理。“html”: 返回纯文本 HTML 信息;包含的script标签会在插入dom时执行。“script”: 返回纯文本 JavaScript 代码。不会自动缓存结果。除非设置了"cache"参数。’’‘注意:’’'在远程请求时(不在同一个域下),所有POST请求都将转为GET请求。(因为将使用DOM的script标签来加载)“json”: 返回 JSON 数据 。“jsonp”: JSONP 格式。使用 JSONP 形式调用函数时,如 “myurl?callback=?” jQuery 将自动替换 ? 为正确的函数名,以执行回调函数。 “text”: 返回纯文本字符串