当前位置: 首页 > news >正文

多用户网站源码阿里云虚拟主机建站教程

多用户网站源码,阿里云虚拟主机建站教程,阳江保安招聘网,武陟住房和城乡建设局网站我们从两个角度研究EnableWebMvc#xff1a; EnableWebMvc的使用EnableWebMvc的底层原理 EnableWebMvc的使用 EnableWebMvc需要和java配置类结合起来才能生效#xff0c;其实Spring有好多Enablexxxx的注解#xff0c;其生效方式都一样#xff0c;通过和Configuration结合…我们从两个角度研究EnableWebMvc EnableWebMvc的使用EnableWebMvc的底层原理 EnableWebMvc的使用 EnableWebMvc需要和java配置类结合起来才能生效其实Spring有好多Enablexxxx的注解其生效方式都一样通过和Configuration结合、使得Enablexxxx中的配置类大多通过Bean注解注入到Spring IoC容器中。 理解这一配置原则EnableWebMvc的使用其实非常简单。 我们还是使用前面文章的案例进行配置。 新增配置类 在org.example.configuration包下新增一个配置类 Configuration EnableWebMvc ComponentScan({org.example.controller}) public class MvcConfiguration{Overridepublic void extendMessageConverters(ListHttpMessageConverter? converters) {for(HttpMessageConverter httpMessageConverter:converters){if(StringHttpMessageConverter.class.isAssignableFrom(httpMessageConverter.getClass())){((StringHttpMessageConverter)httpMessageConverter).setDefaultCharset(Charset.forName(UTF-8));}}} }配置类增加controller的包扫描路径添加EnableWebMvc注解其他不需要干啥。 简化web.xml 由于使用了EnableWebMvc所以web.xml可以简化只需要启动Spring IoC容器、添加DispatcherServlet配置即可 ?xml version1.0 encodingUTF-8? web-app xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlnshttp://xmlns.jcp.org/xml/ns/javaeexsi:schemaLocationhttp://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsdidWebApp_ID version3.1 !-- 1、启动Spring的容器--context-paramparam-namecontextConfigLocation/param-nameparam-valueclasspath:applicationContext.xml/param-value/context-paramlistenerlistener-classorg.springframework.web.context.ContextLoaderListener/listener-class/listenerservletservlet-namedispatcherServlet/servlet-nameservlet-classorg.springframework.web.servlet.DispatcherServlet/servlet-classinit-paramparam-namecontextConfigLocation/param-nameparam-valueclasspath:springmvc.xml/param-value/init-paramload-on-startup1/load-on-startup/servletservlet-mappingservlet-namedispatcherServlet/servlet-nameurl-pattern//url-pattern/servlet-mapping/web-appapplicationContext.xml Spring IoC容器的配置文件指定包扫描路径即可 ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxmlns:aophttp://www.springframework.org/schema/aopxmlns:txhttp://www.springframework.org/schema/txxsi:schemaLocationhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsdcontext:component-scan base-packageorg.examplecontext:exclude-filter typeannotationexpressionorg.springframework.stereotype.Controller //context:component-scan /beansSpringmvc.xml springmvc.xml文件也可以简化只包含一个视图解析器及静态资源解析的配置即可其他的都交给EnableWebMvc即可 ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:mvchttp://www.springframework.org/schema/mvcxmlns:contexthttp://www.springframework.org/schema/contextxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd!-- 放行静态资源 --mvc:default-servlet-handler /!-- 视图解析器 --bean idviewResolver classorg.springframework.web.servlet.view.InternalResourceViewResolver!-- 视图前缀 --property nameprefix value/ /!-- 视图后缀 --property namesuffix value.jsp //bean/beans测试 添加一个controller Controller public class HelloWorldController {GetMapping(value/hello)ResponseBodypublic String hello(ModelAndView model){return h1EnableWebMvc 你好/h1;} }启动应用测试 发现有中文乱码。 解决中文乱码 参考上一篇文章改造一下MvcConfiguration配置文件实现WebMvcConfigurer接口、重写其extendMessageConverters方法 Configuration EnableWebMvc ComponentScan({org.example.controller}) public class MvcConfiguration implements WebMvcConfigurer{public MvcConfiguration(){System.out.println(mvc configuration constructor...);} // 通过EnableWebMVC配置的时候起作用Overridepublic void extendMessageConverters(ListHttpMessageConverter? converters) {for(HttpMessageConverter httpMessageConverter:converters){if(StringHttpMessageConverter.class.isAssignableFrom(httpMessageConverter.getClass())){((StringHttpMessageConverter)httpMessageConverter).setDefaultCharset(Charset.forName(UTF-8));}}}}重启系统测试 中文乱码问题已解决。 问题EnableWebMvc的作用 上述案例已经可以正常运行可我们可以看到web.xml、applicationContext.xml以及springmvc.xml等配置文件都还在一个都没少。 那么EnableWebMvc究竟起什么作用 我们去掉EnableWebMvc配置文件试试看项目中删掉MvcConfiguration文件。 重新启动项目访问localhost:8080/hello报404 回忆一下MvcConfiguration文件中定义了controller的包扫描路径现在MvcConfiguration文件被我们直接删掉了controller的包扫描路径需要以其他方式定义我们重新修改springmvc.xml文件把controller包扫描路径加回来。 同时我们需要把SpringMVC的注解驱动配置加回来 !-- 扫描包 --context:component-scan base-packageorg.example.controller/mvc:annotation-driven /以上两行加入到springmvc.xml配置文件中重新启动应用 应用可以正常访问了中文乱码问题请参考上一篇文章此处忽略。 因此我们是否可以猜测EnableWebMvc起到的作用等同于配置文件中的 mvc:annotation-driven / ? EnableWebMvc的底层原理 其实Spring的所有Enablexxx注解的实现原理基本一致和Configuration注解结合、通过Import注解引入其他配置类从而实现向Spring IoC容器注入Bean。 EnableWebMvc也不例外。 Retention(RetentionPolicy.RUNTIME) Target(ElementType.TYPE) Documented Import(DelegatingWebMvcConfiguration.class) public interface EnableWebMvc { }EnableWebMvc引入了DelegatingWebMvcConfiguration类。看一眼DelegatingWebMvcConfiguration类肯定也加了Configuration注解的 Configuration(proxyBeanMethods false) public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {...DelegatingWebMvcConfiguration类扩展自WebMvcConfigurationSupport其实DelegatingWebMvcConfiguration并没有创建bean、实际创建bean的是他的父类WebMvcConfigurationSupport。 WebMvcConfigurationSupport按顺序注册如下HandlerMappings: RequestMappingHandlerMapping ordered at 0 for mapping requests to annotated controller methods.HandlerMapping ordered at 1 to map URL paths directly to view names.BeanNameUrlHandlerMapping ordered at 2 to map URL paths to controller bean names.HandlerMapping ordered at Integer.MAX_VALUE-1 to serve static resource requests.HandlerMapping ordered at Integer.MAX_VALUE to forward requests to the default servlet. 并注册了如下HandlerAdapters: RequestMappingHandlerAdapter for processing requests with annotated controller methods.HttpRequestHandlerAdapter for processing requests with HttpRequestHandlers.SimpleControllerHandlerAdapter for processing requests with interface-based Controllers. 注册了如下异常处理器HandlerExceptionResolverComposite ExceptionHandlerExceptionResolver for handling exceptions through org.springframework.web.bind.annotation.ExceptionHandler methods.ResponseStatusExceptionResolver for exceptions annotated with org.springframework.web.bind.annotation.ResponseStatus.DefaultHandlerExceptionResolver for resolving known Spring exception types 以及 Registers an AntPathMatcher and a UrlPathHelper to be used by: the RequestMappingHandlerMapping,the HandlerMapping for ViewControllersand the HandlerMapping for serving resources Note that those beans can be configured with a PathMatchConfigurer. Both the RequestMappingHandlerAdapter and the ExceptionHandlerExceptionResolver are configured with default instances of the following by default: a ContentNegotiationManagera DefaultFormattingConversionServicean org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean if a JSR-303 implementation is available on the classpatha range of HttpMessageConverters depending on the third-party libraries available on the classpath. 因此EnableWebMvc确实与 mvc:annotation-driven / 起到了类似的作用注册SpringWebMVC所需要的各种特殊类型的bean到Spring容器中以便在DispatcherServlet初始化及处理请求的过程中生效 上一篇 Spring MVC 十一中文乱码
http://www.yutouwan.com/news/436896/

相关文章:

  • 网站开发需要提供哪些资料wordpress主题 洛米
  • 网站制作与app开发哪个要难一点策划公司网站
  • vue做的网站有什么wordpress海报功能
  • 网页设计实训报告保利集团吐鲁番seo快速排名
  • 辽宁省城乡和建设厅网站东营信息网官网
  • 什么做直播网站阳江网红人物
  • 石家庄怎样做网站个人企业注册信息查询
  • 网络科技公司网站模板一个网站源代码概多大
  • 网站开发 设计文档网站内页seo查询
  • 成品网站怎样建设网站做产品的审核工作
  • 杭州 网站开发公司广州科技网络公司排名
  • 域名有了怎么建设网站贵州遵义新闻
  • 网站写动态新闻有什么好处个人网页制作教程代码
  • 在青岛做阿里巴巴网站找谁0531建设网站
  • sz住房和城乡建设部网站专业商城网站建设报价单
  • 有什么网站做交流会上海网络开发公司
  • jsp类型网站托管费用flash网站模版
  • 船员专用网站开发建议开发公司网签合同条件
  • 漳州网站开发制作南京专业做网站公司地址
  • 低价网站建设优化公司锐捷网络公司排名
  • 吉安市城乡规划建设局网站电商网站 建设
  • 百度微信官网网站模板商业计划书ppt模板免费下载
  • 网站开发笔试题wordpress js代码插件下载
  • 网站写好了怎么做后台管理wordpress 判断语句
  • 深圳住房和建设局网站网页qq登陆官网
  • 深圳网络专科网站建设哪家能建设网站
  • 企业门户网站的设计 流程图wordpress call to un
  • 怎样用dw做网站导航条网站建设浙江公司
  • 如何购买网站服务器网站服务器错误
  • 陕煤建设集团网站最近国语视频在线观看免费播放