NutzCN Logo
问答 模拟httppost请求时,无法在action获取到entity的请求参数.
发布于 2206天前 作者 qq_046f8286 5438 次浏览 复制 上一个帖子 下一个帖子
标签:

然后还有@Param (是否是代码中的setMethodArgs这个方法设置,还是在其它地方设置).以及adapt的实现方法,这里只定义了一个接口,没有实现方法.

 public void process(ActionContext ac) throws Throwable {
        List<String> phArgs = ac.getPathArgs();
        Object[] args = adaptor.adapt(ac.getServletContext(),
                                      ac.getRequest(),
                                      ac.getResponse(),
                                      phArgs.toArray(new String[phArgs.size()]));
        ac.setMethodArgs(args);
        doNext(ac);
    }

获取到的 args :
[{}, org.apache.catalina.connector.RequestFacade@39e308c9]

设置entity代码:

	public String sendHttpPost(String httpUrl, String params, String contentType) {
		HttpPost httpPost = new HttpPost(httpUrl);// 创建httpPost
		try {
			// 设置参数
			StringEntity stringEntity = new StringEntity(params, "UTF-8");
			stringEntity.setContentEncoding("UTF-8");
			stringEntity.setContentType(contentType);
			httpPost.addHeader("Content-Type", contentType);
			httpPost.setEntity(stringEntity);
		} catch (Exception e) {
			e.printStackTrace();
		}
                //调的是execute 方法.
		return sendHttpPost(httpPost);
	}
13 回复

contentType是什么? params的内容是什么

方法能够访问到,但是params无法带过来.

params : {"applyStatusIds":"268"}
contentType: "application/json"

@At("/querySiWelfareApplyFromView")
	@Ok("json")
	public Object querySiWelfareApplyFromView(@Param("..") Map map, HttpServletRequest request) {
		String queryString = request.getQueryString();
		Object o=siWelfareApplyService.querySiWelfareApplyFromView(map);
		return o;
	}
action 接收的代码

哦, json, 那得用@AdaptBy哦

底层request里面的params里面是没有值的,而且最后给setMethodArgs的时候,也是空.

那就是adaptor要做的事,把json字符串转为map, 然后进行注入

好吧,我试试。有问题再来问你。

如果我换成text的contentType 是不是就不需要适配器?

也不行,用form才不需要

其实我就是模拟form的一起ajax请求,可是form掉action的方法时,是不用json 的@AdaptBy的,所以就像解决这个适配器的问题。昨天我们用json的适配器,前台的值过来的时候,会出错。

有办法实现后台模拟form的请求嘛?action在不加json适配器的情况下。

你得看看一个普通form表单请求了

是这种的 application/x-www-form-urlencoded

然后内容是xx=yyy&xxx=zzz

添加回复
请先登陆
回到顶部