Laughing girl2021 2021-08-16 21:44 采纳率: 100%
浏览 26
已结题

java练习el表达式遍历网页,请求转发时报错,请万能的网友解答!


package Servlet;

import Phone.Phone;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "phoneServlet", urlPatterns = "/phoneList")
public class phoneServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //通过servlet从数据库中获取数据,我们这里暂时通过手动创建数据
        Phone phone1=new Phone();
        phone1.setId(1);
        phone1.setName("栗子");
        phone1.setImage("https://img10.360buyimg.com/n7/jfs/t1/187222/16/730/50932/608baf8dE89f4dbe4/6d3f90b580c05028.jpg");
        phone1.setPrice("150");

        Phone phone2=new Phone();
        phone2.setId(2);
        phone2.setName("坚果");
        phone2.setImage("https://img12.360buyimg.com/n7/jfs/t1/182203/25/19103/455925/61163a4bE144be27e/95211033b99ef989.jpg");
        phone2.setPrice("50");

        Phone phone3=new Phone();
        phone3.setId(3);
        phone3.setName("干果");
        phone3.setImage("https://img12.360buyimg.com/n7/jfs/t1/182848/16/19353/211434/61176cd8E2be06b14/34580a7c6e5ee4e1.jpg");
        phone3.setPrice("60");

        Phone phone4=new Phone();
        phone4.setId(4);
        phone4.setName("百草果");
        phone4.setImage("https://img13.360buyimg.com/n7/jfs/t1/190112/19/18621/171530/611a1f0eE84861c97/2ec1a2398fc8ab1c.jpg");
        phone4.setPrice("99");

        Phone phone5=new Phone();
        phone5.setId(5);
        phone5.setName("经典坚果干");
        phone5.setImage("https://img13.360buyimg.com/n7/jfs/t1/184308/7/19260/247883/611776caE6342ecb8/0e8a226cbc5b22c8.jpg");
        phone5.setPrice("199");

        //将所有数据对象保存到新建的list集合中
        List<Phone> list=new ArrayList<>();
        list.add(phone1);
        list.add(phone2);
        list.add(phone3);
        list.add(phone4);
        list.add(phone5);

        //保存集合list到request请求域中,使phone_list.jsp能调用list
        request.setAttribute("list",list);
        //请求转发,页面跳转至phone_list.jsp
        request.getRequestDispatcher("/phone_list.jsp").forward(request,response);

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: pan
  Date: 2021/8/16
  Time: 20:31
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<c:forEach items="${list}" var="phone">
    <div>
        <a><img src="${phone.image}" width="170" height="170" style="" alt="图片"></a>
        <p>
            <a href="" style="color: green">${phone.name}</a>
        </p>
        <p>
            <font color="aqua">商城价格:${phone.price}</font>
        </p>
    </div>
</c:forEach>
</body>
</html>

img

  • 写回答

1条回答 默认 最新

  • 404警告 2021-08-17 09:21
    关注

    404页面未找到 查看是否存在phone_list.jsp文件 查看phone_list,jsp是否与当前文件目录平级

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 8月26日
  • 已采纳回答 8月18日
  • 创建了问题 8月16日

悬赏问题

  • ¥15 请上面代码做什么处理或什么混淆
  • ¥15 英雄联盟自定义房间置顶
  • ¥15 W5500网线插上无反应
  • ¥15 如何用字典的Key,显示在WPF的xaml中
  • ¥15 weautomate读取Excel表格信息然后填写到网页一直报错,如何解决?
  • ¥15 C#如何在Webview2中获取网页验证码
  • ¥15 esp32烧录失败,具体情况在图片上
  • ¥15 selenium安装报错
  • ¥15 在node.js中无法安装yarn
  • ¥15 python程序修改