Spring3——JSP未运行自定义标记
我有一个自定义标记,它不是由我的JSP运行的。标记应该在我的数据库上运行一个查询并将这些值返回到我的JSP,但是当我知道数据库中有非零值时,我的页面中会得到零。我已经在调试模式下运行了应用程序,由于某种原因没有调用标记,但由于某种原因,我似乎得到了NullPointerException,即使标记存在。下面是我的JSP的相关部分。此部分仅在存在cookie时出现Spring3——JSP未运行自定义标记,jsp,nullpointerexception,spring-3,custom-tags,Jsp,Nullpointerexception,Spring 3,Custom Tags,我有一个自定义标记,它不是由我的JSP运行的。标记应该在我的数据库上运行一个查询并将这些值返回到我的JSP,但是当我知道数据库中有非零值时,我的页面中会得到零。我已经在调试模式下运行了应用程序,由于某种原因没有调用标记,但由于某种原因,我似乎得到了NullPointerException,即使标记存在。下面是我的JSP的相关部分。此部分仅在存在cookie时出现 <%@ taglib prefix="form" uri="http://www.springframework.org/tag
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<td><myTag1:poll1 /> <c:choose>
<c:when test="${foundCookiePoll1 == true}">
<table>
<tr>
<td><b><i>Poll #1 -- </i></b>Would you like to have a
30-year reunion in 2016?<br></td>
</tr>
<tr>
<td><b>Yes</b></td>
<td> – <c:out value='${poll1Yes}' /><br /> </td>
</tr>
<tr>
<td><b>No</b></td>
<td> – <c:out value='${poll1No}' /><br /> </td>
</tr>
</table>
</c:when>
投票#1——你想喝一杯吗
2016年重逢30周年?
对
&恩达什
不
&恩达什
这是我的标签
public void doTag(HttpServletRequest request)
throws JspException, IOException {
PageContext pageContext = (PageContext) getJspContext();
HttpSession session = request.getSession(true);
ServletContext servletContext = session.getServletContext();
WebApplicationContext wac = WebApplicationContextUtils
.getRequiredWebApplicationContext(servletContext);
Poll1DAO poll1DAO = (Poll1DAO) wac.getBean("poll1DAO");
pageContext.setAttribute("foundCookiePoll1", cookieFound());
if (cookieFound()) {
HashMap<String, Object> poll1Votes = poll1DAO.getVotes();
pageContext.setAttribute("poll1Yes", (int) poll1Votes.get("yes"));
pageContext.setAttribute("poll1No", (int) poll1Votes.get("no"));
}
}
private boolean cookieFound() {
PageContext pageContext = (PageContext) getJspContext();
HttpServletRequest request = (HttpServletRequest) pageContext
.getRequest();
Cookie[] cookies = request.getCookies();
if (cookies == null) {
return false;
}
for (int i = 0; i < cookies.length; i++) {
if (cookies[i].getName().equals("poll1")) {
return true;
}
}
return false;
}
public void doTag(HttpServletRequest)
抛出JSPEException、IOException{
PageContext PageContext=(PageContext)getJspContext();
HttpSession session=request.getSession(true);
ServletContext=session.getServletContext();
WebApplicationContext wac=WebApplicationContextils
.getRequiredWebApplicationContext(servletContext);
Poll1DAO Poll1DAO=(Poll1DAO)wac.getBean(“Poll1DAO”);
setAttribute(“foundCookiePoll1”,cookieFound());
if(cookieFound()){
HashMap poll1Voces=poll1DAO.getVoces();
setAttribute(“poll1Yes”,(int)poll1vows.get(“yes”);
setAttribute(“poll1No”,(int)poll1vows.get(“no”);
}
}
私有布尔cookieFound(){
PageContext PageContext=(PageContext)getJspContext();
HttpServletRequest=(HttpServletRequest)页面上下文
.getRequest();
Cookie[]cookies=request.getCookies();
如果(cookies==null){
返回false;
}
for(int i=0;i
这是我的tld.xml用于此标记
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.com/j2ee/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>2.0</jsp-version>
<short-name>Poll1Tag</short-name>
<uri>poll1</uri>
<tag>
<name>poll1</name>
<tag-class>com.tags.Poll1Tag</tag-class>
<body-content>empty</body-content>
</tag>
</taglib>
1
2
Poll1Tag
花粉1
花粉1
com.tags.Poll1Tag
空的
这可能不是最好的方法,但在页面的控制器方法中可以这样做
if (cookies != null) {
for (int i = 0; i < cookies.length; i++) {
if (cookies[i].getName().equals("poll1")) {
HashMap<String, Object> poll1Votes = poll1DAO.getVotes();
model.addAttribute("poll1Yes", (int) poll1Votes.get("yes"));
model.addAttribute("poll1No", (int) poll1Votes.get("no"));
}
}
}
$poll1No}
是打字错误吗?或者你真的错过了开头{{?这是一个打字错误。我刚刚在我的原始帖子中修复了它。我不敢相信我错过了。原始代码有开头{。在你的标签中放入一个println并再次验证(只是为了确保)。同时发布JSP标记库指令和TLD xml。我添加了一个println语句,但没有调用它。我添加了我的标记库指令和TLD xml。
<%= poll1Yes %>
int poll1Yes = (Integer) request.getAttribute("poll1Yes");
int poll1No = (Integer) request.getAttribute("poll1No");