spring mvc传值(json格式)

9 篇文章 0 订阅
7 篇文章 0 订阅
java端:
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")]
就可以了
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值