java端:
spring-servlet.xml中的部分配置信息:
请求对应的Java类:
flex端:
javascript端:
-------------------------------
flex和java交互其实可以用blazeDS传递对象太方便了在java端配置好blazsDS在flex端在对应Java对象的bean上添加
[Bindable]
[RemoteClass(alias="com.entity.User")]
就可以了
spring-servlet.xml中的部分配置信息:
<!-- 对模型视图名称的解析,即在模型视图名称添加前后缀
-->
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="mediaTypes">
<map>
<entry key="html" value="text/html"/>
<entry key="xml" value="application/xml"/>
<entry key="json" value="application/json"/>
</map>
</property>
<property name="viewResolvers">
<list>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</list>
</property>
<property name="defaultViews">
<list>
<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"/>
</list>
</property>
</bean>
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<bean
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
</list>
</property>
</bean>
请求对应的Java类:
@Controller
@Scope("prototype")
@RequestMapping("/user_service")
public class UserService {
@RequestMapping(value="/create_user",method=RequestMethod.POST)
@ResponseBody
public String springMVCTestUser(@RequestBody User user) {
// spring mvc中获取request对象的方法
HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder
.getRequestAttributes()).getRequest();
return "";
}
}
flex端:
package com.util
{
import com.util.Constants;
import flash.net.URLVariables;
import mx.rpc.events.ResultEvent
import mx.rpc.http.mxml.HTTPService;
public class HttpServiceWrapper
{
public static const HTTP_METHOD_POST:String="POST";
public static const HTTP_METHOD_GET:String="GET";
private var _method:String;
private var _url:String;
public function HttpServiceWrapper(url:String, method:String)
{
this._url=url;
this._method=method;
}
public function execute(resultHandler:Function, faultHandler:Function, params:Object=null, isLogin:Boolean=false):void
{
var httpService:HTTPService=new HTTPService;
httpService.url=Constants.SERVER + this._url;
httpService.method=this._method;
httpService.useProxy=false;
httpService.requestTimeout = 10;
httpService.headers["JSESSIONID"]=Constants.SESSIONID;
if(isLogin)
{
httpService.contentType="application/json";
}
httpService.contentType=contentType;
httpService.addEventListener("result", resultHandler);
httpService.addEventListener("fault",faultHandler);
httpService.send(params);
}
}
private function resultHandler(event:ResultEvent):void
{
// 这里可以获取请求成功后返回的sessionid
var jsessionId:String = event.currentTarget.headers["JSESSIONID"];
}
}
javascript端:
$.ajax({
type:'post',
url: 'user/addOrUpdate',
contentType : 'application/json',
dataType:'json',
data : strUser,
success:function(data){
$('#win').window('close'); // close window
$('#userTable').datagrid('reload');
$.messager.alert('提示',data.message,'info');
}
});
// 或者这个都可以,这个我没有测,只是参考这个写的上面flex的代码
$.post("user/addOrUpdate",strUser,function(data){
$('#win').window('close'); // close window
$('#userTable').datagrid('reload');
$.messager.alert('提示',data.message,'info');
},'json');
-------------------------------
flex和java交互其实可以用blazeDS传递对象太方便了在java端配置好blazsDS在flex端在对应Java对象的bean上添加
[Bindable]
[RemoteClass(alias="com.entity.User")]
就可以了