Thanos
Thanos

Reputation: 633

Create Custom Tag that talks to the database

I want to create a custom tag that talks to the database and retreives records from a table and then displays on a jsp page.

I am using spring. and Eclipselink as JPA provider. I wrote a custom class.

package com.persistent.testjpa.taghandlers;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import org.springframework.beans.factory.annotation.Autowired;

import com.persistent.testjpa.dao.MyUserDao;
import com.persistent.testjpa.domain.MyAuthorities;


public class MyListTagHandler extends SimpleTagSupport {

    private String tableName;
    private MyUserDao dao;

    public String getTableName() {
        return tableName;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    public void doTag() throws JspException, IOException {
        System.out.println("Indise Do Tag Method");
        JspWriter out = getJspContext().getOut();
        if (tableName != null) {
            System.out.println("Table Name : "+getTableName());
            out.println("<html><body>");
            out.println("Today's Date is "+ new Date());

            out.println("</body></html>");
        }  else {
            out.println("<html><body>");
            out.println("Please Enter Table Name");
            out.println("</body></html>");
        }
    }

    @Autowired
    public void setDao(MyUserDao dao) {
        this.dao = dao;
    }

    public List<MyAuthorities> getList(){
        return dao.list();
    }

}

When I try to access the Dao Object The code throws a NullPointer Exception.

Can someone tell me what's wrong?

Thanks

Upvotes: 1

Views: 3453

Answers (1)

fdreger
fdreger

Reputation: 12495

Probably the cleanest way would be to use standard, existing tags from jstl/sql and make a simple tagfile instead of a tag class:

    <%@taglib  prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
    <%@taglib  prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@attribute name="table" required="true"%>    
    <sql:query var="temporary">
        select * from ${table}
    </sql:query>
    <table border="1">
        <tr>
            <c:forEach items="${temporary.columnNames}" var="temporary_value">
                <th>${temporary_value}</th>
            </c:forEach>
        </tr>
        <c:forEach items="${temporary.rowsByIndex}" var="temporary_row">

            <tr>
                <c:forEach items="${temporary_row}" var="temporary_value">
                    <td>${temporary_value}</td>
                </c:forEach>
            </tr>
        </c:forEach>
    </table>

If you place the code in your WEB-INF/tags as, say, dbtable.tag, you can use it like so:

<%@taglib  prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<sql:setDataSource dataSource="jdbc/mydb" scope="request" />
<tags:dbtable table="customers"/>

The reference to the database must be made in web.xml, and you must have standard JSTL jars somewhere in your classpath.

Note that building sql like this requires constant attention as not to allow sql injection.

In your design there is a great tension between:

  • static typing, object orientation and layering - displayed by using Spring / daos / JPA
  • model 1 architecture, flat (as opposed to layered) design, mixing HTML with logic - displayed by wish to create queries on demand, displaying "records" (as opposed to "making a view of an object graph"), placing business queries in the HTML template etc.

Both approaches are OK (depends on problems you are trying to solve and the scope of your application), but they really don't mix well. Right now it seems like you are getting drawbacks of both approaches and the benefits of none.

I would recommend you either:

  • drop Spring and daos and go with pure jstl/sql; this will make your application a simple, thin layer around your database; you are free to use views and stored procedures to encapsulate the real logic; many large applications work exactly like this, especially those written by people with strong database skills.
  • drop the idea of "magic table tag"; make a set of javabeans that are not of a one-size-fits-all variety, but are tailored to specific tasks. Have them injected by Spring, use daos, declarative transaction demarcation etc. This will make your code much longer and less universal, but (if done right) easier to maintain over years to come.

Upvotes: 2

Related Questions