zoukankan      html  css  js  c++  java
  • SpringMVC基本

    SpringMVC和Struts2的区别

    SpringMVC与Struts2区别

    对比项目

    SrpingMVC

    Struts2

    优势

    国内市场情况

    有大量用户,一般新项目启动都会选用springmvc

    有部分老用户,老项目组,由于习惯了,一直在使用。

    国内情况,springmvc的使用率已经超过Struts2

    框架入口

    基于servlet

    基于filter

    本质上没太大优势之分,只是配置方式不一样

    框架设计思想

    控制器基于方法级别的拦截,处理器设计为单实例

    控制器基于类级别的拦截, 处理器设计为多实例

    由于设计本身原因,造成了Struts2,通常来讲只能设计为多实例模式,相比于springmvc设计为单实例模式,Struts2会消耗更多的服务器内存。

    参数传递

    参数通过方法入参传递

    参数通过类的成员变量传递

    Struts2通过成员变量传递参数,导致了参数线程不安全,有可能引发并发的问题。

    与spring整合

    与spring同一家公司,可以与spring无缝整合

    需要整合包

    Springmvc可以更轻松与spring整合

    SpringMVC有三大组件:处理器映射器、处理器适配器、视图解析器

    SpringMVC的架构

    demo代码结构如下

    productList.jsp

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>商品列表</title>
    </head>
    <body>
    <h1>商品列表</h1>
    <table width="100%" border="1">
        <tr>
            <td>商品名称</td>
            <td>商品价格</td>
            <td>生产日期</td>
            <td>商品描述</td>
            <td>操作</td>
        </tr>
        <c:forEach items="${productList}" var="product">
            <tr>
                <td>${product.name}</td>
                <td>${product.price}</td>
                <td><fmt:formatDate value="${product.createTime}" pattern="yyyy-MM-da HH:mm:ss"/></td>
                <td>${product.detail}</td>
                <td><a href="${pageContext.request.contextPath}/productEdit.form?id=${product.id}">修改</a></td>
            </tr>
        </c:forEach>
    </table>
    </body>
    </html>

    dispatcher-servlet.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:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
        <!--配置controller扫描包-->
        <context:component-scan base-package="com.jinke.springmvc"/>
        <!--视图解析器的配置-->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsp/"/>
            <property name="suffix" value=".jsp"/>
        </bean>
    </beans>

    web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
             version="4.0">
        <!--加载核心配置文件-->
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/applicationContext.xml</param-value>
        </context-param>
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
        <!--核心控制器的配置-->
        <servlet>
            <servlet-name>dispatcher</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>dispatcher</servlet-name>
            <url-pattern>*.form</url-pattern>
        </servlet-mapping>
    </web-app>

    java类

    import java.util.Date;
    
    public class Product {
        private int id;
        private String name;
        private double price;
        private Date createTime;
        private String detail;
    
        public Product() {
        }
    
        public Product(int id, String name, double price, Date createTime, String detail) {
            this.id = id;
            this.name = name;
            this.price = price;
            this.createTime = createTime;
            this.detail = detail;
        }
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public double getPrice() {
            return price;
        }
    
        public void setPrice(double price) {
            this.price = price;
        }
    
        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;
        }
    }
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;
    
    import java.util.Arrays;
    import java.util.Date;
    import java.util.List;
    
    @Controller
    public class ProductControll {
    
        @RequestMapping("productList")
        public ModelAndView productList() {
            ModelAndView mav = new ModelAndView();
            List<Product> products = Arrays.asList(new Product(1, "1", 1111, new Date(), "1111")
                    , new Product(2, "2", 2222, new Date(), "2222")
                    , new Product(3, "3", 3333, new Date(), "3333")
                    , new Product(4, "4", 4444, new Date(), "4444"));
            mav.addObject("productList", products);
            mav.setViewName("productList");
            return mav;
        }
    }

    结果

    欢迎关注我的微信公众号:安卓圈

  • 相关阅读:
    整理之刷过的数据结构与算法题
    EM 算法求解高斯混合模型python实现
    C语言实现二叉树的基本操作
    scala 小结(一)
    Eclipse 配置运行Spark
    js替换字符串
    安装uni-ui
    判断当前时间的 时分 是否在一个时间段内
    vue 定时器实时刷新数据

  • 原文地址:https://www.cnblogs.com/anni-qianqian/p/11051463.html
Copyright © 2011-2022 走看看