在后台都能正确输出map,我猜是map没有正确序列化成json,求高人指点
package com.lhw.action;
import com.lhw.service.news.GetNewsTitleIndexService;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.json.annotations.JSON;
import org.springframework.stereotype.Controller;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
/**
* Created by lhw on 2016/10/27.
*/
@Controller("getNewsTitle")
public class GetNewsTitleAction extends ActionSupport{
private Map<String,String> map = new HashMap<>();
private transient GetNewsTitleIndexService getNewsTitleIndexService;
@Override
public String execute() throws Exception {
//调用service读取数据放到map中
map = getNewsTitleIndexService.getNewsTitleIndex(0,5);
System.out.println(map);
return SUCCESS;
}
public GetNewsTitleIndexService getNewsTitleIndexService() {
return getNewsTitleIndexService;
}
@Resource(name = "getNewsTitleIndexService")
public void setGetNewsTitleIndexService(GetNewsTitleIndexService getNewsTitleIndexService) {
this.getNewsTitleIndexService = getNewsTitleIndexService;
}
public Map<String, String> getMap() {
return map;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="Registration" extends="struts-default">
<action name="user*" class="com.lhw.action.User{1}Action">
<result name="success">/index.jsp</result>
<result name="registFail">/regist.jsp</result>
<result name="loginSuccess">${loginPreUrl}</result>
<result name="loginFail">/login.jsp</result>
<result name="logoutSuccess">${preUrl}</result>
</action>
</package>
<constant name="struts.i18n.encoding" value="UTF-8"/>
<package name="example" extends="json-default">
<action name="getNewsTitle" class="com.lhw.action.GetNewsTitleAction">
<!-- 配置类型的json的Result -->
<result type="json">
<!-- 为该Result指定参数 -->
<param name="noCache">true</param>
<param name="contentType">text/html</param>
<!-- 设置只序列Action的map属性 -->
<param name="includeProperties">map</param >
<param name="root">map</param >
</result>
</action>
<action name="*">
<result>{1}.jsp</result>
</action>
</package>
</struts>
$(document).ready(function () {
$.ajax({
url : 'getNewsTitle',//后台处理程序
type : 'post',
dataType : 'json',
success : function(data){
alert("正确");
for (var propName in data) {
$("#newsTitleUl")
.append("<li>" +
"<div style='float: left'>" + propName + "</div>" +
"<div style='text-align: right;'>" + data[propName] + "</div>" +
"</li>");
alert("<li>" +
"<div style='float: left'>" + propName + "</div>" +
"<div style='text-align: right;'>" + data[propName] + "</div>" +
"</li>")
}
},error : function(){
alert("错误");
}
}
)})