

项目
springmvc-jdbc
springmvc-mybatis
配置文件详解
rbac
ssm
json
package com.how2java.controller;import java.util.ArrayList;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;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.ResponseBody;import org.springframework.web.servlet.ModelAndView;import com.alibaba.fastjson.JSONObject;import com.how2java.pojo.Category;import com.how2java.service.CategoryService;// 告诉spring mvc这是一个控制器类@Controller@RequestMapping("")public class CategoryController { @Autowired CategoryService categoryService; @ResponseBody @RequestMapping("/submitCategory") public String submitCategory(@RequestBody Category category) { System.out.println("SSM接受到浏览器提交的json,并转换为Category对象:"+category); return "ok"; } @ResponseBody @RequestMapping(value = "/getOneCategory/{id}",method=RequestMethod.GET) public String getOneCategory(Category c) { Category category = categoryService.get(c.getId()); JSONObject json= new JSONObject(); json.put("category", JSONObject.toJSON(category)); return json.toJSONString(); } @ResponseBody @RequestMapping("/getManyCategory") public String getManyCategory() { List<Category> cs = new ArrayList<>(); for (int i = 0; i < 10; i++) { Category c = new Category(); c.setId(i); c.setName("分类名称:"+i); cs.add(c); } return JSONObject.toJSON(cs).toString(); }}
$('#sender').click(function(){ var url="getManyCategory"; $.post( url, function(data) { console.log(data); var categorys = $.parseJSON(data); console.log(categorys.length); for(i in categorys){ var old = $("#messageDiv").html(); var category = categorys[i]; $("#messageDiv").html(old + "<br>"+category.id+" ----- "+category.name); } }); });
<body> <form > id:<input type="text" id="id" value="123" /><br/> 名称:<input type="text" id="name" value="category xxx"/><br/> <input type="button" value="提交" id="sender"> </form> <div id="messageDiv"></div> <script> $('#sender').click(function(){ var id=document.getElementById('id').value; var name=document.getElementById('name').value; var category={"name":name,"id":id}; var jsonData = JSON.stringify(category); var page="submitCategory"; $.ajax({ type:"post", url: page, data:jsonData, dataType:"json", contentType : "application/json;charset=UTF-8", success: function(result){ } }); alert("提交成功,请在Tomcat控制台查看服务端接收到的数据"); }); </script> </body>
restful
@Autowired
CategoryService categoryService;
@ResponseBody
@RequestMapping(value = "/getOneCategory/{id}",method=RequestMethod.GET)
public String getOneCategory(Category c) {
Category category = categoryService.get(c.getId());
JSONObject json= new JSONObject();
json.put("category", JSONObject.toJSON(category));
return json.toJSONString();
}
<input type="button" value="通过AJAX获取一个Hero对象---" id="sender">
<div id="messageDiv"></div>
<script>
$('#sender').click(function(){
var url="getOneCategory/1";
$.get(
url,
function(data) {
var json=JSON.parse(data);
var name =json.category.name;
var id = json.category.id;
$("#messageDiv").html("分类id:"+ id + "<br>分类名称:" +name );
});
});