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

你可能感兴趣的文章
python3的 pymysql把mysqldb库取代了,让python 3支持mysqldb的解决方法
查看>>
Python3笔记——IDE的选择
查看>>
python3简单使用requests 用户代理,cookie池
查看>>
Python3组播通信编程实现教程(发送者+接收者)
查看>>
Python3编写网络爬虫10-数据存储方式三-CSV文件存储
查看>>
python3读取redis返回数据带有'b'的问题
查看>>
Python3连接MySQL
查看>>
Python关于isinstance函数的解析 | 附Demo(全)
查看>>
Python3:setup.py:完成所有任务的PIP安装(Build_ext+Install)
查看>>
python4.0 gil_Python 并发编程系列--1、浅谈GIL历史及对多线程的影响
查看>>
python60行绘图程序_天底下最简单的QT画图板,就一个类,60行代码
查看>>
Python: AES加密与解密
查看>>
Python: Range() 最大尺寸;动态的还是静态的?
查看>>
Python: Windows下用 multiprocessing 的深坑
查看>>
python:Could not find platform independent libraries <prefix>解决方法
查看>>
Python:datetime tzinfo 时区名称文档
查看>>
Python:deepcopy(list) vs new_list = old_list[:]
查看>>
Python:Google API - 从消息中获取 mimeTypes
查看>>
Python:Google API - 从消息中获取 mimeTypes
查看>>
Python:subprocess.call,stdout to file,stderr to file,在屏幕上实时显示stderr
查看>>