我正在使用JSON数组从Servlet中的Db获取值。我正在起诉以下代码
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
List<ProjectDto> act=new ArrayList<ProjectDto>();
act=ActivitiesDao.getActivitiesDetails();
JSONArray jsonarray= new JSONArray();
for(int i=0;i<act.size();i++)
{
JSONObject obj = new JSONObject();
//obj.put("issueno", quiz.get(i).getIssueno());
obj.put("projectName",act.get(i).getProjectName());
obj.put("projectDescription",act.get(i).getProjectDescription());
obj.put("currentStatus", act.get(i).getCurrentStatus());
obj.put("area", act.get(i).getArea());
jsonarray.put(obj);
}
System.out.println("Json in Servelt"+ jsonarray.toString());
response.getWriter().print(jsonarray.toString());
}
}
我能够从DB成功获得价值。我已经在控制台中打印了值。
现在,我在其他一些Java文件中定义了一种方法来转换段落中的数据。现在,我通过以下方式在Servlet中调用该方法:
//方法:
public static List<String> convertParagraphs(String text){
List <String> convertedList= new ArrayList<String>();
if(text==null|| text==""){
}
else{
String[] paragraphs= text.split("\\|");
convertedList= Arrays.asList(paragraphs);
}
return convertedList;
}
在Servlet中调用此方法:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
List<ProjectDto> act=new ArrayList<ProjectDto>();
act=ActivitiesDao.getActivitiesDetails();
JSONArray jsonarray= new JSONArray();
for(int i=0;i<act.size();i++)
{
JSONObject obj = new JSONObject();
obj.put("projectName",act.get(i).getProjectName());
String ttextt= act.get(i).getProjectDescription();
obj.put("ProjectDescriptionList" , act.get(i).setProjectDescriptionList(NewsletterUtil.convertParagraphs(ttextt)));
obj.put("currentStatus", act.get(i).getCurrentStatus());
obj.put("area", act.get(i).getArea());
obj.put("ProjectDescriptionList" , act.get(i).setProjectDescriptionList(NewsletterUtil.convertParagraphs(ttextt)));
jsonarray.put(obj);
}
System.out.println("Json in Servelt"+ jsonarray.toString());
response.getWriter().print(jsonarray.toString());
}
}
获取和设置方法:
public class ProjectDto {
private String projectName;
private String projectDescription;
private String currentStatus;
private String area;
private List<String> projectDescriptionList= new ArrayList<String>();
private List<String> currentStatusList=new ArrayList<String>();
public List<String> getCurrentStatusList() {
return currentStatusList;
}
public void setCurrentStatusList(List<String> currentStatusList) {
this.currentStatusList = currentStatusList;
}
public List<String> getProjectDescriptionList() {
return projectDescriptionList;
}
public boolean setProjectDescriptionList(List<String> projectDescriptionList) {
this.projectDescriptionList = projectDescriptionList;
return false;
}
public String getCurrentStatus() {
return currentStatus;
}
public void setCurrentStatus(String currentStatus) {
this.currentStatus = currentStatus;
}
public String getArea() {
return area;
}
public void setArea(String area) {
this.area = area;
}
public String getProjectName() {
return projectName;
}
public void setProjectName(String projectName) {
this.projectName = projectName;
}
public String getProjectDescription() {
return projectDescription;
}
public void setProjectDescription(String projectDescription) {
this.projectDescription = projectDescription;
}
@Override
public String toString() {
return "ProjectDto [projectName=" + projectName
+ ", projectDescription=" + projectDescription
+ ", currentStatus=" + currentStatus + ", area=" + area
+ ", projectDescriptionList="
+ projectDescriptionList + "]";
}
}
问题:现在,我正在打印ProjectDescription List的值,它返回的是假值。我不确定我在哪里做错了。我试图以多种方式解决问题,但无法解决问题。您能纠正我,并就我的问题向我提出建议吗?
请在jsp中找到我的ajax代码
<script>
$.ajax({
type: "POST",
url: "./ProjectServlet",
success: function (responseText) {
var jsonData = JSON.parse(responseText);
var uniueTabs = getUniqueLists(jsonData);
for (var i = 0; i < uniueTabs.length; i++) {
$('#tabs').append('<li ><a href="#tab-content-' + i + '">' + uniueTabs[i].area + '</a></li>');
var div = '<div id="tab-content-' + i + '" class="tab-pane fade">';
for (var j = 0; j < uniueTabs[i].tabContent.length; j++) {
var obj = uniueTabs[i].tabContent[j];
div += '<p>' + obj.projectName + '</p>'+ '<p>' + obj.projectDescription + '</p>' + obj.currentStatus ;
}
$('.tab-content').append(div);
$('#tab-content-0').addClass('in active');
}
$('#tabs li').eq(0).addClass('active');
setListner();
function getUniqueLists(responseText) {
var resArr = [];
responseText.filter(function (x, i) {
if (resArr.indexOf(x.area) === -1) {
resArr.push(x.area);
}
})
//console.log(resArr);
return mergeDataAreaWise(resArr, responseText);
}
function mergeDataAreaWise(area, responseText) {
var tabList = [];
for (var i = 0; i < area.length; i++) {
tabList.push({
area: area[i],
tabContent: []
});
}
var prjlist;
var output;
var proh;
var status;
for (var i = 0; i < tabList.length; i++) {
for (var j = 0; j < responseText.length; j++) {
statuss = '<li>' + responseText[j].currentStatus + '</li>';
prjlist= responseText[j].projectDescriptionList;
/* output= prjlist.split("&");
proh=output.join("/n");
*/
//proh=prjlist.replace(/&/g, '<br>');
status =statuss.replace(/&/g, '<br>');
var Obj = {
projectName: '<h3>'+ responseText[j].projectName +'</h3>',
projectDescription: '<p>'+ prjlist + '<p>',
currentStatus: '<b>'+ "Current Status:" +'</b>' + status
}
var currentArea = responseText[j].area;
if (tabList[i].area === currentArea) {
tabList[i].tabContent.push(Obj);
}
}
}
console.log(tabList);
return tabList;
}
}
});
function setListner () {
$("#tabs a").click(function () {
$(this).tab('show');
});
}
</script>
最佳答案
当然,您将获得false
的值。
在您的setter中,您将返回一个boolean
,它确实设置了projectDescriptionList
的值,但最终每次都会返回硬代码false
。
解,
在你的吸毒者;
// optional, just stick to setter rules, set the value and don't return anything.
public setProjectDescriptionList(List<String> projectDescriptionList) {
this.projectDescriptionList = projectDescriptionList;
}
在您的
doPost
for循环中,相关代码的第三部分act.get(i).setProjectDescriptionList(NewsletterUtil.convertParagraphs(ttextt));
obj.put("ProjectDescriptionList" ,act.get(i).getProjectDescriptionList() );
obj.put("currentStatus", act.get(i).getCurrentStatus());
obj.put("area", act.get(i).getArea());
// I don't know why you wrote it twice.. I'm commenting it.
// obj.put("ProjectDescriptionList" , act.get(i).setProjectDescriptionList(NewsletterUtil.convertParagraphs(ttextt)));
jsonarray.put(obj);