博客
关于我
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/

你可能感兴趣的文章
ogg参数解析
查看>>
ognl详解
查看>>
Oil Deposits
查看>>
OJ中处理超大数据的方法
查看>>
OJ中常见的一种presentation error解决方法
查看>>
OK335xS UART device registe hacking
查看>>
ok6410内存初始化
查看>>
Okhttp3添加拦截器后,报错,java.io.IOException: unexpected end of stream on okhttp3.Address
查看>>
OKR为什么到今天才突然火了?
查看>>
ol3 Demo2 ----地图搜索功能
查看>>
OLAP在大数据时代的挑战
查看>>
oldboy.16课
查看>>
OLEDB IMEX行数限制的问题
查看>>
ollama 如何删除本地模型文件?
查看>>
ollama-python-Python快速部署Llama 3等大型语言模型最简单方法
查看>>
Ollama怎么启动.gguf 大模型
查看>>
ollama本地部署DeepSeek(Window图文说明)
查看>>
ollama运行多模态模型如何进行api测试?
查看>>
OMG,此神器可一次定一周的外卖
查看>>
Omi 多端开发之 - omip 适配 h5 原理揭秘
查看>>