- 论坛徽章:
- 0
|
呵呵刚学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 |
|