处理JSP页面时出现异常[/views/index.jsp]

时间:2017-12-27 06:10:30

标签: java spring jsp

我在spring mvc中有错误。

服务器遇到意外情况,无法完成请求。

org.apache.jasper.JasperException: An exception occurred processing JSP page [/views/index.jsp] at line [21]
18: <div class="row section-row">
19:     <div class="col-xs-12 section-col">
20: 
21:         <h:dataTable value="#{homeController.allNews}" var="news">
22: 
23:             <h:column>
24:                 <f:facet name="header">Title</f:facet>

Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:584)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:481)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:386)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:330)
javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
org.apache.jasper.runtime.JspRuntimeLibrary.include(JspRuntimeLibrary.java:894)
org.apache.jsp.views.home_jsp._jspService(home_jsp.java:121)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:443)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:386)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:330)
javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:91)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

我的家庭控制器代码:

@ManagedBean
@RequestScoped
public class HomeController {


news_simple news = new news_simple();

ApplicationContext context = new AnnotationConfigApplicationContext(ProjectConfig.class);

NewsService service = context.getBean(NewsService.class);


public List<news_simple> getAllNews() {
    List<news_simple> x = service.GetAll();
    return x;
}

public news_simple getNews() {
    return news;
}
public void setBook(news_simple news) {
    this.news = news;
}
}

在我的home.jsp代码中:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html" %>
<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core" %>
<div class="row section-row">
<div class="col-xs-12 section-col">

    <h:dataTable value="#{homeController.allNews}" var="news">

        <h:column>
            <f:facet name="header">Title</f:facet>
            <h:outputText value="#{news.title}"></h:outputText>
        </h:column>
        <h:column>
            <f:facet name="header">Content</f:facet>
            <h:outputText value="#{news.content}"></h:outputText>
        </h:column>
        <h:column>
            <f:facet name="header">Date</f:facet>
            <h:outputText value="#{news.date}"></h:outputText>
        </h:column>

    </h:dataTable>

</div>

在Home Controller中我有一个名为news的对象,这个对象有setter和getter。

另外,当我用我的测试控制器调用它时,getAllNews工作。并且ApplicationContext和NewsService已成功返回数据。

1 个答案:

答案 0 :(得分:0)

我不知道为什么jsf不起作用。但是我将代码更改为jsp,并使用了jsp文件而不是jsf。

家庭控制器:

@Controller
@Scope(value = "request")
public class HomeController {

 @RequestMapping(value = "/", method = RequestMethod.GET)
 public String Home(HttpServletRequest request, HttpServletResponse response) throws 
 Exception {
    List<NewsDoc> list = getTopNewsList();
    request.setAttribute("list", list);
    return "Home";
}

并位于Home.jsp顶部:

<% List<NewsDoc> newsList = (List<NewsDoc>) request.getAttribute("list");

以及我想在哪里查看结果:

<% for (NewsDoc news : newsList) { %>
    <h2><%=news.getTitle()%></h2>
    <p>
        <%=news.getAbout()%>
    </p>
<% } %>