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

你可能感兴趣的文章
php 代码混淆
查看>>
PHP 使用 $_SERVER['PHP_SELF'] 获取当前页面地址及其安全性问题
查看>>
Redis系列之如何避免缓存击穿
查看>>
php 内存分析
查看>>
PHP 函数名前面加&
查看>>
php 删除包含某一字符的数组元素
查看>>
Redis学习总结(19)——Redis 5种集群方式对比
查看>>
php 反射
查看>>
php 处理 大并发
查看>>
php 大文件上传
查看>>
php 子进程监听消息,swoole学习笔记之多线程端口监听问题记录 多进程epoll模式...
查看>>
PHP 学习笔记 (四)
查看>>
Redis入门概述
查看>>
php 实现Iterator 接口
查看>>
PHP 实现N阶矩阵相乘
查看>>
PHP 实现页面跳转的三种方式及详细解析
查看>>
php 将XML对象转化为数组
查看>>
PHP 工具
查看>>
php 常用方法
查看>>
PHP 并发扣款,保证数据一致性(悲观锁和乐观锁)
查看>>