-1

下面是我想要获取特定管理员编号的名称输出的代码,管理员 Emailid,但我得到的输出是:列表 ---> com.demo.model.Administrator@91e143 与不同的数字,基本上我是java新手。请你在循环迭代中帮助我。

package com.demo.action;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.demo.model.Administrator;

import com.demo.model.AdministratorDAO;
import com.demo.model.AdministratorDemo;
import com.demo.model.JQueryDataTableParam;
import com.demo.model.JqueryDatatablesParamUtil;

public class AdministratorAction extends Action {

    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        System.out.println("in execute..");
        JQueryDataTableParam param = JqueryDatatablesParamUtil
                .getParam(request);


        String txt2=request.getParameter("txt1");
        //String select2=request.getParameter("select1");
        //request.setAttribute("e", "select2");

        //String select3=request.getParameter("select2");

        //System.out.println("txtValue->"+e);
        //System.out.println("txtValue->"+select2);
        System.out.println("txtValue->"+txt2);
        //String var = Administrator.isValidname(sData);
        String sEcho = param.sEcho;
        int iTotalRecords;
        int iTotalDisplayRecords;
        int start = param.iDisplayStart;
        System.out.println("start" + start);
        int last = param.iDisplayLength +param.iDisplayStart;
        System.out.println("last" + last);
        int sortColumnIndex = param.iSortColumnIndex;
        System.out.println("sortColumnIndex" + sortColumnIndex);
        String sortDirection = param.sSortDirection;
        System.out.println("sortDirection" + sortDirection);

        JSONArray data = new JSONArray();
        iTotalRecords = AdministratorDemo.getAdminCount();
        List<Administrator> Administrators = new LinkedList<Administrator>();
        for (Administrator a : AdministratorDemo.getAdimistrators()) {
            if (a.getAdministrator_nm() != null
                    && a.getAdministrator_nm().toLowerCase()
                            .contains(param.sSearch.toLowerCase())

                    || a.getAdmin_Email_ID() != null
                    && a.getAdmin_Email_ID().toLowerCase()
                            .contains(param.sSearch.toLowerCase())
                    || a.getAdmin_Fax_Phone_Num_Tx() != null
                    && a.getAdmin_Fax_Phone_Num_Tx().toLowerCase()
                            .contains(param.sSearch.toLowerCase())) {
                Administrators.add(a);

            }

        }
        iTotalDisplayRecords = iTotalRecords;

    if (Administrators.size() < param.iDisplayStart + param.iDisplayLength)

                Administrators = Administrators.subList(param.iDisplayLength,
                        Administrators.size());
            else
                Administrators = Administrators.subList(param.iDisplayStart,
                        param.iDisplayStart + param.iDisplayLength);

        System.out.println("End of the operations");

        try {
            JSONObject jsonresponse = new JSONObject();
            jsonresponse.put("sEcho", sEcho);
            jsonresponse.put("iTotalRecords", iTotalRecords);
            jsonresponse.put("iTotalDisplayRecords", iTotalDisplayRecords);
            JSONArray row = new JSONArray();

            for(Iterator<Administrator> i = AdministratorDemo.getAdimistrators().iterator();i.hasNext();)
            {
            System.out.println(i.next());
            }
            jsonresponse.put("aaData", data);
            response.setContentType("application/json");
            response.getWriter().print(jsonresponse.toString());
        } catch (JSONException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
            response.setContentType("text/html");
            response.getWriter().print(e1.getMessage());
        }


        System.out.println("In execute method.");


        return null;

    }

    public String getXMLObject(HttpServletRequest request) {

        return new java.util.Date().toString()
                + " sent by vasu from Date Action";

    }
}
4

3 回答 3

2

您看到的结果 (com.demo.model.Administrator@91e143) 是 Administrator 对象的默认字符串表示形式,更具体地说,它是toString()从继承自的默认方法返回的内容Object

要打印有用的信息,public String toString()覆盖Administrator

于 2012-04-16T19:21:41.340 回答
2

你需要在你的类中覆盖Object#toString() 。Administrator

此方法可以返回您想要的对象的任何有意义的表示,例如:

@Override
public String toString() {
    return "id = " + id + "email = " + email;
}
于 2012-04-16T19:24:41.820 回答
0

您正在制作一个Administrators 列表,您想在其中制作一个Strings 列表。
或者(可能更好),如果您可以更改Administrator类,只需在其中实现toString方法,这将返回您想在屏幕上看到的字符串。

于 2012-04-16T19:23:25.510 回答