1、写在前面
在项目真实开发过程中,框架使用太多,已经不知道Spring的最简形式和原理了,在此记录学习过程
2、核心操作
- @Controller接口暴露
- 请求网址测试
- 静态页面下载
- 添加登录拦截器
3、详细操作
3.1、@Controller接口暴露
@Controller在springboot中直接标注,然后就可以进项目的接口访问,但是在SpringMVC的架构中不是这样
DispatchcerServlet是Spring实现的核心组件,其原理决定了为什么Spring不能直接是要科技馆Controller标注
1)添加POM依赖
2)web.xml添加依赖(DispatchcerServlet)监听器
<!-- 配置DispatchcerServlet -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置Spring mvc下的配置文件的位置和名称 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--配置过滤器-->
<filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
3)添加spring-mvc.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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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">
<description>Spring MVC Configuration</description>
<!-- 配置自动扫描的包, 使用 Annotation 自动注册 Bean,只扫描 @Controller -->
<context:component-scan base-package="com.enzoism.spring" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 默认的注解映射的支持 -->
<mvc:annotation-driven />
<!-- 定义视图文件解析, 配置视图解析器 如何把handler 方法返回值解析为实际的物理视图 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 静态资源映射 -->
<mvc:resources mapping="/static/**" location="/static/" cache-period="31536000"/>
</beans>
3.2、请求网址测试
- localhost:8082/test/index
- localhost:8082/test/main
- localhost:8082/test/json
- localhost:8082/test/hello
- localhost:8082/main/index
- http://localhost:8082/static/index.html
说明:没有使用Controller以前,html静态是可以进行访问的,添加dispatchcerServlet后,所有的请求都被拦截,静态页面只能在指定的目录下才能够被访问,其余所有JSP页面跳转都是controller请求
3.3、静态页面下载
- 静态登录模板下载
- 静态登录百度云
- 当前代码下载(master分支):https://gitee.com/enzoism/java-simple-springmvc
3.4、添加登录拦截器
- 当前代码下载(master-01-interceptor分支):https://gitee.com/enzoism/java-simple-springmvc
网友评论