郭文涛's profile郭文涛的N度空间BlogListsNetwork Tools Help

Blog


    July 12

    struts2与spring结合

    转载于在webwork的文章
    1 配置struts.properties文件,指定spring作为struts的IoC容器
    struts.objectFactory = spring
    (1)默认的autowiring模式是:by name
    即如果applicationContext.xml文件中的bean id与struts.xml文件中的action name相同,就
    (2)如果要改为其他模式:
    struts.objectFactory.spring.autoWire = name|type|auto|constructor
    2 配置web.xml文件,启动Spring侦听器
    <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    3 在WEB-INF目录下增加applicationContext.xml文件
    例:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
    <beans default-autowire="autodetect">
    <bean id="personManager" class="com.acme.PersonManager"/>
    ...
    </beans>
    4 设置由Spring来初始化action
    4.1  在Spring的applicationContext.xml文件中配置bean(即action类)
    4.2  将struts.xml中的action的class属性,由class名改为Spring中定义的bean名
    例如:
    (1)applicationContext.xml中,定义bean id是bar
    <beans default-autowire="autodetect">
    <bean id="bar" class="com.my.BarClass" singleton="false"/>
    ...
    </beans>
    (2)struts.xml中,action的class="bar",而不是通常的类名
    <package name="secure" namespace="/secure" extends="default">
    <action name="bar" class="bar">
    <result>bar.ftl</result>
    </action>
    </package>

    Comments

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    Trackbacks (1)

    The trackback URL for this entry is:
    http://gwt521600.spaces.live.com/blog/cns!992608FFAAD6787D!178.trak
    Weblogs that reference this entry