Reputation: 633
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
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:
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:
Upvotes: 2