博客
关于我
SpringMVC之入门程序
阅读量:420 次
发布时间:2019-03-06

本文共 2960 字,大约阅读时间需要 9 分钟。

SpringMVC入门程序——从浏览器展示商品数据的实现

1. 创建POJO类(商品实体)

首先,我们需要创建一个用于存储和处理商品数据的POJO类。这个类会作为后续开发的核心数据模型。具体实现如下:

package deep.springmvc.pojo;import org.springframework.stereotype.Component;@Component("items")public class Items {    private Long id;    private String name;    private Double price;    private Date createTime;    private String description;    public Items(Long id, String name, Double price, Date createTime, String description) {        this.id = id;        this.name = name;        this.price = price;        this.createTime = createTime;        this.description = description;    }    // getter和setter方法省略...}

2. 导入必要的包

在项目中,需要导入以下包:

import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;

3. 配置web.xml文件

将Spring MVC的前端控制器映射到具体的请求URL。web.xml配置如下:

springmvc-01
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
springmvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springmvc.xml
springmvc
*.action

4. 配置Spring MVC核心配置文件

在springmvc.xml中进行Spring的基本配置:

5. 创建Controller类

编写一个用于展示商品列表的控制器类:

package deep.springmvc.controller;import java.util.ArrayList;import java.util.Date;import java.util.List;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;@Controllerpublic class ItemController {        @RequestMapping(value = "/item/itemlist.action")    public ModelAndView itemList() {        List
list = new ArrayList<>(); Items item1 = new Items(1L, "1华为荣耀8", 2399.0, new Date(), "质量好!"); Items item2 = new Items(2L, "2华为荣耀8", 2399.0, new Date(), "质量好!"); Items item3 = new Items(3L, "3华为荣耀8", 2399.0, new Date(), "质量好!"); Items item4 = new Items(4L, "4华为荣耀8", 2399.0, new Date(), "质量好!"); Items item5 = new Items(5L, "5华为荣耀8", 2399.0, new Date(), "质量好!"); Items item6 = new Items(6L, "6华为荣耀8", 2399.0, new Date(), "质量好!"); list.add(item1); list.add(item2); list.add(item3); list.add(item4); list.add(item5); list.add(item6); ModelAndView mav = new ModelAndView(); mav.addObject("itemList", list); mav.setViewName("/WEB-INF/jsp/itemList.jsp"); return mav; }}

6. 导入JSP页面并获取数据

在JSP页面中,通过request.getParameterValues("itemList")获取前台传递的数据,并进行展示。

转载地址:http://bkuuz.baihongyu.com/

你可能感兴趣的文章
python-queue队列通信
查看>>
python-ros No module named PyKDL
查看>>
python-social-auth 是否需要会话
查看>>
Python再获年度编程语言,微软或成最大赢家
查看>>
python-week5
查看>>
Python写表情包搜索工具(非常详细)零基础入门到精通,收藏这一篇就够了
查看>>
Python-【1】配置
查看>>
Python-从子文件夹执行绝对导入
查看>>
Python-刷新页面,直到元素可单击
查看>>
PYTHON-唤醒+VOSK+DEEPSEEK+离线合成-实现纯离线大模型问答
查看>>
Python-在Dash回调中重用函数
查看>>
PYTHON-基本数据类型-元祖类型,字典类型,集合类型
查看>>
python-外观模式
查看>>
Python-如何使用FastAPI和uvicorn.run而不阻塞线程?
查看>>
Python-字典
查看>>
python-字符串
查看>>
Python-将二进制遮罩转换为多边形
查看>>
python-文件操作
查看>>
PYTHON-模块 sys os random shutil-练习
查看>>
Python-模块与包的管理
查看>>