springMvc初探
简单实现springMvc请求(基于maven的实现)
1.1在pom.xml中引入springMvc和servlet3.0的依赖包
1. <dependencies>
2. <!-- springmvc依赖 -->
3. <dependency>
4. <groupId>org.springframework</groupId>
5. <artifactId>spring-webmvc</artifactId>
6. <version>4.3.13.RELEASE</version>
7. </dependency>
8. <dependency>
9. <groupId>javax.servlet</groupId>
10. <artifactId>servlet-api</artifactId>
11. <version>3.0-alpha-1</version>
12. <!--防止冲突 于tomcat中的servlet冲突 -->
13. <scope>provided</scope>
14. </dependency>
15. </dependencies>
1.2 在web.xml中配置springMvc的核心前端控制器 DispatcherServlet
1. <!-- springMvc的核心前端控制器DispatcherServlet -->
2. <servlet>
3. <servlet-name>springMvc</servlet-name>
4. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
5. <init-param>
6. <!-- 必须指定springmvc的初始化路径 参数为contextConfigLocation -->
7. <!-- 可以自定义servlet.xml配置文件的位置和名称,默认为WEB-INF目录下,名称为[<servlet-name>]-servlet.xml,如springMvc-servlet.xml-->
8. <param-name>contextConfigLocation</param-name>
9. <!-- 此处为空会报错 Could not open ServletContext resource [/WEB-INF/springMvc-servlet.xml] -->
10. <!-- 如果配置了spring的ContextLoaderListener监听器会默认去找/WEB-INF/applicationContext.xml-->
11. <!-- spring是父容器,springmvc是子容器 -->
12. <!-- 此处作为spring的简单实现 ,必须在配置文件中指定扫描包让spring可以找到容器 -->
13. <param-value>classpath:springMvc-servlet.xml</param-value>
14. </init-param>
15. <load-on-startup>1</load-on-startup>
16. </servlet>
17. <!-- 拦截路径 -->
18. <servlet-mapping>
19. <servlet-name>springMvc</servlet-name>
20. <url-pattern>/</url-pattern>
21. </servlet-mapping>
22. <!-- <listener> -->
23. <!-- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> -->
24. <!-- </listener> -->
1.3在 springMvc-servlet.xml中配置包扫描
1. <?xml version="1.0" encoding="UTF-8"?>
2. <beans xmlns="http://www.springframework.org/schema/beans"
3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
4. xmlns:context="http://www.springframework.org/schema/context"
5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
6. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
7. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
8. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
9.
10. <!-- 启用spring mvc 高级特性 包括 静态资源 视图解析器 上传下载 乱码处理等 -->
11. <!-- <context:annotation-config /> -->
12.
13. <!-- 设置使用注解的类所在的jar包 ,包括递归子包中的类都会被扫描-->
14. <context:component-scan base-package="cn.hxl"></context:component-scan>
15. <!-- 对转向页面的路径解析。prefix:前缀, suffix:后缀 -->
16. <!-- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" /> -->
17.
18. </beans>
1.3 简单的请求 直接看代码
1. @Controller
2. public class SpringMvcIntroct {
3.
4. @RequestMapping("/hello")
5. //因为此处我们没有开启视图的解析,直接返回字符串用@ResponseBody
6. @ResponseBody
7. public String hello(){
8. return "hello";
9. }
10. }
在浏览器中输入访问路径会出现 hello
到此 springmvc的简单实现完成