将对象数组从servlet发送到JSP [英] Send array of objects from servlet to JSP

查看:69
本文介绍了将对象数组从servlet发送到JSP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过请求将自己的对象数组发送到JSP页面.

在servlet的这部分代码中,我将获取数据,将其放在对象数组中,并将其设置为request.

     if (request.getParameter("todo").equals("show_article_list")) {
         try {
             Article[] articles = this.getArticleList();

             request.setAttribute("articles", articles);
            RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("article/article_list.jsp");
            dispatcher.forward(request, response);
         } catch (Exception e) {
         }
     }

    public Article[] getArticleList() throws Exception {
    db data = new db();
    Connection con = data.OpenConnection();

    PreparedStatement statement = con.prepareStatement("SELECT * FROM `article`");
    ResultSet result = statement.executeQuery();


    int size = 0;  
    if (result != null)   
    {  
        if (result.last()) {
            size = result.getRow();
            result.beforeFirst();
        }
    }  

    Article[] articles = new Article[size];
    int i = 0;
    while(result.next()){
        articles[i] = new Article (
                result.getInt(1),
                result.getString(2),
                result.getString(3),
                result.getString(4));
        i++;        
    }

    return articles;
  }

这是我的课程:

public class Article {
public Integer getId(){return id;}

public String getTitle(){return title;}
public void setTitle(String title){this.title = title;}

public String getText(){return text;}
public void set(String text){this.text = text;}

public String getDescription(){return description;}
public void setDescription(String description){this.description= description;}

private Integer id;
private String title;
private String text;
private String description;

public Article(Integer Id, String Title, String Text, String Description)
{
    id = Id;
    title = Title;
    text = Text;
    description = Description;
}
}

在我的JSP页面上,我想使用request.getAttribute("articles");循环这样的对象数组,我该怎么做?

我必须使用<jsp:useBean/>或其他名称吗?我试图这样做:

Article[] articles = request.getAttribute("articles");

但是我有一个错误:Article cannot be resolved to a type

我做错了什么?

解决方案

您可能

  • 忘记了使用<%@ page import="com.foo.bar.Article" %>
  • 在JSP中导入Article类.
  • 忘记将getAttribute()的结果强制转换为文章数组:

Article[] articles = (Article[]) request.getAttribute("articles");

请注意,JSP中不应包含任何Java代码.您应该使用JSP EL,JSTL和其他自定义标签.阅读如何避免JSP文件中的Java代码?. /p>

I want to send array of my own objects to JSP page by request.

At this part of code in servlet I'll get my data, put it on array of objects, and set them to request.

     if (request.getParameter("todo").equals("show_article_list")) {
         try {
             Article[] articles = this.getArticleList();

             request.setAttribute("articles", articles);
            RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("article/article_list.jsp");
            dispatcher.forward(request, response);
         } catch (Exception e) {
         }
     }

    public Article[] getArticleList() throws Exception {
    db data = new db();
    Connection con = data.OpenConnection();

    PreparedStatement statement = con.prepareStatement("SELECT * FROM `article`");
    ResultSet result = statement.executeQuery();


    int size = 0;  
    if (result != null)   
    {  
        if (result.last()) {
            size = result.getRow();
            result.beforeFirst();
        }
    }  

    Article[] articles = new Article[size];
    int i = 0;
    while(result.next()){
        articles[i] = new Article (
                result.getInt(1),
                result.getString(2),
                result.getString(3),
                result.getString(4));
        i++;        
    }

    return articles;
  }

This is my class:

public class Article {
public Integer getId(){return id;}

public String getTitle(){return title;}
public void setTitle(String title){this.title = title;}

public String getText(){return text;}
public void set(String text){this.text = text;}

public String getDescription(){return description;}
public void setDescription(String description){this.description= description;}

private Integer id;
private String title;
private String text;
private String description;

public Article(Integer Id, String Title, String Text, String Description)
{
    id = Id;
    title = Title;
    text = Text;
    description = Description;
}
}

On my JSP page, I want to loop such array of objects using request.getAttribute("articles"); How I can do it?

I must use <jsp:useBean/> or something else? I tried to do this way:

Article[] articles = request.getAttribute("articles");

But I have an error: Article cannot be resolved to a type

What I did wrong?

解决方案

You probably

  • forgot to import the Article class in the JSP, using <%@ page import="com.foo.bar.Article" %>
  • forgot to cast the result of getAttribute() to an array of articles:

Article[] articles = (Article[]) request.getAttribute("articles");

Note that you shouldn't have any Java code in a JSP. You should use the JSP EL, the JSTL, and other custom tags. Read How to avoid Java code in JSP files?.

这篇关于将对象数组从servlet发送到JSP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆