Spring select查询未在页面

时间:2016-02-15 08:09:05

标签: java spring spring-mvc jstl

当我运行项目时,它只显示表结构。不是数据。 一切都很好。只是它显示表结构和电子邮件和密码标题。但没有数据显示。

controller_fetch.java

public class controller_fetch extends model.modelclass {
SqlRowSet srs=null;
ArrayList arraylist=null;
    @RequestMapping(value = "/list")
    public ArrayList list(HttpServletRequest request, HttpServletResponse response) throws ClassNotFoundException {
        try {
            arraylist = new ArrayList();
            modelclass obj = new modelclass();
            SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
            dataSource = new dbconnection.connection().getConnection(dataSource);
            JdbcTemplate jt = new JdbcTemplate(dataSource);
            String sql = "SELECT * FROM user_info";
             srs = jt.queryForRowSet(sql);

            while (srs.next()) {
                obj.setEmailid(srs.getString("email_id"));
                obj.setPwd(srs.getString("pwd"));
            arraylist.add(obj);
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return arraylist;
    }

}

list.jsp

$ {arraylist}始终为空。没有从控制器获得返回值。

         <table>
            <tr><td><b>Email</b></td><td><b>Password</b></td></tr>
            <c:forEach  items="${arraylist}" var="value">
            <tr>
                <td>${value.email_id}</td>
                <td>${value.pwd}</td>               
            </tr>
        </c:forEach>            
        </table>

1 个答案:

答案 0 :(得分:0)

如果您使用Spring MVC,我认为这种方法会更容易。我还是新手,但我仍然希望这适合你。

在您的控制器

 @RequestMapping(value = "/list", method = RequestMethod.GET)
public ModelAndView list(HttpServletRequest request, HttpServletResponse response) throws ClassNotFoundException {
        ModelAndView model = new ModelAndView();        
        try {

        arraylist = new ArrayList();
        modelclass obj = new modelclass();
        SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
        dataSource = new dbconnection.connection().getConnection(dataSource);
        JdbcTemplate jt = new JdbcTemplate(dataSource);
        String sql = "SELECT * FROM user_info";
         srs = jt.queryForRowSet(sql);

        while (srs.next()) {
            obj.setEmailid(srs.getString("email_id"));
            obj.setPwd(srs.getString("pwd"));
        arraylist.add(obj);
        }
      model.addObject("arraylist", arraylist);

    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return model;
}

}

在jsp中

 <table>
        <tr><td><b>Email</b></td><td><b>Password</b></td></tr>
        <c:forEach  items="${arraylist}" var="value">
        <tr>
            <td><c:out value="${value.email_id}" /></td>
            <td><c:out value="${value.pwd}" /></td>               
        </tr>
    </c:forEach>            
    </table>

希望这有帮助

编辑:将ModelAndView置于try

之上