我是靠谱客的博主 活力果汁,最近开发中收集的这篇文章主要介绍spring将service注入到Action中(s1和s2),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

[b][color=red][align=center][size=x-large]spring将service注入到Action中[/size][/align][/color][/b]


[size=x-large][color=green][b]spring与struts1.x集成[/b][/color][/size]
[size=medium][b][color=red]方式一:Action交给spring管理,将业务类注入action[/color][/b][/size]         
引入[b][color=green]spring-webmvc-struts.jar包[/color][/b]到lib下,
struts-config.xml配置:


<action name="StudentForm" path="/StudentList"
parameter="actions"
type="org.springframework.web.struts.DelegatingActionProxy">
<forward name="list" path="/userList.jsp"/>
</action>

 
spring配置applicationContext.xml:

<bean name="/StudentList" class="com.org.momo.action.StudentAction" scope="prototype">
<property name="studentService" ref="studentService" />
</bean>

[color=red]注意:保持spring配置的bean name和struts配置的action path一致[/color]

StudentAction.java:
private StudentService studentService ;	
public StudentService getStudentService() {
return studentService;
}
public void setStudentService(StudentService studentService) {
this.studentService = studentService;
}


注意:加入上面applicationContext.xml里面的action移到applicationContext-action.xml里面时,
1.需在sturts-config.xml配置applicationContext-action.xml加载路径

<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="classpath*:applicationContext-struts.xml" />
</plug-in>
 


[size=medium][color=red][b]方式二:请求首先交给ActionServlet,然后给org.springframework.web.struts.DelegatingRequestProcessor[/b][/color][/size],由这个请求处理器根据请求路径从spring容器获取action对象
 
struts-config.xml:
 
<action path="/searchAction">

<!--注意到没有?这里没有配置type属性-->
<forward name="success" path="/result.jsp"/>
</action>
<controller>
<set-property property="processorClass" value="org.springframework.web.struts.DelegatingRequestProcessor"/>
</controller>


spring配置applicationContext.xml:     
<bean name="/searchAction" class="com.myproject.action.SearchAction" scope="prototype">
<property name="searchService" ref="searchService"/>
</bean>
 
 
      
[size=medium][color=red][b]方式三:Action不交给spring管理,直接在Action中实例化应用上下文[/b][/color][/size],然后通过getBean("xxx"),获取相应业务对象
struts配置struts-config.xml:
<action path="/searchAction"
type="com.myproject.action.SearchAction">

<forward name="success" path="/result.jsp"/>
</action>

<action path="/searchAction" type="com.myproject.action.SearchAction">
<forward name="success" path="/result.jsp"/>
</action>


 
对应的Action处理类:

Actionprivate ApplicationContext context=WebApplicationContextUtils.getWebApplicationContext(this.getServlet().getServletContext());

SearchService searchService=(SearchService)context.getBean("searchService");
private ApplicationContext context=WebApplicationContextUtils.getWebApplicationContext(this.getServlet().getServletContext());
SearchService searchService=(SearchService)context.getBean("searchService");
 
 
  
 
[size=x-large][b][color=green]struts2.x与spring集成[/color][/b][/size] 
struts.properties 配置
struts.objectFactory=spring
#指定struts的action类实例由spring生成
 
struts-config.xml配置       
<action name="search" class="searchAction" method="searchMax">

<result name="success">/result.jsp</result>
<result name="error">/error.jsp</result>
</action>
  
      
spring配置文件applicationContext.xml:                 
<bean id="searchAction" class="com.myproject.action.SearchAction" scope="prototype">

<property name="searchService" ref="searchService"/>
</bean>

<action name="search" class="searchAction" method="searchMax">
<result name="success">/result.jsp</result>
<result name="error">/error.jsp</result>
</action>

最后

以上就是活力果汁为你收集整理的spring将service注入到Action中(s1和s2)的全部内容,希望文章能够帮你解决spring将service注入到Action中(s1和s2)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(40)

评论列表共有 0 条评论

立即
投稿
返回
顶部