2-13 自由编程
来源:2-13 自由编程
慕的地2082093
2020-01-18 21:04:15
package com.imooc.jstl;
public class Notice {
private String id;
private String name;
public Notice() {
super();
// TODO Auto-generated constructor stub
}
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 "Notice [id=" + id + ", name=" + 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 noticeServlet
*/
@WebServlet("/notice")
public class noticeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public noticeServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
List <Notice> list = new ArrayList<Notice>();
Notice notice1 = new Notice();
Notice notice2 = new Notice();
Notice notice3 = new Notice();
notice1.setId("001");
notice1.setName("欢迎来到慕课网!");
notice2.setId("002");
notice2.setName("欢迎学习Java Web课程!");
notice3.setId("003");
notice3.setName("一定要多写代码,多练习哟!");
list.add(notice3);
list.add(notice2);
list.add(notice1);
request.setAttribute("notices", list);
request.getRequestDispatcher("/notice.jsp").forward(request, response);
//response.getWriter().append("Served at: ").append(request.getContextPath());
}
}
<%@ 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="${requestScope.notices}" var="c">
<h1>${c.id }——${c.name}</h1>
</c:forEach>
</body>
</html>
1回答
好帮手慕酷酷
2020-01-19
同学你好,代码完成的不错,但有一个小建议,注意一下类名的命名规则,当类名由一个单词组成时,该单词首字母大写;如果类名由多个单词组成,则每个单词的首字母均大写。如:noticeServlet应改为:NoticeServlet。继续努力!
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
相似问题