课时1:SpringMVC环境搭建及第一个程序

.1)第一个SpringMVC程序

  1.servirt-----------SpringMVC

  2.常用的头文件

 xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
  http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.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

  3.流程

    3.1 普通的servlet流程

 <servlet>
    <servlet-name>servlet</servlet-name>
    <servlet-class>net.bt.servlet.TestServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>servlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

      3.1.1 请求 <url-pattern>-交给对应的servlet处理

    3.2 如果现在想用SpringMVC,而不是普通的servlet,如何告知程序?-如何让springMVC介入程序:

      3.2.1 需要在web.xml配置SpringMVC自带的Servlet 交给SpringMVC处理

<!--  处理请求-->
  <servlet>
    <servlet-name>servlet</servlet-name>
<!--    springMVC的servlet-->
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--    告知springMVC的配置文件在哪个里面-->
    <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>servlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

        3.2.1.1 /是拦截所有的请求 如果想拦截指定的请求/user/xxxx

      3.2.2 在springmvc中配置如下 注解的方式

<!--        配置扫描器-->
    <context:component-scan base-package="net.bt.handler"/>

    <!--    配置视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/views/">
        </property>
        <property name="suffix" value=".jsp">
        </property>
    </bean>

      3.2.3 编写servlet类

package net.bt.handler;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class TestServlet {

    @RequestMapping("we")
    public String welcome(){
        return "success";
    }
}

 

posted @ 2020-03-17 18:26  何邦柱  阅读(161)  评论(0编辑  收藏  举报