创建与数据库对话的自定义标签
我想创建一个与数据库对话并从表中检索记录然后显示在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许最干净的方法是使用 jstl/sql 中的标准现有标签并创建一个简单的标签文件而不是标签类:
如果将代码放在 WEB-INF/tags 中,例如 dbtable.tag,则可以使用就像这样:
对数据库的引用必须在 web.xml 中进行,并且类路径中的某个位置必须有标准的 JSTL jar。
请注意,像这样构建 sql 需要不断注意,以免允许 sql 注入。
在您的设计中,以下之间存在很大的张力:
这两种方法都可以(取决于您要解决的问题以及应用程序的范围) ),但它们确实混合得不好。现在看来,您似乎看到了这两种方法的缺点,却没有看到任何优点。
我会推荐你:
Probably the cleanest way would be to use standard, existing tags from jstl/sql and make a simple tagfile instead of a tag class:
If you place the code in your WEB-INF/tags as, say, dbtable.tag, you can use it like so:
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: