Spring MVC框架理解

原文链接:ITeye SpringMVC深度探险专栏

 

基本要素
1. 指定SpringMVC的入口程序(在web.xml中) 
复制代码
<!-- Processes application requests -->  
<servlet>  
    <servlet-name>dispatcher</servlet-name>  
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
    <load-on-startup>1</load-on-startup>  
</servlet>  
          
<servlet-mapping>  
    <servlet-name>dispatcher</servlet-name>  
    <url-pattern>/**</url-pattern>  
</servlet-mapping>  
复制代码

 


 
2. 编写SpringMVC的核心配置文件(在[servlet-name]-servlet.xml中) 
复制代码
<beans xmlns="http://www.springframework.org/schema/beans"  
       xmlns:mvc="http://www.springframework.org/schema/mvc"  
       xmlns:context="http://www.springframework.org/schema/context"  
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
       xsi:schemaLocation="  
            http://www.springframework.org/schema/beans  
            http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
            http://www.springframework.org/schema/context   
            http://www.springframework.org/schema/context/spring-context-3.1.xsd  
            http://www.springframework.org/schema/mvc  
            http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"   
       default-autowire="byName">  
      
    <!-- Enables the Spring MVC @Controller programming model -->  
    <mvc:annotation-driven />  
      
    <context:component-scan base-package="com.demo2do" />  
      
      
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
        <property name="prefix" value="/" />  
        <property name="suffix" value=".jsp" />  
    </bean>  
      
</beans>  
 
复制代码

 

3. 编写控制(Controller)层的代码 


复制代码
@Controller  
@RequestMapping  
public class UserController {  
  
    @RequestMapping("/login")  
    public ModelAndView login(String name, String password) {  
       // write your logic here   
           return new ModelAndView("success");  
    }  
  
}  
复制代码

 



程序化处理
    SpringMVC将Http处理流程抽象为一个又一个处理单元
    SpringMVC定义了一系列组件(接口)与所有的处理单元对应起来
    SpringMVC由DispatcherServlet贯穿始终,并将所有的组件串联起来 
 
    DispatcherServlet —— 串联起整个逻辑主线,是整个框架的心脏
    组件 —— 逻辑处理单元的程序化表示,起到承上启下的作用,是SpringMVC行为模式的实际承载者
 
初始化主线
DispatcherServlet负责对容器(WebApplicationContext)进行初始化。
容器(WebApplicationContext)将读取SpringMVC的核心配置文件进行组件的实例化。

    初始化组件
    DispatcherServlet从容器(WebApplicationContext)中读取组件的实现类,并缓存于DispatcherServlet内部
 

 

posted @   JillWen  阅读(456)  评论(0编辑  收藏  举报
编辑推荐:
· 从二进制到误差:逐行拆解C语言浮点运算中的4008175468544之谜
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
阅读排行:
· C# 13 中的新增功能实操
· Supergateway:MCP服务器的远程调试与集成工具
· Vue3封装支持Base64导出的电子签名组件
· 万字长文详解Text-to-SQL
· Ollama本地部署大模型总结
点击右上角即可分享
微信分享提示