Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3620014
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T22:57:04+00:00 2026-05-18T22:57:04+00:00

I want to create a custom tag that talks to the database and retreives

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T22:57:05+00:00Added an answer on May 18, 2026 at 10:57 pm

    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.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want create a custom json data from the mssql 2008 results so that
I want to create a custom set that will automatically convert objects into a
I want to create a custom property on one of my entities mapped from
I want to create a custom style for an activity that will look like
I'm trying to create a custom AccordionItem that can take the tag property value
I have to create a custom tag that uses POI to read in an
I want to create a custom tag library which should extend the existing Spring
i want to know about this custom tag that google use for social plug
I want to create a custom container that supports iterators. It looks like this:
I want to create a custom tag library but in the handler class I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.