spring3---json
在applicationContext.xml中加入
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<util:list id="beanList">
<ref bean="mappingJacksonHttpMessageConverter" />
</util:list>
</property>
</bean>
<bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
这样在action方法加返回bean或map,spring3会把它处理成json格式。如:
@Controller
public class JsonTestController {
@RequestMapping("/getJsonList")
@ResponseBody
public List<ItemTest> searchItemsList()
{
ItemTest item = new ItemTest();
item.setItemnum("123");
item.setItemDesc("123desc");
ItemTest item1 = new ItemTest();
item1.setItemnum("456");
item1.setItemDesc("456desc");
List<ItemTest> list = new ArrayList<ItemTest>();
list.add(item);
list.add(item1);
return list;
}
}
访问getJsonList.do返回的就是json形式的数据,注意要加上 @ResponseBody
================================================
spring3---ajax---json
网上搜索到一般都是这样:
$(function(){
$("#doImport").click(
function(){
var importInfo = $("#importInfo").serializeObject();
$.ajax({
url:"${base}cmb/doImport.do",
contentType :"application/json",
cache:false,
type:"POST",
data:JSON.stringify(importInfo),
dataType:"json",
success: function(data){
$('#result').val(data.message)
alert(data.message);
},
error:function(){
alert("失败");
}
});
});
});
我觉得这个方法写得太复杂了,于是改了它:
function doImport(){
$.ajaxSetup({ cache:false });
var data = {excelFileName:$("#excelFileName").val(),
sheetName:$("#sheetName").val(),
tableName:$("#tableName").val(),
startRow:$("#startRow").val(),
startCol:$("#startCol").val()};
$.getJSON("${base}cmb/doImport.do",data,function(form){
$("#result").val(form.message);
alert(form.message); });
}
action:
@RequestMapping("/cmb/doImport.do")
public @ResponseBody Map<String,String> doImport(HttpServletRequest request, ModelMap model){
System.out.println("===doImport===>>" + DateUtil.getCurrentDateTime());
String excelFileName = request.getParameter("excelFileName");
String sheetName = request.getParameter("sheetName");
String tableName = request.getParameter("tableName");
int startRow = Integer.parseInt(request.getParameter("startRow"));
int startCol = Integer.parseInt(request.getParameter("startCol"));
System.out.println("excelFileName=" + excelFileName);
System.out.println("sheetName=" + sheetName);
System.out.println("tableName="+tableName);
System.out.println("startRow="+startRow);
System.out.println("startCol="+startCol);
Map<String,String> result = new HashMap<String,String>();
result.put("message", "导入成功!");
return result;
}
注意以下几点:
1. ajax必须清缓存:$.ajaxSetup({ cache:false });
2. 将表单的数据组成json格式:
var data = {excelFileName:$("#excelFileName").val(),
sheetName:$("#sheetName").val(),
tableName:$("#tableName").val(),
startRow:$("#startRow").val(),
startCol:$("#startCol").val()};
# XX与表单组件中的ID对应,如
<input type="hidden" id="excelFileName" value="${excelFileName}" />
3.表单上必须有一个地方能存放返回值:
<input type="hidden" id="message" />
action中的返回值为json格式,
$("#result").val(form.message) 这句话将返回值中的result属性取出赋给表单中的message