SpringMVC环境搭建之Idea

SpringMVC
.. SpringMVC重要组件介绍
    1. DispacherServlet:前端控制器,接收所有请求,(如果配置/不包含jsp)
    2. HandlerMapping:解析请求格式,判断希望要执行哪个方法
    3. HandlerAdapter:负责调用具体的方法
    4. ViewResovler:视图解析器,解析结果,准备跳转的具体的视图

spring的jar包官方下载地址:

https://repo.spring.io/libs-release-local/org/springframework/spring/
环境搭建
    1.导入jar包
    2.在web.xml中配置前端控制器,若不配置<init-param>,会去/web-inf/<servlet-name>servlet.xml找
<servlet>
<servlet-name>qq</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>

<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>qq</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
  3.src下面新建springmvc.xml
   3.1.引入命名空间
<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">

<!--扫描注解-->
<context:component-scan base-package="com.bj.controller"></context:component-scan>
<!--注解驱动-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--放行静态资源两个*号表示文件夹下所有文件-->
<mvc:resources mapping="/js/**" location="/js/"></mvc:resources>
<mvc:resources mapping="/css/**" location="/css/"></mvc:resources>
<mvc:resources mapping="/imgs/**" location="/imgs/"></mvc:resources>
</beans>
3.2.编写控制器类

1 @Controller
2 public class Democontroller {
3 @RequestMapping("demo")
4      public String demo(){
5         return "main.jsp";
6     }
7 
8 }




猜你喜欢

转载自www.cnblogs.com/code-coffee/p/10257644.html