jeesite学习(二)----回顾SpringMVC

一、其配置文件

  1. web.xml
    <load-on-startup>1</load-on-startup>表示启动容器时初始化该Servlet
    <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext-springmvc.xml</param-value> </init-param>在这里可以指定SpringMVC配置文件的位置。
    如果不想指定位置,就默认在/WEB-INF/'名字-servlet.xml’位置,'名字’和servlet-name一样
    <url-pattern>/</url-pattern>表示哪些请求交给Spring Web MVC处理, “/” 是用来定义默认servlet映射的。也可以是“.html”、“.jsp”
	<!-- 2.配置SpringMvc的前端控制器 拦截所有请求 -->
	<servlet>
		<!-- <description>spring mvc servlet</description> -->
		<servlet-name>dispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- 指定SpringMvc配置文件的位置 如果不指定,就在web.xml同级文件下建一个'名字-servlet.xml','名字'和servlet-name一样 -->
		<init-param>
			<!-- <description>spring mvc 配置文件</description> -->
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:applicationContext-springmvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>dispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

2.dispatcherServlet-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:task="http://www.springframework.org/schema/task"
	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
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd">

	<!-- SpringMvc的配置文件 包含 网站跳转逻辑的控制及配置 -->
	<!-- 1.自动扫描 use-default-filters-只扫描默认的 -->
	<context:component-scan base-package="com.smxy.ssm_crud"
		use-default-filters="false">
		<!-- 只扫描控制器 -->
		<context:include-filter type="annotation"
			expression="org.springframework.stereotype.Controller" />
		<context:include-filter type="annotation"
			expression="org.springframework.web.bind.annotation.ControllerAdvice" />
	</context:component-scan>

	<!-- 2.配置视图解析器,方便页面返回 -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/views/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>

	<!-- 3.俩个标准配置 -->
	<!-- 将mvc不能处理的请求交给tomcat,实现静态资源也可以访问成功 -->
	<mvc:default-servlet-handler />
	<!-- 能支持SpringMvc更高级的一下功能  	,映射动态请求,JSP303校验,快捷的ajax -->
	<mvc:annotation-driven />
</beans>

二、运行原理
1.下图来自黑马程序员。
在这里插入图片描述
2.结合代码理解
在这里插入图片描述三、MVC中的注解

名称
@Controller 定义控制器类
@RequestMapping 用来处理请求地址映射的注解,可用于类或方法上
@Resource 为J2EE提供的注解,需要导入包javax.annotation.Resource,默认按照ByName自动注入
@Autowired 为Spring提供的注解,需要导入包org.springframework.beans.factory.annotation.Autowired;只按照byType注入。
@ModelAttribute 传递和保存数据
@SessionAttribute(s) 将值放到session作用域中,写在class上面。
@PathVariable 将请求URL中的模板变量映射到功能处理方法的参数上,即取出uri模板中的变量作为参数
@requestParam 用于在SpringMVC后台控制层获取参数
@ResponseBody 用于将Controller的方法返回的对象,例如返回json、xml等等
@Component 相当于通用的注解,当不知道一些类归到哪个层时使用,但是不建议。
@Repository 用于注解dao层,在daoImpl类上面注解

点这里,看更多注解https://blog.csdn.net/fzy198926/article/details/78358068

猜你喜欢

转载自blog.csdn.net/qq_37971615/article/details/85697405
今日推荐