<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 配置@Controller注解扫描 -->
<context:component-scan base-package="com.ken.controller" />
<!-- 如果没有显示的配置处理器映射器和处理器适配器, 那么springmvc就会去默认的dispatchServlet.properties中查找,
对应的处理器映射器和处理器适配器去使用, 这样每个请求都要扫描一次它的默认配置文件, 效率非常低,会降低访问速度 -->
<!-- 注解驱动。
作用:替我们配置最新版的注解的处理器映射器和处理器适配器
-->
<mvc:annotation-driven></mvc:annotation-driven>
</beans>
再来配视图解析器

我们当前还没有配置视图解析器,也能正常运行,是因为用了默认的解析器,jsp的解析器。jsp spring mvc但是,我们面临一个问题:
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 配置@Controller注解扫描 -->
<context:component-scan base-package="com.ken.controller" />
<!-- 如果没有显示的配置处理器映射器和处理器适配器, 那么springmvc就会去默认的dispatchServlet.properties中查找,
对应的处理器映射器和处理器适配器去使用, 这样每个请求都要扫描一次它的默认配置文件, 效率非常低,会降低访问速度 -->
<!-- 注解驱动。 作用:替我们配置最新版的注解的处理器映射器和处理器适配器 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 配置视图解析器.
作用:在Controller中指定页面路径的时候就不用写页面的完整路径名称了
-->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 真正的页面路径= 前缀 + 去掉后缀名的页面名称 + 后缀 -->
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
总结:
在springmvc的核心配置文件中做3件事:
配置注解扫描
配置注解驱动
配置视图解析器
本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/jisuanjixue/article-73123-3.html
10W*1
中国有这么勇敢就好
禽兽不如