为什么我这个不能显示?
来源:2-12 自由编程
Wuq1an
2020-03-05 10:53:43
<%@ 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>
<c:forEach items="${renquestScope.month}" var="c" varStatus="idx">
<h2>${idx.index}——${c}</h2>
</c:forEach>
</body>
</html>
package com.wuqian.jstl_servlet;
import java.util.List;
import java.io.IOException;
import java.util.ArrayList;
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 jstl
*/
@WebServlet("/jstl")
public class jstl extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public jstl() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List list = new ArrayList();
list.add(new month("May"));
list.add(new month("March"));
list.add(new month("January"));
list.add(new month("February"));
list.add(new month("April"));
request.setAttribute("month", list);
request.getRequestDispatcher("/jstl.jsp").forward(request, response);
}
}
package com.wuqian.jstl_servlet;
public class month {
private String month;
public month(String month) {
this.month=month;
}
public String getMonth() {
return month;
}
public void setMonth(String month) {
this.month = month;
}
}
2回答
同学你好,1. renquestScope书写错误,建议修改为requestScope。
2. 建议同学在循环中调用getMonth()方法,展示月份。修改后代码如下:
<c:forEach items="${requestScope.month }" var="c" varStatus="idx"> <h2>${idx.index }——${c.getMonth()}</h2> </c:forEach>
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
好帮手慕小班
2020-03-06
同学你好,非常抱歉,这里老师的说法有误,在循环中需要使用对象.属性名这样的方式来展示月份,例如:
如上所示,就可以正确展示月份对象中的月份属性。
在课程中,老师也是这样来使用的,例如:
另外,建议同学注意代码规范,类名要单词首字母大写。
如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~
相似问题