SpringWeb

系列文章目录

废话不多说,直接进入正题。


一、JavaEE项目架构

项目架构一般分为以下4层:

  • 表示层:servlet–> 针对VO类的封装操作
  • 业务层:service–> 针对VO(ViewObject)类的封装操作,内部要实现VO与PO的相互转换
  • 持久层:dao --> 针对PO类的封装操作–>实体类(Entity)、模型类(Model)、持久化类(PO)
  • 数据层:mysql等数据库封装

二、如何在Web应用中使用Spring管理Bean

首先搞明白底层原理:Spring无法管理JSP和Servlet(以为这俩个都依赖servlet容器)。所以无法用Spring直接注入依赖的Bean。
解决途径:需要在Servlet和JSP中找到Spring的容器

操作步骤:

  1. 添加Spring提供的Web应用支持包:例如名如:spring-web-x-x-x.Release.jar,spring-tx-x-x-x.Release.jar
  2. 在Web应用程序中的web.xml中配置Spring提供的一个叫ContextLoaderListener的监听器,它会在应用程序部署完成后自动创建一个WebApplicationContext实例并存放到ServletContext容器中。
    示例:
<!-- 使用应用上下文参数来指定spring的配置文件 -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 配置初始化Spring应用上下文的监听器:  这个监听器会在本web应用部署成功后得到执行 
     1) 执行时先获取应用上下文参数contextC onfigLocation指定的配置文件,创建出WebA pplicationC ontext实例.      
     2) 把这个WebA pplication Context实例放置到ServletContext中。
     (WebA pplicationC ontext单例)-->
<listener>    
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
  1. 在Servlet或JSP中可以使用如下代码获取WebApplicationContext实例:
WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
  1. 这样你就可以Spring应用上下文中获取你想要的Bean。

三、Spring针对web应用还提供了一个工具类

org.springframework.web.filter.CharacterEncodingFilter:设置请求消息体中的编码方式的一个过滤器

配置如下:

<filter>
    <filter-name>charaterEncodeing</filter-name>    
    <filter-class>org.springframework.web.filter.CharacterE ncodingF ilter</filter-class>    
    <init-param>
            <param-name>encoding</param-name>        
            <param-value>UTF-8</param-value>    
      </init-param>    
      <init-param>
              <param-name>forceRequestEncoding</param-name>        
              <param-value>true</param-value>    
       </init-param>
</filter>

<filter-mapping>
    <filter-name>charaterEncodeing</filter-name>    
    <url-pattern>/*</url-pattern>
</filter-mapping>

org.springframework.web.util.IntrospectorCleanupListener:Spring刷新Introspector防止内存泄露的监听器

配置如下:

<listener>
    <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
 </listener>

四、Spring提供的常用工作类

  1. 字符串:org.springframework.util.StringUtils
    有用方法:isEmpty()、hasText()、getFilename()、getFilenameExtension()
  2. 数字:org.springframework.util.NumberUtils
  3. 对象:org.springframework.util.ObjectUtils
  4. 集合:org.springframework.util.CollectionUtils
    boolean isEmpty(Collection)
    List arrayToList(Object source)
  5. 文件和目录复制:org.springframework.util.FileSystemUtils
  6. 文件内容复制:org.springframework.util.FileCopyUtils
  7. JavaBean工具:org.springframework.beans.BeanUtils
    实例化Bean:T instantiateClass(Class clazz)
    复制Bean的属性:void copyProperties(Object source, Object target)等
  8. 请求工具类:org.springframework.web.bind.ServletRequestUtils
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值