struts2-2(和Spring集成)

 

1.    加入Springjar包,并且加入struts2struts2-spring-plugin-2.0.14.jar包。

2.    修改web.xml文件加入

<!-- spring配置 -->
 <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/classes/applicationContext.xml,
            /WEB-INF/classes/daoContext.xml,
            /WEB-INF/classes/actionContext.xml,
        </param-value>
    </context-param>
 <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
 </listener>

 

3. 修改Struts.properties文件,告知Struts 2运行时使用Spring来创建对象(如Action等)。struts.objectFactory = spring

4.修改业务bean,针对spring的借口编程。

public interface IFriendServices {

  public String[] getFriends();

}

public class FriendServices implements IfriendServices…

5.配置applicationContext.xml

<bean id="friendService" class="com.dyp.struts2.services.FriendServices"/>

    <bean id ="friendAction" class ="com.dyp.struts2.action.FriendAction" scope ="prototype">

        <property name ="friendService">

            <ref local ="friendService"/>

        </property>

</bean>

6.Action

    public class FriendAction implements Action {

   

    //通过Spring注入

    private IFriendServices friendService;

    private String[] friends;

   

    public void setFriendService(IFriendServices friendService) {

       this.friendService = friendService;

    }

    ………

7.struts.xml配置转发

<action name="Friend" class="friendAction">

        <!-- 如果处理结果返回login,进入login.jsp页面 -->

        <result name="login">/login.jsp</result>

        <!-- 如果处理结果返回success,进入showBook.jsp页面 -->

        <result name="success">/showFriend.jsp</result>

    </action>

    注意:这里的Action和平常不同的就是class属性,它对应于Spring所定义的beanid,而不是它的类全名。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值