SpringMVC基本

SpringMVC和Struts2的区别html

SpringMVC与Struts2区别java

对比项目web

SrpingMVCspring

Struts2安全

优点服务器

国内市场状况微信

有大量用户,通常新项目启动都会选用springmvc架构

有部分老用户,老项目组,因为习惯了,一直在使用。并发

国内状况,springmvc的使用率已经超过Struts2mvc

框架入口

基于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; } }

结果

欢迎关注个人微信公众号:安卓圈

相关文章
相关标签/搜索