zoukankan      html  css  js  c++  java
  • SpringMVC框架一:搭建测试

    这里做一个Demo:展示商品列表

    新建Dynamic Web Project:

    导入jar包,放在lib下:

     

    放入Lib文件夹之后,会自动build path

    接下来配置web.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://java.sun.com/xml/ns/javaee"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        id="WebApp_ID" version="2.5">
        <display-name>springMVC</display-name>
        <welcome-file-list>
            <welcome-file>index.html</welcome-file>
            <welcome-file>index.htm</welcome-file>
            <welcome-file>index.jsp</welcome-file>
            <welcome-file>default.html</welcome-file>
            <welcome-file>default.htm</welcome-file>
            <welcome-file>default.jsp</welcome-file>
        </welcome-file-list>
    
        <!-- 前端控制器 -->
        <servlet>
            <servlet-name>springmvc</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:springmvc.xml</param-value>
            </init-param>
        </servlet>
    
        <servlet-mapping>
            <servlet-name>springmvc</servlet-name>
            <url-pattern>*.action</url-pattern>
        </servlet-mapping>
    </web-app>

    简单写一个商品列表的jsp页面:itemList.jsp

    <%@ 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="${pageContext.request.contextPath }/item/queryitem.action" method="post">
    查询条件:
    <table width="100%" border=1>
    <tr>
    <td><input type="submit" value="查询"/></td>
    </tr>
    </table>
    商品列表:
    <table width="100%" border=1>
    <tr>
        <td>商品名称</td>
        <td>商品价格</td>
        <td>生产日期</td>
        <td>商品描述</td>
        <td>操作</td>
    </tr>
    <c:forEach items="${itemList }" var="item">
    <tr>
        <td>${item.name }</td>
        <td>${item.price }</td>
        <td><fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
        <td>${item.detail }</td>
        
        <td><a href="${pageContext.request.contextPath }/itemEdit.action?id=${item.id}">修改</a></td>
    
    </tr>
    </c:forEach>
    
    </table>
    </form>
    </body>
    
    </html>

     商品Items的POJO类:

    package org.dreamtech.springmvc.pojo;
    
    import java.util.Date;
    
    public class Items {
    
        public Items(Integer id, String name, Float price, Date createtime, String detail) {
            super();
            this.id = id;
            this.name = name;
            this.price = price;
            this.createtime = createtime;
            this.detail = detail;
        }
    
        private Integer id;
    
        private String name;
    
        private Float price;
    
        private String pic;
    
        private Date createtime;
    
        private String detail;
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name == null ? null : name.trim();
        }
    
        public Float getPrice() {
            return price;
        }
    
        public void setPrice(Float price) {
            this.price = price;
        }
    
        public String getPic() {
            return pic;
        }
    
        public void setPic(String pic) {
            this.pic = pic == null ? null : pic.trim();
        }
    
        public Date getCreatetime() {
            return createtime;
        }
    
        public void setCreatetime(Date createtime) {
            this.createtime = createtime;
        }
    
        public String getDetail() {
            return detail;
        }
    
        public void setDetail(String detail) {
            this.detail = detail == null ? null : detail.trim();
        }
    }
    View Code

    springmvc配置文件:SpringMVC.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    
        <!-- 扫描@Controler @Service -->
        <context:component-scan base-package="org.dreamtech" />
    
    
    </beans>

    ItemController:

    package org.dreamtech.springmvc.controller;
    
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;
    
    import org.dreamtech.springmvc.pojo.Items;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;
    
    @Controller
    public class ItemController {
    
        @RequestMapping(value = "/item/itemlist.action")
        public ModelAndView itemList() {
    
            // 创建页面需要显示的商品数据(模拟从数据库中取出)
            List<Items> list = new ArrayList<Items>();
            list.add(new Items(1, "1华为 荣耀", 2399f, new Date(), "质量好!1"));
            list.add(new Items(2, "2华为 荣耀", 2399f, new Date(), "质量好!2"));
            list.add(new Items(3, "3华为 荣耀", 2399f, new Date(), "质量好!3"));
            list.add(new Items(4, "4华为 荣耀", 2399f, new Date(), "质量好!4"));
            list.add(new Items(5, "5华为 荣耀", 2399f, new Date(), "质量好!5"));
            list.add(new Items(6, "6华为 荣耀", 2399f, new Date(), "质量好!6"));
    
            ModelAndView mav = new ModelAndView();
            mav.addObject("itemList", list);
            mav.setViewName("/WEB-INF/jsp/itemList.jsp");
            return mav;
        }
    }

    这里返回的是ModelAndView:这种是最全的返回类型

    另外可以返回:String,直接返回视图名即可,如果要传参,带个Model,model.addAttribute()即可

    这种方式推荐使用:

    重定向:返回"redirect:/xx/xxx":用于修改完信息后

    转发:返回"forward:/xx/xxx"

    最后一种返回voic:适用于无返回视图的情况:JSON,如果强行返回或者传参,需要servlet原生方法

    启动Tomcat:

    访问:http://localhost:8080/springMVC/item/itemlist.action

    看到页面和数据:

    到这里,第一个小案例就完成了

    后边会仔细介绍SpringMVC

  • 相关阅读:
    神经网络系列学习笔记(一)——神经网络之ANN学习资料汇总
    时间序列分析算法学习笔记
    推荐算法学习笔记
    神经网络系列学习笔记(二)——神经网络之DNN学习笔记
    AB test学习笔记
    大数据的存储——HBase、HIVE、MYSQL数据库学习笔记
    本地已经存在的项目如何跟github发生关联
    深度神经网络对脑电信号运动想象动作的在线解码
    脑机音乐接口,高效检测用户的情绪状态
    将深度学习技术应用于基于情境感知的情绪识别
  • 原文地址:https://www.cnblogs.com/xuyiqing/p/9417353.html
Copyright © 2011-2022 走看看