SSM项目的启动过程:
ssm框架中,项目启动过程以及web.xml配置详解_菜鸟不会飞-CSDN博客_ssm项目启动入口是什么本篇主要在基于SSM的框架,深入讲解web.xml的配置web.xml 每个javaEE项目中都会有,web.xml文件是用来初始化配置信息:比如Welcome页面、servlet、servlet-mapping、filter、listener、启动加载级别等。 web.xml配置文件内容如下: <!DOCTYPE web-app PUBLIC "-/...https://blog.csdn.net/qq_35571554/article/details/82385838理解:tomcat启动会根据web.xml的配置去激活spring和springMVC对应的两个IOC容器,IOC容器会分别去初始化自己,比如查找自己管理的bean。
=================================================================
XML配置SSM项目需要的三大配置文件:
1、WEN-INF文件夹下的web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<!-- 实例化ApplicationContext容器 -->
<context-param>
<!-- 加载(resources目录下,该目录下的东西打成war时会自动到src目录下去)src目录下的applicationContext.xml文件 -->
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml
</param-value>
</context-param>
<!-- 指定以ContextLoaderListener方式启动Spring容器,启动IOC的初始化工作 -->
<!-- ServletContext用来存放全局变量,每个Java虚拟机每个Web项目只有一个ServletContext,
这个ServletContext是由Web服务器创建的,来保证它的唯一性。
由于一个WEB应用中的所有Servlet共享同一个ServletContext对象,!!!!!!!!!!!!!!!!!!!!!
因此Servlet对象之间可以通过ServletContext对象通讯。ServletContext对象通常也被称之为Context域对象。-->
<!-- ContextLoaderListener继承自ContextLoader,实现的是ServletContextListener接口 -->
<!--ContextLoaderListener的作用就是启动Web容器时,读取在contextConfigLocation中定义的xml文件,自动装配ApplicationContext的配置信息,并产生WebApplicationContext对象,然后将这个对象放置在ServletContext的属性里,这样我们只要得到Servlet就可以得到WebApplicationContext对象,并利用这个对象访问spring容器管理的bean。
简单来说,就是上面这段配置为项目提供了spring支持,初始化了Ioc容器。-->
<!--https://www.jianshu.com/p/523bfddf0810-->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!--==========上面为普通spring IOC容器的上下文,存放除了MVC(controller、Exception的bean)之外的所有bean==-->
<!--springmvc的配置文件到底放哪里?
1.普通的Java项目(不用maven管理):
当web.xml中DispatcherServlet配置声明中,没有明确DispatcherServlet前端控制器配置文件的位置时,则系统默认DispatcherServlet前端控制器配置文件放在WEB-INF文件夹下。
当web.xml中DispatcherServlet配置声明中,已经明确DispatcherServlet前端控制器配置文件的位置时,则必须将前端控制器的配置文件放在src下面,不能放在webcontent(web)下了,当然,在src的各级子文件夹中也是可以的。一般放在java resources下面,或者在resources下新建一个config的文件夹专门用来存放配置文件
原文链接:https://blog.csdn.net/qq_45796667/article/details/118056282-->
<!--配置springMVC前端控制器DispatcherServlet,负责分发请求到对应的controller -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--关联MVC的配置文件地址,不设置的话默认放在WEB-INF文件夹下.因为我这使用fileUpload配置路径,MVC配置文件还必须放在WEB-INF下面
,对于每次重启tomcat文件丢失的问题,可以修改tomcat的server.xml配置虚拟路径-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!--匹配请求/不包含.JSP-->
<!--匹配请求/*包含.JSP-->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--==========上面为MVC IOC容器的上下文,存放MVC(controller、Exception的bean)相关的所有bean。这个IOC容器其实是上面那个容器的子类,所以我能用你的,你不能用我存的bean==-->
<!--两个容器各管各的,层次分明,功能清晰。-->
<!-- 避免中文乱码 -->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
2、spring核心容器的配置文件applicationContext.xml。maven项目的话放在src文件夹下或者resources文件夹下都行,因为最终都会编译到一个文件夹下。
<?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"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 配置数据源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/shop?characterEncoding=utf8"/>
<property name="username" value="root"/>
<property name="password" value="1234"/>
<!-- 最大连接数 -->
<property name="maxActive" value="30"/>
<!-- 最大空闲连接数 -->
<property name="maxWait" value="10"/>
<!-- 初始化连接数 -->
<property name="initialSize" value="5"/>
</bean>
<!-- 添加事务支持 -->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--============使用注解的话以上配置对应的配置类是JdbcConfig======================================-->
<!-- 开启事务注解-->
<!--============使用注解的话是在主配置类上使用@EnableTransactionManagement========================-->
<tx:annotation-driven transaction-manager="txManager"/>
<!--=========================================================================================-->
<!-- 配置MyBatis工厂,同时指定数据源,并与MyBatis完美整合 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- configLocation的属性值为MyBatis的核心配置文件 -->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<!--Mapper代理开发,使用Spring自动扫描MyBatis的接口并装配
(Spring将指定包中所有被@Mapper注解标注的接口自动装配为MyBatis的映射接口) -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- mybatis-spring组件的扫描器 ,必须写全dao的包名,且只能扫描一个dao包-->
<property name="basePackage" value="com.dao"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
<!--============使用注解的话是mybatis这两个配置对应的配置类是MyBatisConfig========================-->
<!--将service也声明为bean方便controller层调用-->
<!-- 指定需要扫描的包(包括子包),使注解生效。dao包在mybatis-spring组件中已经扫描,这里不再需要扫描-->
<context:component-scan base-package="com.service"/>
</beans>
3、springmvc的配置文件springmvc-servlet.xml。放在resources文件夹或者WEB-INF文件夹下都行,在web.xml中未指定的话默然是要放在WEB-INF下。
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--开启注解功能-->
<mvc:annotation-driven/>
<!-- 静态资源需要单独处理,不需要dispatcher servlet -->
<mvc:resources location="/css/" mapping="/css/**"></mvc:resources>
<mvc:resources location="/images/" mapping="/images/**"></mvc:resources>
<!-- 查看图片时,logos文件夹不需要dispatcher servlet -->
<mvc:resources location="/logos/" mapping="/logos/**"></mvc:resources>
<!-- 配置视图解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver">
<!-- RequestMapping注解的方法的返回值自动加上前缀 -->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!-- RequestMapping注解的方法的返回值自动加上后缀 -->
<property name="suffix" value=".jsp"/>
</bean>
<!-- 配置MultipartResolver 用于文件上传 使用spring的CommosMultipartResolver -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
p:defaultEncoding="UTF-8"
p:maxUploadSize="5400000"
p:uploadTempDir="fileUpload/temp"
>
</bean>
<!-- defaultEncoding="UTF-8" 是请求的编码格式,默认为iso-8859-1
maxUploadSize="5400000" 是允许上传文件的最大值,单位为字节
uploadTempDir="fileUpload/temp" 为上传文件的临时路径-->
<!-- 托管统一项目异常处理MyExceptionHandler -->
<bean class="com.exception.MyExceptionHandler"/>
<!-- 使用扫描机制,扫描包,将controller对应的bean交给MVC对应的IOC容器管理-->
<context:component-scan base-package="com.controller"/>
</beans>
=========================================================================
SSM未前后端分离的电商网站项目:
环境:IDEA2021+JDK8+MAVEN3.8+TOMCAT7插件
前端(实际未分离):jsp+el表达式+jstl标签库+spring标签库
后端:见POM.XML相关依赖,主要有数据库MySQL5.7 ,数据源Druid,持久层狂框架mybatis,mybatis-spring、spring核心容器、spring事务、spring MVC、测试junit和spring-test
web服务器:tomcat8.5(通过IDEA配置)
=====================================
重点:统一异常处理
数据交互:未使用JSON!纯用MVC原装的传参方式,参考下文
springMVC参数绑定与数据回显 - 小虾米的java梦 - 博客园简单例子:修改商品信息的jsp页面: 参数绑定过程: 1.2.1 默认支持的参数类型 处理器形参中添加如下类型的参数处理适配器会默认识别并进行赋值。 1.1.1 HttpServletRequesthttps://www.cnblogs.com/fengli9998/p/6651516.html 后端往前端传数据:前端将数据放入Model对象(model.addAttribute("allGoods", allGoods);
),return转发到对应的JSP页面,JSP页面可以通过EL表达式取出${"xxx"},搭配JSTL的C:foreach等遍历。
前端往前端传数据:在前端JSP页面使用spring提供的标签库中的form表单,通过表单的属性modelAttribute=“XXX”,将表单的内容装入一个Model对象,点击提交后,后端controller通过@ModelAttribute注解将Model对象的字段挨个映射到具体的对象,比如User、Goods。
服务端会话技术:session存储用户是否登录的信息
=================================================================
功能:
购买者分类查看、查看单个商品详情、关注商品、用户中心(查看过往订单、关注的商品)、加入购物车、清空购物车、结算、支付(一张图片代替)
管理员:后台商品上架、删除、修改库存等,商品类型管理、用户管理、订单管理(未付款的可以删除)、公告发布
项目结构:
===============================================================
POM.XML
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<packaging>war</packaging>
<name>ssm_eshop</name>
<groupId>org.example</groupId>
<artifactId>ssm_eshop</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!--连接数据库、连接池、CRUD简化-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.8</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
<!--MVC需要依赖servlet,JSP也要依赖servlet-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<!--spring核心容器和web-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.12</version>
</dependency>
<!--spring事务依赖于JDBC事务-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.12</version>
</dependency>
<!--JSP读取商品图片和图片落盘-->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
<!--页面JSP,jstl简化jsp书写-->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.3.12</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
主要的三个配置文件web.xml,spring和兴配置文件、springMVC配置文件如前文所述。
完整代码见gitee: