extjs跟Spring mvc3中的数据交互,依然用的是JSON,步骤如下
1) 注册SPRING MVC3中的JSON解析器
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
</list>
</property>
</bean>
注意把jackson-json jar包放到LIB下
2)前端:
Ext.Ajax.request({
url : 'doSomething.htm',
method: 'POST',
headers: { 'Content-Type': 'application/json' },
params : { "test" : "testParam" },
jsonData: {
"username" : "admin",
"emailId" : "
[email protected]"
},
success: function (response) {
var jsonResp = Ext.util.JSON.decode(response.responseText);
Ext.Msg.alert("Info","UserName from Server : "+jsonResp.username);
},
failure: function (response) {
var jsonResp = Ext.util.JSON.decode(response.responseText);
Ext.Msg.alert("Error",jsonResp.error);
}
});
3) 后端
@Controller
public class DataController
{
@RequestMapping(value = "/doSomething", method = RequestMethod.POST)
@ResponseBody
public User handle(@RequestBody User user) throws IOException
{
System.out.println("Username From Client : "+user.getUsername());
System.out.println("EmailId from Client : "+user.getEmailId());
user.setUsername("SivaPrasadReddy");
user.setEmailId("
[email protected]");
return user;
}
}