一直很困action中返回type为json类型的数据

kevin-go 2014-01-25 11:43:51
<package name="emp" extends="json-default">
<action name="fetchEmps" class="empAction" method="fetchEmps">
<result name="success" type="json">
<param name="ignoreHierarchy">false</param>
<param name="root">pagination</param>
</result>
</action>
</package
有一个action是这么配置的,success的result返回json数据,action中的相应方法fetchEmps是这样的


public String fetchEmps(){
int count = this.empService.countAll();
this.pagination = new Pagination(this.page, Integer.valueOf(count), this.limit.intValue());
List ps = this.empService.listAll(this.pagination.getPage().intValue(), this.pagination.getPageSize());
this.pagination.setDatas(ps);
return "success";
}
我的疑惑是Struts2是怎么判断把哪个对象当做json数据返回的,方法里也没有显示把某个要返回的对象放入域啊,相应的action代码为:

import xxxxxxxx;
@Controller("empAction")
@Scope("prototype")
public class EmpAction extends BaseAction
{
private static final long serialVersionUID = 1L;
private IEmpService empService;
private String newEmp;
private String userName;
private boolean exists;

@Autowired
@Required
public void setEmpService(IEmpService empService)
{
this.empService = empService;
}

@JSON(serialize=false)
public IEmpService getEmpService() {
return this.empService;
}

public String fetchEmps()
{
int count = this.empService.countAll();
this.pagination = new Pagination(this.page, Integer.valueOf(count), this.limit.intValue());
List ps = this.empService.listAll(this.pagination.getPage().intValue(), this.pagination.getPageSize());
this.pagination.setDatas(ps);
return "success";
}
...全文
232 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
kevin-go 2014-01-25
  • 打赏
  • 举报
回复
引用 1 楼 coolbamboo2008 的回复:
你配置文件里配置的按json返回,struts2就当json处理,不管你返回的是啥
这个我知道,但是我的疑惑是我在方法里把某个list转换成了JSONObject对象了了,那sturts是不是就一定返回这个JSONObject对象,那如果我在方法里有两个JSONObject呢?比如 JSONObject json1=JSONObject.fromObject(list); JSONObject json1=JSONObject.fromObject(list); 那我怎么指定返回json1而不返回json2?
kevin-go 2014-01-25
  • 打赏
  • 举报
回复
引用 1 楼 coolbamboo2008 的回复:
你配置文件里配置的按json返回,struts2就当json处理,不管你返回的是啥
那是把Action类中的所有对象都返回到前端吗?
coolbamboo2008 2014-01-25
  • 打赏
  • 举报
回复
你配置文件里配置的按json返回,struts2就当json处理,不管你返回的是啥
jwd001 2014-01-25
  • 打赏
  • 举报
回复
<param name="root">pagination</param> 返回pagination属性
Mxreng 2014-01-25
  • 打赏
  • 举报
回复
直接转 把方法的String 改为void即可,我用是阿里巴巴的Json包
	/**
	 * 把对象转换成JSON后,再进行打印
	 * 
	 * @param obj
	 */
	public static void writeJson(HttpServletResponse response, Object obj) {
		String str = JSON.toJSONString(obj);
		try {
			response.setContentType("text/html;charset=utf-8");
			response.getWriter().write(str);
			response.getWriter().flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

67,549

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧