springmvc项目的搭建

springmvc替代servlet的工作

Servlet - Springmvc       
jsp ->Servlet (Springmvc)->Jsp

springmvc配置文件 springmvc.xml
选中常用的命名空间:beans  aop context  mvc

普通的servlet流程:
请求-url-pattern -交给对应的servlet去处理

如果现在想用springmvc,而不是普通的servlet,如何告知程序?-如何让springmvc 介入程序:
需要配置一个 Springmvc自带的servlet

通过以下配置,拦截所有请求,交给SpringMVC处理:

web.xml:

 <servlet>
      <servlet-name>springDispatcherServlet</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
              <param-name>contextConfigLocation</param-name>         //指定读取mvc的配置文件路径
              <param-value>classpath:springmvc.xml</param-value>
      </init-param>
      <load-on-startup>1</load-on-startup>
  </servlet>
 
  <servlet-mapping>
      <servlet-name>springDispatcherServlet</servlet-name>         //servlet-mapping:用于拦截请求     servlet:在 servlet-mapping拦截后交给servlet-class中的类去处理
      <url-pattern>/</url-pattern>                    //对应的servlet-name要一致
  </servlet-mapping>

/:一切请求  ,注意不是 /*
/user:拦截以 /user开头的请求
/user/abc.do  :只拦截该请求
.action:只拦截 .action结尾的请求

 

假如项目需要同时兼容 springMVC和Servlet,怎么做?

1,<url-pattern>不要再用'/'全部拦截了,否则都交给了mvc处理

2,指定具体的url路径,分别交给二者去处理

web.xml:

  <servlet>
      <servlet-name>springDispatcherServlet</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>springDispatcherServlet</servlet-name> <url-pattern>.action</url-pattern> </servlet-mapping>

 

web.xml:

<!-- 配置spring读取的配置文件 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:spring_dao.xml
            classpath:spring_service.xml
    </param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

<!-- 配置前端控制器 -->
    <servlet>
        <servlet-name>SpringMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>                 //指定读取mvc的配置文件路径
            <param-value>classpath:spring-web-mvc.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>

springMvc.xml:

<!-- 自动注册相关bean,用以支持SpringMVC的注解 -->
    <mvc:annotation-driven/>
    
    <!-- 扫描哪个包下面有Spring中加了注解的组件 例如我们的Controller -->
    <context:component-scan base-package="com.briup"></context:component-scan>
    
    <!-- 注册视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>  
        <property name="prefix" value="/WEB-INF/jsp/"/>  
        <property name="suffix" value=".jsp"/>  
    </bean>
    
    <!-- 处理静态资源的访问 -->
    <mvc:resources mapping="/images/**" location="/images/"/>  
    <mvc:resources mapping="/js/**" location="/js/"/>  
    <mvc:resources mapping="/css/**" location="/css/"/> 
    <mvc:resources mapping="/fonts/**" location="/fonts/"/> 
    <mvc:resources mapping="/script/**" location="/script/"/> 
    
    
    <!--自己在xml中定义的controller,不用在java类中再写对应的controller了-->
    <mvc:view-controller path="/index" view-name="index"/>
    <mvc:view-controller path="/top" view-name="top"/>
    <mvc:view-controller path="/switch" view-name="switch"/>
    <mvc:view-controller path="/left" view-name="left"/>

 

posted @ 2019-09-12 10:44  千里之外kb  阅读(181)  评论(0编辑  收藏  举报