无法在JSP Spring MVC中呈现对象

时间:2014-09-07 20:25:18

标签: jsp spring-mvc

任何人都可以告诉我如何在JSP中呈现对象。 我已经尝试了很多组合,但它没有打印。 我的下面代码是否正确?

"导致"是一个通过hibernate从类中获取值的对象。

这是我的代码

@RequestMapping(value = "/loginvalidate")
public ModelAndView validatelogin (LoginForm request) throws Exception {

    String userName = request.getUserName();
    ModelAndView mav = new ModelAndView("/loginvalidate"); 
    sessionUser.setUserName(userName);
    System.out.println("username from the controller class " + sessionUser.getUserName());
    List result = loginservice.ValidateLogin(request);
    mav.setViewName("loginvalidate");
    mav.addObject("ToDoList", result);
    return mav;

}

这是我的jsp代码

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <%@ page import="java.util.List"%>
<c:if test="${not empty ToDoList}">
<table>
    <c:forEach var="item" items="${ToDoList}">
        <tr>
            <td>${item.id}</td>
            <td>${item.status}</td>
            </tr>
    </c:forEach>
 </table>
</c:if>

这是我的使用scriptlet的样本JSP,当我尝试在系统中打印List时,我得到&#34; null&#34;。 但是我在控制器类中打印了相同的内容并返回了一些值。

我已尝试过两次会话并请求获取属性。

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Iterator" %>
<%@ page import="com.BillingSolution.hibernatebean.BillingSolutionTrx" %>

<html>
<head>
    <title>Home Page</title>

    <link href="/resources/css/style.css" rel="stylesheet" type="text/css"/>
</head>    
<body>


    <% List list = (List) session.getAttribute("ToDoList");
    System.out.println("printing the attributed from List" +list);
        BillingSolutionTrx trx = null;
        for (Iterator iterator = list.iterator(); iterator.hasNext();) {
            trx = (BillingSolutionTrx) iterator.next();
            Long id = (Long) trx.getId();
            String status = (String) trx.getStatus();
            out.println("ID from the to do list JSP " + id);
            out.println("STATUS from the to do list JSP " + status);
        }
            %>



  </body>
 </html>

堆栈跟踪

ava.lang.NullPointerException
at jsp_servlet._web_45_inf._jsp.__loginvalidate._jspService(loginvalidate.jsp:24)
at weblogic.servlet.jsp.JspBase.service(JspBase.java:34)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:242)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:216)
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:132)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:352)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:235)
at weblogic.servlet.internal.RequestDispatcherImpl.invokeServlet(RequestDispatcherImpl.java:567)
at weblogic.servlet.internal.RequestDispatcherImpl.forward(RequestDispatcherImpl.java:263)
at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:209)
at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:266)
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1225)
at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1012)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:751)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:844)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:242)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:216)
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:132)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:352)
at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:25)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:74)
at weblogic.servlet.internal.RequestEventsFilter.doFilter(RequestEventsFilter.java:27)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:74)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3288)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3254)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)
at weblogic.servlet.provider.WlsSubjectHandle.run(WlsSubjectHandle.java:57)
at weblogic.servlet.internal.WebAppServletContext.doSecuredExecute(WebAppServletContext.java:2163)
at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2089)
at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2074)
at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1512)
at weblogic.servlet.provider.ContainerSupportProviderImpl$WlsRequestExecutor.run(ContainerSupportProviderImpl.java:254)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:256)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:221)

2 个答案:

答案 0 :(得分:0)

我认为问题可能来自您的<c:if ...>测试。我会写的:

<c:if test="${ToDoList.empty == false}">

答案 1 :(得分:0)

您可以查看控制器功能的备用代码,如下所示。我使用ModelMap类来发送您的

@RequestMapping(value = "/loginvalidate")
public String validatelogin (LoginForm request,ModelMap map) throws Exception   
{
    String userName = request.getUserName();
    ModelAndView mav = new ModelAndView("/loginvalidate"); 
    sessionUser.setUserName(userName);
    System.out.println("username from the controller class " + sessionUser.getUserName());
    List result = loginservice.ValidateLogin(request);
    map.addAttribute("ToDoList", result);
    return "loginvalidate"; //Your JSP Page Path
}  

在JSP中使用EL来获取列表的值。