博客
关于我
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基础内容_Python基础入门必须了解的
查看>>
Python基础之面向对象2(封装)
查看>>
Python基础之给函数增加元信息
查看>>
python基础之字符编码
查看>>
python基础之多线程与多进程(二)
查看>>
Python基础——MySQL数据库基本操作
查看>>
Python基础_列表 list
查看>>
python基础---->常用模块
查看>>
Python基础(2):控制结构
查看>>
Python基础 | 关于“循环”那些事
查看>>
python系列【仅供参考】:python 远程rdp
查看>>
python基于flask搭建http服务(四)—— Docker容器化部署
查看>>
Python块键盘/鼠标输入
查看>>
python在使用HTMLTestRunner时,报告为空,错误提示<_io.TextIOWrapper name='<stderr>' mode='w' encoding='utf_8'>...
查看>>
python在gpu上运行_【python】python开启GPU加速
查看>>
Python在def函数中使用for循环
查看>>
Python在def函数中使用for循环
查看>>
Python在Conda环境中,但在Windows虚拟环境中没有激活
查看>>
python系列【仅供参考】:python pip 错误 ModuleNotFoundError: No module named pip._internal 解决办法
查看>>
python图像条状状噪声,使用PYTHON PIL从验证码图像中删除背景嘈杂的线条
查看>>