2.springmvc_demo(传智播客)

需求:查询商品列表(静态数据模拟)

一.环境搭建

1
2
添加相关依赖
3

二.开发步骤

1.web.xml

完成springmvc配置文件的加载以及将所有请求交由springmvc解析

<display-name>Archetype Created Web Application</display-name>
<servlet>
  <servlet-name>springmvc</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <!-- 加载springmvc配置文件 -->
  <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring/springmvc.xml</param-value>
  </init-param>
</servlet>
<!-- 配置将所有请求交给springmvc来处理 -->
<servlet-mapping>
  <servlet-name>springmvc</servlet-name>
  <url-pattern>/</url-pattern>
</servlet-mapping>

2.springmvc.xml

完成处理器、处理器映射器、处理器适配器以及试图解析器的配置

<!-- 1.配置处理器 -->
<!-- 可以扫描controller、service、... 这里让扫描controller,指定controller的包 -->
<context:component-scan base-package="com.steven.ssm.controller"></context:component-scan>

<!-- 2.配置处理器映射起和处理器适配器 -->
<!-- 使用mvc:annotation-driven配置注解映射器和注解适配器,其默认加载很多的参数绑定方法,比如json转换解析器等等-->
<mvc:annotation-driven></mvc:annotation-driven>

<!-- 3.配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
   <!-- 配置jsp路径的前缀 -->
   <property name="prefix" value="/WEB-INF/views/"/>
   <!-- 配置jsp路径的后缀 -->
   <property name="suffix" value=".jsp"/>
</bean>

3.处理器(控制器)的开发

@Controller
@RequestMapping("/items")
public class ItemsController {
    //商品查询列表
    @RequestMapping("/queryItems")
    public ModelAndView queryItems() throws Exception{
        //调用service查找数据库,查询商品列表,这里使用静态数据模拟
        List<Items> itemsList = new ArrayList<Items>();
        Items items_1 = new Items();
        items_1.setId(1);
        items_1.setName("联想笔记本");
        items_1.setPrice(6000f);
        items_1.setDetail("ThinkPad T430 c3 联想笔记本电脑!");

        Items items_2 = new Items();
        items_2.setId(2);
        items_2.setName("苹果手机");
        items_2.setPrice(5000f);
        items_2.setDetail("iphone6苹果手机!");

        itemsList.add(items_1);
        itemsList.add(items_2);
        //返回ModelAndView
        ModelAndView modelAndView = new ModelAndView();
        //相当于request的setAttribute方法,在jsp页面中通过itemsList取数据
        modelAndView.addObject("itemsList",itemsList);
        //指定视图
        modelAndView.setViewName("items/queryItems");
        return modelAndView;
    }
}

4.视图的开发

<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>查询商品列表</title>
</head>
<body>
<form action="" method="post">
    商品列表:
    <table width="100%" border=1>
        <tr>
            <td>商品编号</td>
            <td>商品名称</td>
            <td>商品价格</td>
            <td>商品描述</td>
        </tr>
        <c:forEach items="${itemsList }" var="item">
            <tr>
                <td>${item.id}</td>
                <td>${item.name}</td>
                <td>${item.price }</td>
                <td>${item.detail }</td>
            </tr>
        </c:forEach>
    </table>
</form>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/u010286027/article/details/84227323