//strut json配置
<package name="mall_theme_ajax" extends="json-default" namespace="/themeAjax">
<action name="*_Ajax" class="cztAction" method="{1}_Ajax">
<interceptor-ref name="json" /><!--处理以JSON文本提交的请求-->
<result type="json" name="success">
</result> <!--将action的bean属性以json字符串返回浏览器-->
</action>
</package>
//action
public String selectThemeMode_Ajax() throws IOException{
List<String> themeModeList=queryThemeModeList();
HttpServletResponse response=ServletActionContext.getResponse();
//声明JSONObject //以下实现将JSON字符串传到前台
response.setContentType("text/json");
response.setCharacterEncoding("UTF-8");
PrintWriter out = null;
String jsonStr="[";
try { for(String np:themeModeList){
jsonStr+="{\"name\":\""+np.split(",")[0]+"\",\"num\":\""+np.split(",")[1]+"\"},"; }
jsonStr=jsonStr.substring(0, jsonStr.length()-1);
jsonStr+="]";
out = response.getWriter();
System.out.println(jsonStr);
out.print(jsonStr);
} catch (IOException e) {
System.out.println("FareAction:queryFareDetail" + e.getMessage());
}finally {
out.flush();
out.close();
}
return "success";
}
//js 代码
//ajax选择模板
function selectThemeModeAjax(pNum){
//$("#themeModeList").css("display","block"); $.ajax({
type:'GET',
url:'/themeAjax/selectThemeMode_Ajax.htm',
data:{},//发送的参数
dataType:'json',
error:function(){
alert("加载失败,请重试!");
return false;
},
success:function(data){
for(var o in data){
//alert(o);
//alert(data[o]);
alert("text:"+data[o].name+" value:"+data[o].num );
}
}
}); }