0

无法从 JSP 页面中的 Bean 调用 getNames()...成功设置名称,但是当我调用 getNames 时它返回 Null

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {
  response.setContentType("text/html;charset=UTF-8");
  PrintWriter out = response.getWriter();

  try {
    List<String> name=new ArrayList<String>();
    name.add("Pushkar");
    name.add("Raj");
    ArrayBean bean=new ArrayBean();
    bean.setNames(name.toArray(new String[name.size()]));       

    // request.getRequestDispatcher("DDL.jsp").forward(request, response);
    response.sendRedirect("DDL.jsp");

  } finally {            
    out.close();
  }

public class ArrayBean {
  public String[] names;

  public String[] getNames() {
    return names;
  }

  public void setNames(String[] names) {
    this.names = names;
  }
}
4

2 回答 2

0

将您的尝试捕获更改为

try {
name=new ArrayList<String>();
name.add("Pushkar");
name.add("Raj");
ArrayBean bean=new ArrayBean();
bean.names=new String[name.size()];
        bean.names=name.toArray(bean.names);      

// request.getRequestDispatcher("DDL.jsp").forward(request, response);
response.sendRedirect("DDL.jsp");} finally {            
out.close();  }

希望它会奏效。

于 2013-02-24T13:54:14.673 回答
0

这东西有效,我在 Bean 中添加了一个 LoadData() 方法来设置值,然后我从 JSP 调用 getNames(),它工作正常,从 Servlet 中删除了所有代码,现在甚至不需要 Servlet

public class ArrayBean {
public List<String> names;

public List<String> getNames() {
    if(names==null){loadNames();
    }return names;
}   


private void loadNames()
{
     names=new ArrayList<String>();
        names.add("Pushkar");
        names.add("Raj");        

}

}

于 2013-02-25T06:48:50.363 回答