我正在使用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());
}
}
Getters and Setters方法:
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的值,它返回false值。我不确定我在哪里做错了。我试图以多种方式解决问题,但无法解决问题。你可以请你纠正我并向我推荐我的问题。
请在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>
答案 0 :(得分:1)
当然,您将获得false
的值。
在你的setter中你返回boolean
,它确实设置了projectDescriptionList
的值,但最后每次都返回硬代码false
。
解决方案,
在你的getter-setter中;
// optional, just stick to setter rules, set the value and don't return anything.
public setProjectDescriptionList(List<String> projectDescriptionList) {
this.projectDescriptionList = projectDescriptionList;
}
在for循环中的doPost
,问题的第三部分代码
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);