免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 930 | 回复: 0
打印 上一主题 下一主题

第一个struts+spring的例子 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-09-19 13:11 |只看该作者 |倒序浏览
呵呵刚学spring 写还望各位大虾指教!
配置struts就不用说了吧,如果用myEclipse直接add一下就可以了.接下来就来配置让它支持spring 可
一,新建项目后,打开struts-confing.xml在后面加人下面代码
   
        
   
简单吧!
二然后就开始写我们的bean 了,老土点吧程序还是实现say hello
1.User  bean
package com.yourcompany.struts.model;
public class User {
public String sayHello(String str)
{
return "hello,"+str;
}
}

2.控制器
private User user=null;
public void setUser(User user)
{
  this.user=user;
}

    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response)
throws Exception {
      DynaActionForm userForm = (DynaActionForm) form;
  String suss=user.sayHello((String)userForm.get("userName"));
  request.setAttribute("hello",suss);
  return mapping.findForward("sayHello");
}
动态formbea的配置我另一骗文章里有,大家注意黑体部门。这就是spring 的依赖注入了.
咱们先看看配置文件
   
   
        
   
        
   
首先定义的是 userAction它有个user的属性,而user属性又依赖 下面定义的user  bean;.,还有一点就是
在struts-config里更改userAction的type属性让它由spring来控制.

这样spring就会在运行的时候装载各个bean并对相应的bean初始化了
爽吧
最后表现层
    This a struts page.
   
   
   
   
   
   
   
   
  

完了,总结一下spring这样的做法的好处,在运行的时候通过配置文件把关系组装起来,更改关系的时候更
改配置文件就好了,而且struts的action由spring 来能减少控制层的代码,层次更清晰还有.....呵呵想不出来了。继续努力.....


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/45990/showart_385265.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP