创建与数据库对话的自定义标签

发布于 2024-10-09 13:55:32 字数 1676 浏览 0 评论 0原文

我想创建一个与数据库对话并从表中检索记录然后显示在 jsp 页面上的自定义标记。

我正在使用弹簧。 Eclipselink 作为 JPA 提供者。我写了一个自定义类。

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();
    }

}

当我尝试访问 Dao 对象时,代码抛出 NullPointer 异常。

有人能告诉我出了什么问题吗?

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

段念尘 2024-10-16 13:55:32

也许最干净的方法是使用 jstl/sql 中的标准现有标签并创建一个简单的标签文件而不是标签类:

    <%@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>

如果将代码放在 WEB-INF/tags 中,例如 dbtable.tag,则可以使用就像这样:

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

对数据库的引用必须在 web.xml 中进行,并且类路径中的某个位置必须有标准的 JSTL jar。

请注意,像这样构建 sql 需要不断注意,以免允许 sql 注入。

在您的设计中,以下之间存在很大的张力:

  • 静态类型、面向对象和分层 - 通过使用 Spring / daos / JPA
  • 模型 1 架构来显示,平面(而不是分层)设计,将 HTML 与逻辑混合 - 通过创建查询的愿望来显示按需、显示“记录”(而不是“查看对象图”)、在 HTML 模板中放置业务查询等。

这两种方法都可以(取决于您要解决的问题以及应用程序的范围) ),但它们确实混合得不好。现在看来,您似乎看到了这两种方法的缺点,却没有看到任何优点。

我会推荐你​​:

  • 放弃 Spring 和 daos,使用纯 jstl/sql;这将使您的应用程序成为数据库周围的一个简单的薄层;您可以自由地使用视图和存储过程来封装真正的逻辑;许多大型应用程序都是这样工作的,尤其是那些由具有强大数据库技能的人编写的应用程序。
  • 放弃“魔法表标签”的想法;制作一组 JavaBean,这些 JavaBean 不是一刀切的,而是针对特定任务量身定制的。让它们通过 Spring 注入、使用 daos、声明性事务划分等。这将使您的代码更长且不太通用,但(如果做得正确)在未来几年更容易维护。

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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文