如何将值从servlet传递给Dao类并执行操作并将List / Arraylist返回到JSP页面

时间:2017-05-09 14:21:51

标签: java jsp servlets javabeans dao

你好我是JSP开发的新手,我需要一些关于JSP MVC编程的帮助我想从Servlet传递一个值,在Dao类中我希望在List函数中接收它并执行操作并将数组返回到JSP页面使用..

的Servlet

String company = "ABCD";
ObsBean ComName = new ObsBean();
ComName.setCompanyName(company);
dao.getComNotify(ComName);

Bean类(ObsBean)

private String CompanyName;
public String getCompanyName() {
    return CompanyName;
}
public void setCompanyName(String CompanyName) {
    this.CompanyName = CompanyName;
}

** DAO班(ObsDao)**

public List getComNotify(ObsBean ComName) {
    List<ObsBean> comNotify = new ArrayList<ObsBean>();
    String cname = ComName.getCompanyName();//getting from bean class by getter
    try {
        String sql = "SELECT * from ObsNotify where notto='"+cname+"'";
        ps = conn.prepareStatement(sql);
        rs = ps.executeQuery();

        while (rs.next()) {
            ObsBean userNotify = new ObsBean();

            userNotify.setNotifyName(rs.getString("notname"));
            userNotify.setNotifyBy(rs.getString("notby"));
            userNotify.setNotifyTo(rs.getString("notto"));
            userNotify.setNotifyDate(rs.getString("notdate"));

            comNotify.add(userNotify);
        }
    } 
    catch (Exception e) {
        out.print("Error for User Notification - : "+e);
    }
    return comNotify; 
}

在JSP页面中

<%
ObsDao dao = new ObsDao();
List<ObsBean> ComNotify = dao.getComNotify();
for (ObsBean UserNotifi : ComNotify) {
.........
.........
}
%>

这是我的完整代码,但它显示错误,为什么?

错误:

HTTP Status 500 – Internal Server Error

Type Exception Report

Message javax.servlet.ServletException: java.lang.NoSuchMethodError: com.ApexCorner.ModelDao.ObsDao.getComNotify()Ljava/util/List;

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

org.apache.jasper.JasperException: javax.servlet.ServletException: java.lang.NoSuchMethodError: com.ApexCorner.ModelDao.ObsDao.getComNotify()Ljava/util/List;
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:565)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:466)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

2 个答案:

答案 0 :(得分:0)

在你的Dao课程中,应该有CompanyName的getter方法。所以请尝试以下代码:

STRING COM = Comname;,&lt; - 将此行更改为String COM=Comname.getCompanyName();

答案 1 :(得分:0)

根据你的代码我假设&#34; notto&#34;在你的表中是一个公司名称,所以如果你的sql语句是正确的,我会按如下方式重写你的代码:

public List getComNotify(ObsBean Comname) {
    List<ObsBean> comNotify = new ArrayList<ObsBean>();
    String COM = Comname.getcompanyname();
    try {
        String sql = "SELECT * from ObsNotify where notto='"+COM+"'";
        ps = conn.prepareStatement(sql);
        rs = ps.executeQuery();
        while (rs.next()) {
            ObsBean userNotify = new ObsBean();
            int totalRows = rs.getRow();

            userNotify.setNotifyName(rs.getString("notname"));
            userNotify.setNotifyBy(rs.getString("notby"));

            comNotify.add(userNotify);
        }
    } 
    catch (Exception e) {
        out.print("Error for User Notification - : "+e);
    }
    return comNotify; 
}

Comname是ObsBean类型的对象,您应该将公司名称作为字符串检索,然后才能将其应用于sql语句,还要注意java是区分大小写的。

JSP中的

执行类似

的操作
<%
ObsDao dao = new ObsDao();
ObsDao otherdao = new ObsDao();
otherdao.setCompanyName("My company")
List<ObsBean> ComNotify = dao.getComNotify(otherdao);
for (ObsBean UserNotifi : ComNotify) {
.........
.........
}
%>