1、首先创建父容器(AnnotationConfigWebApplicationContext),通过自定义的getRootConfigClasses()拿到配置类,并注册到父容器中。
2、通过父容器作为参数创建ContextLoaderListener监听器。并添加到servletContext(Tomcat servlet容器)。
3、通过自定的getServletConfigClasses()方法拿到MVC的配置类创建子容器,并把配置类注册到子容器中。
4、创建DispatcherServlet,并将DispatcherServlet添加到servletContext的Servlet容器中去。
5、设置dispatcherServlet相关属性(启动时加载,设置映射路径)。
Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。
Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块。使用 Spring 可插入的 MVC 架构,可以选择是使用内置的 Spring Web 框架还是 Struts 这样的 Web 框架。通过策略接口,Spring 框架是高度可配置的,而且包含多种视图技术。
Spring MVC 分离了控制器、模型对象、分派器以及处理程序对象的角色,这种分离让它们更容易进行定制。
1、@requestBody注解常用来处理content-type不是默认的application/x-www-form-urlcoded编码的内容,比如说:application/json或者是application/xml等。一般情况下来说常用其来处理application/json类型。
2、通过@requestBody可以将请求体中的JSON字符串绑定到相应的bean上,当然也可以将其分别绑定到对应的字符串上。
例如说以下情况:
$.ajax({
url:"/login",
type:"POST",
data:'{"userName":"admin","pwd","admin123"}',
content-type:"application/json charset=utf-8",
success:function(data)
{
alert("request success ! ");
}
});
@requestMapping("/login")
public void login(@requestBody String userName,@requestBody String pwd){
System.out.println(userName+" :"+pwd);
}
这种情况是将JSON字符串中的两个变量的值分别赋予了两个字符串,但是呢假如我有一个User类,拥有如下字段: String userName; String pwd; 那么上述参数可以改为以下形式:@requestBody User user 这种形式会将JSON字符串中的值赋予user中对应的属性上 需要注意的是,JSON字符串中的key必须对应user中的属性名,否则是请求不过去的。
3、在一些特殊情况@requestBody也可以用来处理content-type类型为application/x-www-form-urlcoded的内容,只不过这种方式不是很常用,在处理这类请求的时候,@requestBody会将处理结果放到一个MultiValueMap<String,String>中,这种情况一般在特殊情况下才会使用,例如jQuery easyUI的datagrid请求数据的时候需要使用到这种方式、小型项目只创建一个POJO类的话也可以使用这种接受方式。
作用:
i) 该注解用于读取Request请求的body部分数据,使用系统默认配置的HttpMessageConverter进行解析,然后把相应的数据绑定到要返回的对象上;
ii) 再把HttpMessageConverter返回的对象数据绑定到 controller中方法的参数上。
原因有: spss软件包有问题,可以重新下一个确定好用的再安装 跟操作系统位数要一致 打开中文破解版安装包,然后根据用户自己的操作系统进行相应的安装包的选择
SpringMVC运行原理
1. 客户端请求提交到DispatcherServlet
2. 由DispatcherServlet控制器查询一个或多个HandlerMapping,找到处理请求的Controller
3. DispatcherServlet将请求提交到Controller
4. Controller调用业务逻辑处理后,返回ModelAndView
5. DispatcherServlet查询一个或多个ViewResoler视图解析器,找到ModelAndView指定的视图
6. 视图负责将结果显示到客户端
Java Spring MVC (Model-View-Controller) is a powerful framework that provides developers with the tools they need to build robust and scalable web applications. With its modular architecture and extensive libraries, Spring MVC has become one of the most popular choices for Java developers.
Java Spring MVC is an open-source framework that follows the Model-View-Controller architectural pattern. It helps developers build web applications that are loosely coupled and easy to test and maintain. The framework provides a set of classes and annotations that streamline the development process and promote best practices.
To get started with Java Spring MVC, developers need to set up a Spring MVC project and configure the necessary dependencies. They can then start writing controllers, views, and models to build their application. The Spring MVC framework provides a rich set of tools and libraries that developers can leverage to simplify their development process.
There are several benefits to using Java Spring MVC for web application development:
Java Spring MVC is a powerful framework for building robust and scalable web applications. With its modular architecture, extensive libraries, and strong community support, Spring MVC has become a popular choice for Java developers. Whether you are building a small application or a complex enterprise system, Spring MVC provides the tools and features you need to get the job done.
Thank you for reading this article. We hope it has provided you with a comprehensive overview of Java Spring MVC and its benefits. By leveraging the power of Spring MVC, you can build web applications that are maintainable, testable, and scalable.
spring mvc的容器是ioc,而ioc是spring core的组件,所以说spring core是spring mvc的核心容器。
说到 Spring,也许现在的开发者们最先想到的是 Josh Long。他凭借超快的语速与现场代码能力,让很多 Java 开发者折服。然而 Spring 的历史上最传奇的应该是其创始人:Rod Johnson!
Spring MVC是一种基于Java的Web应用框架,它的原理是基于MVC(Model-View-Controller)设计模式。MVC是一种常用的Web应用程序架构,它将Web应用程序分为三个组件:模型(Model)、视图(View)和控制器(Controller)。
Spring MVC的原理可以概括为以下几个步骤:
1. 用户发送请求:首先,用户在浏览器中输入URL,提交请求给服务器。
2. DispatcherServlet接收请求:DispatcherServlet是Spring MVC框架的核心组件,它负责接收所有的请求,并将请求委托给其他组件处理。
3. HandlerMapping处理器映射:HandlerMapping根据请求的URL路径,找到可以处理该请求的处理器(Controller)。
4. HandlerAdapter处理器适配器:HandlerAdapter将请求传递给处理器(Controller),并将处理器的响应返回给DispatcherServlet。
5. 视图解析器:视图解析器将处理器的响应解析为视图(View)。
6. 视图渲染器:视图渲染器将视图转换为HTML代码,并将其发送回浏览器,呈现给用户。
在Spring MVC框架中,Handler(控制器)是处理请求的核心组件,它通过注解或配置文件的方式将请求映射到相应的处理方法上。同时,Spring MVC还提供了丰富的视图解析器(ViewResolver),可以根据请求的类型(如HTML、JSON等)选择相应的视图渲染器进行渲染。
总之,Spring MVC框架采用MVC设计模式,通过DispatcherServlet、HandlerMapping、HandlerAdapter、视图解析器和视图渲染器等核心组件,实现了对Web应用程序的请求处理、响应渲染等功能。
参考如下内容:Spring MVC的Controller用的是Servlet的思想,单例性能好,但线程不安全,如果用其它的Scope,性能会下降。建议用默认单例方式,实现要共享对象属性,可以用ThreadLocal保护。
controller默认是单例的,不要使用非静态的成员变量(service无所谓,因为它不会变),否则会发生数据逻辑混乱。
比如a线程将int i=3,b线程将 i = 4,然后a再访问 i 时, i的值为4