1、在action定义一个全局变量如:
private Map<String, Object> dataMap;
2、控制层方法
说明:主要的目的是把我们定义的Map转为Json对象,然后输出到前台
public String valiDateCode(){ HttpServletRequest request = ServletActionContext.getRequest(); HttpSession session = request.getSession(); String code = (String) session.getAttribute("code"); dataMap= new HashMap<String, Object>(); dataMap.put("msg", "success"); dataMap.put("code", code); return "dataMapData"; }3、在struts.xml配置文件中配置如:
<struts>
<package name="web-package" namespace="/" extends="struts-default,json-default"> <action name="pb_*" class="com.fs.web.action.PetitionBusinessAction" method="{1}"> <result name="dataMapData" type="json"> <param name="root">dataMap</param> </result> </action> </package></struts>注意的是:配置时加上result标签type="json",param标签name="root"