请老师检查一下,谢谢!

来源:2-13 自由编程

MAYxDAY

2020-03-06 20:55:38

package com.imooc.jstl;


public class Notice {

private String id;

private String name;

public Notice(String id, String name) {

this.id = id;

this.name = name;

}

public String getId() {

return id;

}

public void setId(String id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

@Override

public String toString() {

return  id + "------" + name ;

}


}

**************************************

package com.imooc.jstl;


import java.io.IOException;

import java.util.ArrayList;

import java.util.List;


import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;


/**

 * Servlet implementation class NoticeList

 */

@WebServlet("/nl")

public class NoticeList extends HttpServlet {

private static final long serialVersionUID = 1L;

       

    /**

     * @see HttpServlet#HttpServlet()

     */

    public NoticeList() {

        super();

        // TODO Auto-generated constructor stub

    }


/**

* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

*/

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

Notice notice1=new Notice("001","欢迎来到慕课网!");

Notice notice2=new Notice("002","欢迎学习java web 课程!");

Notice notice3=new Notice("003","一定要多写代码!");

List<Notice> notices=new ArrayList();

notice.add(notice1);

notice.add(notice2);

notice.add(notice3);

request.setAttribute("notices", notices);

request.getRequestDispatcher("/notice.isp").forward(request, response);

}


}

*******************************************

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

    <%@ taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Insert title here</title>

</head>

<body>

   <h1>公告展示</h1>

     <c:forEach items="${requestSpoce.notices}" var="n">

                   <h2>${n}</h2>

     

     </c:forEach>

</body>

</html>


写回答

1回答

好帮手慕酷酷

2020-03-07

同学你好,代码完成的不错,但还有一点小问题:

1、应该是给集合notices添加元素,应将notice 修改为 notices

具体如下:

http://img.mukewang.com/climg/5e630ebc098317b307580412.jpg

2、requestScope单词拼写错了,具体如下:

http://img.mukewang.com/climg/5e630f23092670ad07190215.jpg

如果我的回答解决了你的疑惑,请采纳!祝学习愉快!

0

0 学习 · 9666 问题

查看课程