JavaEE项目的5层
Model层: JavaBean
DAO层实现技术:JDBC,MyBatis,HIbernate,JPA。 (IoC)
Service层实现技术:Java类。(IoC)
Controller层实现技术: Servlet, Spring MVC, Struts2, Struts1。
View层实现技术:JSP+HTML+CSS+JS+JSTL+EL+ Spring MVC Tag,
Spring MVC概念
Spring MVC是基于JavaEE和Spring的 VC层的框架。解决了其他框架所存在的缺点,是目前最流行的框架之一,也是目前开发项目使用最多的框架。
Spring MVC的组成
核心控制器DispatcherServlet,通过该控制器接收所有的请求,并对请求进行分发处理。
控制器Controller: 完成具体的控制器任务。
View的解析器:完成逻辑名到具体View对象的转换。
请求映射RequestMapping: 完成Controller的请求地址,请求方式等。
Spring MVC项目的配置
1.创建 Maven Web项目
查看此链接:https://blog.csdn.net/Kedongyu_/article/details/81365825
2.引入Spring JAR类库。
查看pom.xml文件下是否导入下列代码:
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.7.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.7.RELEASE</version>
</dependency>
添加上述代码并保存即可导入相关的类库。
3.创建Spring MVC单独的IoC容器配置文件: mvc-context.xml
创建mvc-context.cml文件,并将下列内容复制到文件中。
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
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/lang http://www.springframework.org/schema/lang/spring-lang.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<!-- 自动扫描注入 -->
<context:component-scan base-package="com.neusoft.first" />
<!-- 启动AOP aspectj切面代码服务 -->
<aop:aspectj-autoproxy/>
<!--View解析器-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--启用Spring MVC注解-->
<mvc:annotation-driven/>
</beans>
4.配置Spring MVC的核心控制器:DispatcherServlet
添加下列配置:
<servlet>
<servlet-name>app</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/mvc-context.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>app</servlet-name>
<url-pattern>*.mvc</url-pattern>
</servlet-mapping>
其中 <param-value>标签中设置Spring MVC 的Ioc容器文件路径,即步骤3的mvc-context.xml文件路径。
5.配置一个JSP View解析器:
在Spring MVC的IoC配置文件中mvc-context.xml查看是否添加下列内容:
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
到此,Spring MVC 基本配置已经完成。