jsp:include包含servlet

和Struts2的<s:action/>这个标签一样,<jsp:include>也是在页面中包含另外的页面或servlet,一般在页面一加载就显示后台数据时这两个标签比较实用。


首先是一个servlet:

public class LoadAllNewsController extends HttpServlet {

    private static final long serialVersionUID = 1L;

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
            
            INewsService newsService = new NewsServiceImpl();
        
        try {
            List<News> newsList = newsService.getAll();//得到数据
            
            request.setAttribute("newsList", newsList);
        } catch (ServiceException e) {
            e.printStackTrace();
            
        }
        finally{
        
            newsService = null;
            //这里不再是跳转页面,而是include
            request.getRequestDispatcher("/WEB-INF/admin/loadAllNews.jsp").include(request, response);
        }
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        doGet(request, response);

    }

}


这个servlet要返回的页面:

<div class="row clearfix">
            <div class="col-md-12 column">
                <!-- 新闻列表 -->
                <c:choose>
                    <c:when test="${newsList != null && newsList.size() != 0 }">
                        <table class="table">
                            <thead>
                                <tr>
                                    <th>新闻标题</th>
                                    <th>发布时间</th>
                                    <th>城市</th>
                                    <th>分类</th>
                                    <th>编辑</th>
                                    <th>是否审核</th>
                                </tr>
                            </thead>
                            <tbody>
                                <c:forEach items="${newsList }" var="news">
                                    <c:choose>
                                        <c:when test="${news.examined == 0 }">
                                            <tr class="error">
                                        </c:when>
                                        <c:when test="${news.examined == 1 }">
                                            <tr class="success">
                                        </c:when>
                                    </c:choose>
                                        <td><a
                                            href="root/newsDescription?id=<c:out value="${news.id}"/>"><c:out
                                                    value="${news.title }"></c:out></a></td>
                                        <td><fmt:formatDate value="${news.publishTime }"
                                                pattern="yyyy年MM月dd日 HH:mm:SS" /></td>
                                        <td><c:out value="${news.city }"></c:out></td>
                                        <td><c:choose>
                                                <c:when test="${news.category == 1 }">社会</c:when>
                                                <c:when test="${news.category == 2 }">国际</c:when>
                                                <c:when test="${news.category == 3 }">军事</c:when>
                                                <c:when test="${news.category == 4 }">娱乐</c:when>
                                                <c:when test="${news.category == 5 }">时尚</c:when>
                                                <c:when test="${news.category == 6 }">科技</c:when>
                                                <c:when test="${news.category == 7 }">财经</c:when>
                                                <c:when test="${news.category == 8 }">体育</c:when>
                                                <c:when test="${news.category == 9 }">旅游</c:when>
                                                <c:when test="${news.category == 10 }">美食</c:when>
                                                <c:when test="${news.category == 11 }">汽车</c:when>
                                                <c:when test="${news.category == 12 }">教育</c:when>
                                                <c:when test="${news.category == 13 }">其他</c:when>
                                            </c:choose></td>
                                        <td><a
                                            href="root/listNewsByEditorId?id=<c:out value="${news.editorId }"/>"><c:out
                                                    value="${news.editor }"></c:out></a></td>
                                        <td><c:choose>
                                                <c:when test="${news.examined == 0 }">未审核</c:when>
                                                <c:otherwise>已审核</c:otherwise>
                                            </c:choose></td>
                                    </tr>
                                </c:forEach>
                            </tbody>
                        </table>
                    </c:when>
                    <c:otherwise>
                        <h3>没有新闻</h3>
                    </c:otherwise>
                </c:choose>
            </div>
        </div>


包含该servlet的页面index.jsp:

<div class="row clearfix">
            <div class="col-md-12 column">
                <jsp:include page="${basePath }/root/loadAllNews"></jsp:include>
                
            </div>
        </div>


这样在刷新index.jsp时加载servlet的数据。有时会报这个错:

org.apache.jasper.JasperException: /WEB-INF/admin/rootIndex.jsp (line: 83, column: 26) attribute for %>" is not properly terminated
    at org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:42)
    at org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:443)
   。。。

我的原因是

<jsp:include page="${basePath }/root/loadAllNews"></jsp:include>

的servlet路径不正确,不要用<%=basePath>来加载路径,标签的page属性值可以是相对路径URL或者<%=表达式 %>,但同时只能存在其中一种

<jsp:include page="/frame/include.jsp"></jsp:include> 表示绝对路径

<jsp:include page="frame/include.jsp"></jsp:include> 表示相对路径

<jsp:include page="<%=uri %>"></jsp:include> 两个引号与<%=uri %>之间不能有空格


转载于:https://my.oschina.net/ChiLin/blog/644426

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值