Spring web 应用程序
来源:互联网 发布:windows平板系统下载 编辑:程序博客网 时间:2024/05/18 02:32
这一节内容是学习spring web开发的一部分,在接下来的时间会陆续补充spring工作原理的相关章节。
本章内容
- 映射请求到spring控制器
- 透明地绑定表单参数
- 校验表单提交
1,spring MVC基础
spring MVC工作的基本思路是将请求在调度Servlet、处理器映射(handler mapping)、控制器以及视图解析器(view resolver)之间移动。
- 请求离开浏览器。DispatcherServlet前端控制器根据请求的信息将请求委托给应用程序的其它组件来处理。
- 确定请求的下一站,处理器映射根据请求所携带的URL信息来进行定位。
- DispatcherServlet将请求发送给选中的控制器
- 控制器处理请求,并将模型数据打包,并且标出用于渲染输出的视图名。然后将请求和模型以及视图发送回DispatcherServlet
- DispatcherServlet调用视图解析器将4中返回的逻辑视图名匹配成一个特定的视图实现。
- 返回服务器响应。
2,配置DispatcherServlet
DispatcherServlet的配置分两种:1,在web.xml中配置;2,使用Java配置
在web.xml中配置
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <display-name>winner-test Web Application</display-name> <servlet> <servlet-name>Spring MVC Test</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--配置文件设置--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:my-dispatcher.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Spring MVC Test</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
my-dispatcher.xml的内容如下:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" mlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 配置包扫描器 --> <context:component-scan base-package="app.config"/> <!-- 配置注解驱动 --> <mvc:annotation-driven/> <!-- 视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> </bean></beans>
因为xml文件的维护越来越难,应该尽可能使用Java的方法配置
使用Java配置
package physigapp.config;import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;public class PhysigWebApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { /* * map one or more route to DispatcherServlet * */ @Override protected String[] getServletMappings() { return new String[] {"/"}; } /* * these annotated classes will be used to define ContextLoaderListener beans of application context * */ @Override protected Class<?>[] getRootConfigClasses() { return new Class<?>[] {RootConfig.class}; } /* * these annotated classes will be used to define DispatcherServlet bean of application context * */ @Override protected Class<?>[] getServletConfigClasses() { return new Class<?>[] {WebConfig.class}; }}
当DispatcherServlet启动的时候,它会创建Spring应用上下文,并加载配置文件或配置类中所声明的bean。getServletConfig()方法要求DispatcherServlet加载应用上下文时,使用定义在WebConfig配置类中的bean。但是在Spring Web应用中,通常还有另外一个应用上下文,这个应用上下文有ContextLoaderListener创建。
通常,我们使用DispatcherServlet加载包含Web组件的bean,如控制器、视图解析器和处理器映射,而ContextLoaderListener则负责加载应用中的其它bean,这些bean主要是驱动后端的中间层和数据层组件。
阅读全文
0 0
- Spring web 应用程序
- Spring Junit4 测试Web应用程序
- Spring Junit4 测试Web应用程序
- Spring Junit4 测试Web应用程序
- Spring Junit4 测试Web应用程序
- Spring Junit4 测试Web应用程序
- 初学spring:web应用程序中使用Spring
- spring(5)构建 spring web 应用程序
- 使用Spring MVC构建Web应用程序
- Spring MVC 构建入门级 Web 应用程序
- spring mvc web应用程序框架的搭建
- 使用Spring MVC构建Web应用程序
- 在web应用程序中使用Spring
- 第5章-构建Spring Web应用程序
- 第五章 构建Spring Web应用程序
- Spring Boot 非web应用程序实例
- spring终结篇 -- 使用spring web应用程序配置
- 【Java.Spring.MVC】使用Spring MVC构建Web应用程序
- 2017年7月19日奥地利维也纳之旅
- 实用性强的批处理/DOS指令笔记
- Android:运行时权限(转自网络)
- git 操作之合并commit(提交之前本地合并)
- hdu_1016(剪枝深度优先搜索)
- Spring web 应用程序
- Java基本数据类型
- Android Studio 如何打JAR包
- C语言求1-100的偶数和
- 什么是隔离见证?简单介绍隔离见证
- 弦图(草稿)
- Xmind:夏日也不能忘了健身哦!
- 20170724_父进程和子进程到底是如何执行的?
- tree