ssh

来源:互联网 发布:淘宝哪家银店真的吗 编辑:程序博客网 时间:2024/06/06 04:28

spring容器的测试:

ApplicationContext  ac = new  ClassPathXmlApplicationContext("bean.xml");

EmployeeService  es = (EmployeeService)ac.getBean("employeeService");

看是否得到es对象


一、action是由struts和spring插件生成:
如果spring配置了注解
1、struts2的action由struts2和spring的plugIn插件生成,然后再根据spring的配置文件注入对象。
所以action前面就不要加@Component注解了,加了的话就相当于spring创建了该action对象,从而使得spring配置文件比正确的配置多了个action。
而servlet和dao就需要@Component,因为他们是相当于由spring创建的。
2、action中@Resource注入可以放在属性上也可以不写,但是放在set方法上不会起作用。
3、当action中不应用任何注解时,默认会按名字来注入
二、action是由spring生成:
action中必须写上@Component、@prototype注解


openSessionInView解决了懒加载对象在页面读取对象属性时无session的问题。
web.xml中openSessionInView的过滤器配置:应该放在struts2的过滤器前面:
并且在filter中需要指定sessionFactoryBeanName的值(某个什么一样的情况除外)
    <filter>
        <filter-name>openSessionInView</filter-name>
        <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
        <init-param>
            <param-name>sessionFactoryBeanName</param-name>
            <param-value>sf</param-value>
        </init-param>
    </filter>
    
    <filter-mapping>
        <filter-name>openSessionInView</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
    openSessionInView的filter在拦截到一个方法时,如果该方法没有配置事物,
    那么openSessionInView会自动认为该方法的事物是只读的,此时如果该方法会持久化数据就会报错的!
      
    
    中文乱码问题:spring.jar文件中已经定义了乱码过滤器,只要拿过来用就可以了:
    在web.xml如下配置:
    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>GBK</param-value>
        </init-param>
    </filter>
    
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>