Spring(一)第一个入门案例

入门案例:用户提交一个请求,服务器端处理器在接受到这个请求后,在页面上展示出欢迎信息

一:引入依赖

<dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.3</version>
      <scope>test</scope>
    </dependency>

    <!--引入beans节点-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>4.2.0.RELEASE</version>
    </dependency>


    <dependency>
      <groupId> org.aspectj</groupId >
      <artifactId> aspectjweaver</artifactId >
      <version> 1.8.7</version>
    </dependency>

    <!--ServletAPI-->
    <dependency>
      <groupId>javaee</groupId>
      <artifactId>javaee-api</artifactId>
      <version>5</version>
    </dependency>

    <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
    <scope>runtime</scope>
  </dependency>
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.2.0.RELEASE</version> </dependency> <!--SpringWebMVC--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.2.0.RELEASE</version> </dependency>


二:配置文件:

1)在web.xml中配置前端控制器(需要自己手动配置)

前端控制器本身是一个servlet 即DispatcherServlet

2)处理器映射器(系统自带可以不配)

3)处理器适配器(系统自带可以不配)

4)处理器(需要自己手动配置)

5)视图解析器(系统自带可以不配)

在WEB.XML里写入以下代码:

      <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
          <init-param>
              <param-name>contextConfigLocation</param-name>
              <param-value>classpath:applicationContexday01.xml</param-value>
          </init-param>
          <load-on-startup>1</load-on-startup>
      </servlet>

    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

  三:写一个类实现controller接口

  

public class FirstController implements Controller {
    public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/index.jsp");
        return mv;
    }
}

  四:最后一步,写xml配置文件:

  

<bean id="/xxx" class="cn.mvc.day01.FirstController"></bean>

  

posted @ 2018-03-21 08:52  Gtr_Right  阅读(172)  评论(0编辑  收藏  举报