Spring实例Action的两种方式:
1、DelegatingActionProxy类整个Action由Spring实例
2、DelegatingRequestProcessor类extends RequestProcessor重写processActionCreate方法,由Spring创建
web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
SSH整合第一式:缺点:麻烦一个方法对应一个<bean>如何实现整合的:
重写了Action类的execute方法,使用Spring容器来实例bean,查找bean的名字是<action path="">的值
①struts-config.xml
<action ……
path="/doSave"
type="org.springframework.web.struts.DelegatingActionProxy">
</action>
②applicationContext.xml
<bean name="/doSave" class="com.yourcompany.struts.action.UserinfoAction">
<property name="manager" ref="userInfoManager"></property>
</bean>
注解:name="/doSave" 取得时<action path="/doSave" />
SSH整合第二式:第一式的延伸,使用MyDelegatingActionProxy类继承DelegatingActionProxy和DispatchAction类
缺点:实现起来太繁琐;优点:一个Action类对应一个<bean>
①struts-config.xml
<action ……
type="com.yourcompany.struts.action.UserinfoAction">
</action>
②applicationContext.xml
<bean name="com.yourcompany.struts.action.UserinfoAction" class="com.yourcompany.struts.action.UserinfoAction">
<property name="manager" ref="userInfoManager"></property>
</bean>
③Action extends MyDelegatingActionProxy
注解:
1、如果不继承DispatchAction类的话就会产生死循环调用execute方法的问题。
DelegatingActionProxy类的作用:改变Action类的创建方式,改成由Spring容器创建的
DispatchAction类的作用:把execute方法分解成多个普通方法执行
2、屏蔽死循环问题必须在MyDelegatingActionProxy类重写execute方法一下是此类的关键代码:
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
Action delegateAction = getDelegateAction(mapping);
String parameter = getParameter(mapping, form, request, response);
String name = getMethodName(mapping, form, request, response, parameter);
return dispatchMethod(mapping, form, request, response, name,
delegateAction);
}
SSH整合第三式:使用DelegatingRequestProcessor类
缺点:一个方法一个<bean>
①struts-config.xml
<action ……
type="com.yourcompany.struts.action.UserinfoAction">
</action>
<controller>
<set-property property="processorClass" value="org.springframework.web.struts.DelegatingRequestProcessor"/>
</controller>
②applicationContext.xml
<bean name="/doSave" class="com.yourcompany.struts.action.UserinfoAction">
<property name="manager" ref="userInfoManager"></property>
</bean>
注解:name="/doSave" 取得时<action path="/doSave" />
SSH整合第四式:自定义MyDelegatingRequestProcessor类extends DelegatingRequestProcessor,重写determineActionBeanName方法,return mapping.getType();
优点:一个Action对应一个<bean>,[推荐,常用]
①struts-config.xml
<action ……
type="com.yourcompany.struts.action.UserinfoAction">
</action>
<controller>
<set-property property="processorClass" value="MyDelegatingRequestProcessor类路径"/>
</controller>
②applicationContext.xml
<bean name="com.yourcompany.struts.action.UserinfoAction" class="com.yourcompany.struts.action.UserinfoAction">
<property name="manager" ref="userInfoManager"></property>
</bean>
SSH整合第五式:继承ActionSupport、DispatchActionSupport、MappingDispatchActionSupport
①struts-config.xml
<action ……
type="com.yourcompany.struts.action.UserinfoAction">
</action>
②applicationContext.xml
<bean name="com.yourcompany.struts.action.UserinfoAction" class="com.yourcompany.struts.action.UserinfoAction" />
<bean id="userinfoManager" class="com.yourcompany.struts.action.UserinfoAction" />
注解:UserInfoAction extends MappingDispatchActionSupport实现onInit方法
manager=(IUserInfoManager) getWebApplicationContext().getBean("userInfoManager");
不需要set方法
问1:这段代码在什么时候执行,起什么作用?
<controller>
<set-property property="processorClass" value="org.springframework.web.struts.DelegatingActionProxy"/>
</controller>
答:在ActionServlet类的process方法中
RequestProcessor processor = getProcessorForModule(config);读取返回一个RequestProcessor对象,如果没有配置的话就会采取默认的,也就是Struts的核心类RequestProcessor
问2:ContextLoaderPlugIn类的作用,如何使用?
答:在struts-config.xml中配置
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLication" value="/WEB-INF/classes/applicationContext.xml"/>
</plug-in>
相当于再web.xml中配置
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>
道理是相同的。
一般是配置在web.xml中。