本来是Struts2的,培训老师用Struts1.也没法了~
废话不多说,心得,过程是关键
Struts1.X 和 2.X 是天差地别的。但是原理是差不多的~学起来不累。
此次是用1.2
Struts基本流程--借用下官方的图。
以前总是在Action类里面写业务逻辑,初始化等。(现在想想。其实还是“初始化”拿对象,只是,拿已经初始化过的,放在Spring IoC容器中的对象)现在要考虑的就是,该怎样把原先那些手动初始化的类交给Spring容器。
现在看下Spring的 IoC 是如何工作的,或者说,Struts 中的对象是怎样托管给Spring IoC容器的。
一:明确目的
IoC 是用来 -- 初始化对象
那些在容器启动时就初始化的对象
在Spring中配置来告诉Spring 的 IoC这些对象
二:基本配置
Spring部分:
<bean id="iuserService" class="com.sms.service.ipml.UserServiceImpl"> </bean> <bean name="/user" class="com.sms.struts.action.UserAction"> <property name="iuserService"> //以下这个bean=".."必须和上面配置的 <bean id=".." class=".."></bean>一致,否者会出错,红色标出 <ref bean="iuserService" /> </property> </bean> public class UserAction extends Action { private IUserService iuserService; public IUserService getIuserService() { return iuserService; } public void setIuserService(IUserService iuserService) { this.iuserService = iuserService; } public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { UserForm userForm = (UserForm) form; Hashtable<String, Object> temp = new Hashtable<String, Object>(); // temp = this.view(temp, userForm); temp = this.iuserService.view(request, response, temp, userForm); if (temp != null) { request.setAttribute("temp", temp); return mapping.findForward(temp.get("url").toString().trim()); } return null; } //其他代码段
好吧,我承认,自己看这段配置,就知道,这是一个完全的新手写的代码,重写一遍肯定不会这样的~~~!!!
如果你没看出来,那就Happy了~~~~我们一起加油吧~
如果哪位阅读过这篇劣日志的前辈,劳烦您指点下。看看我想的对不对。。
*********************************************************************************************************************************************************以下是文档中的一个示例配置:
//hey Spring 。 我需要一些帮手,你能帮我准备些人手不?我告诉我要的帮手的 id 。。。。 <bean id="exampleBean" class="examples.ExampleBean"> <!-- setter injection using the nested <ref/> element --> <property name="beanOne"><ref bean="anotherExampleBean"/></property> <!-- setter injection using the neater 'ref' attribute --> <property name="beanTwo" ref="yetAnotherBean"/> <property name="integerProperty" value="1"/> </bean>//bean 指定一个对象,hey Spring,帮我初始化(配备好)这些人手吧~~~我告诉你地址。<bean id="anotherExampleBean" class="examples.AnotherBean"/>
<bean id="yetAnotherBean" class="examples.YetAnotherBean"/>
public class ExampleBean {
private AnotherBean beanOne;
private YetAnotherBean beanTwo;
private int i;
public void setBeanOne(AnotherBean beanOne) {
this.beanOne = beanOne;
}
public void setBeanTwo(YetAnotherBean beanTwo) {
this.beanTwo = beanTwo;
}
public void setIntegerProperty(int i) {
this.i = i;
}
}The
idref
elementThe
idref
element is simply an error-proof way to pass the id (string value - not a reference) of another bean in the container to a<constructor-arg/>
or<property/>
element.<bean id="theTargetBean" class="..."/> <bean id="theClientBean" class="..."> <property name="targetName"> <idref bean="theTargetBean" /> </property> </bean>The above bean definition snippet is exactly equivalent (at runtime) to the following snippet:
<bean id="theTargetBean" class="..." /> <bean id="client" class="..."> <property name="targetName" value="theTargetBean" /> </bean>
总结。。为什么名为:“依赖注入”??(也是在写这篇日志的时候,才稍微有点领会的)
凡是<bean>配置的,都会被Spring的IoC容器初始化,
我们知道,类中经常用到其他对象,
例如:
public class Test { public static void main(String[] args) { FunctionClass fc = new FunctionClass(); System.out.println(fc.ConvertName("amy")); } } public class FunctionClass { private String name; public String ConvertName(String str) { name = "jake"; str = str + name; return str; } }想要在容器启动时初始化Test类,Test类中有FunctionClass ,必须先初始化FunctionClass。
而这时,不是Test类自己去初始化,而是Spring的配置文件注入给它。
<bean id="theTargetBean" class="..."/> <bean id="theClientBean" class="..."> <property name="targetName"> <idref bean="theTargetBean" /> </property> </bean>这样,无 property 属性的,就是不需要注入的,可以直接初始化的。有property的就是需要依赖Spring给它注入一个类,才能初始化的。
问题1:其实,就像无需依赖注入的类,指定路径就可以初始化,那么即使类中有其他类(依赖),如果获得了这个依赖类的地址,就照样也能初始化了?没有import的说明在同包,有的就可以获得包名了。技术上应该可以实现。
问题2:如果这样,需要依赖的类中初始化了一次依赖类,依赖类本身又初始化了一次自己,Spring IoC是管理单例的呀~~
不钻牛角了。
Struts 部分的配置:
struts-config.xml
<action name="userForm" path="/user" scope="request" <!-- 原先是告诉request跳转到哪个Action的,现在给一个授权的代理类 --> type="org.springframework.web.struts.DelegatingActionProxy" validate="false"> <forward name="success" path="/admin/user/usersuccess.jsp" /> <forward name="nosuccess" path="/admin/user/usersave.jsp" /> </action> <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"> <set-property value="/WEB-INF/applicationContext.xml" property="contextConfigLocation"/> </plug-in> //授权代理,通过这个user来确定要去的Action?这个真的不确定呀。等会试验下。 <bean name="/user" class="com.sms.struts.action.UserAction"> //依赖的类 <property name="iuserService"> <ref bean="iuserService" /> </property> </bean>只贴这两个示例性的示例,
三:struts 和 Spring 的整合
这样,就完成了Spring和Struts的整合
public class UserAction extends Action {
//这个iuserService是个接口哦 private IUserService iuserService; public IUserService getIuserService() { return iuserService; }
//实例化它的是配置文件---制定的类。没有程序去执行这个new iuserService 和 setClass 方法。都是Spring IoC容器做的事 public void setIuserService(IUserService iuserService) { this.iuserService = iuserService; } public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { UserForm userForm = (UserForm) form; Hashtable<String, Object> temp = new Hashtable<String, Object>(); // temp = this.view(temp, userForm); temp = this.iuserService.view(request, response, temp, userForm); if (temp != null) { request.setAttribute("temp", temp); return mapping.findForward(temp.get("url").toString().trim()); } return null; }
这样,就完成了Spring 和 Struts1.2 的最基础的整合。。
第一次写技术心得日志。感觉很好。 温故而知新~~
以后坚持每一天有空就写写