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;
}
}
结果
欢迎关注我的微信公众号:安卓圈
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
2018-06-19 unity打aar包工具
2016-06-19 【设计模式】中介者模式
2016-06-19 【设计模式】迭代器模式
2016-06-19 【设计模式】解释器模式
2016-06-19 【设计模式】命令模式