iteye_16683 2008-10-15 22:59
浏览 189
已采纳

使用AutowiringRequestProcessor整合struts和spring遇到的问题

如题,小弟今天遇到了一个AutowiringRequestProcessor不能自动完成注入service实例的问题,得到的service是空的,不管使用byName和byType都不行。如果使用ApplicationContext ctx = WebApplicationContextUtils .getRequiredWebApplicationContext(request.getSession()
  .getServletContext());
 pUserExtService=(IPUserExtService)ctx.getBean("pUserExtService");获得service,以下程序能正常运行。现帖出源码,希望各位能帮忙解决,谢谢!

 

web.xml

 


<web-app xmlns="&lt;a href=" http:="" java.sun.com="" xml="" ns="" j2ee"="">http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
   .............

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/*Context*.xml</param-value>
  </context-param>

 ..............

   <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
      <param-name>config</param-name>
      <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
   
    <init-param>
      <param-name>debug</param-name>
      <param-value>3</param-value>
    </init-param>
    <init-param>
      <param-name>detail</param-name>
      <param-value>3</param-value>
    </init-param>
    <load-on-startup>0</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>

 

   .........................


 </web-app>

 

 

RegeditAction.java

 

 public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) {
  RegeditForm regeditForm = (RegeditForm) form;// TODO Auto-generated method stub
  return null;
 }
 
 /**
  * Ajax方法,用于验证用名户是否已被注册
  * @param name
  * @param request
  * @return
  */
 public boolean validateUser(String name,HttpServletRequest request)
 {
//  ApplicationContext ctx = WebApplicationContextUtils
//  .getRequiredWebApplicationContext(request.getSession()
//    .getServletContext());
//  pUserExtService=(IPUserExtService)ctx.getBean("pUserExtService");
 

  DetachedCriteria dc=DetachedCriteria.forClass(PUserExt.class);
  dc.add(Restrictions.eq("account", name));
  List list=this.getPUserExtService().findByDetachedCriteria(dc);
  if(list!=null && list.size()>0)
  {
   return true;
  }
   return false;
 }

 

BaseAction.java

 

public class BaseAction extends Action {
 private static final Log log = LogFactory.getLog(BaseAction.class);
 
 protected IPUserExtService pUserExtService;
 protected ITblNewsService tblNewsService;
 
 public IPUserExtService getPUserExtService() {
  return pUserExtService;
 }
 public ITblNewsService getTblNewsService() {
  return tblNewsService;
 }

 public void setTblNewsService(ITblNewsService tblNewsService) {
  this.tblNewsService = tblNewsService;
 }

 public void setIPUserExtService(IPUserExtService pUserExtService) {
  this.pUserExtService = pUserExtService;
    }


 ...............


}

 

struts-config.xml


http://struts.apache.org/dtds/struts-config_1_3.dtd">

<struts-config>
  <form-beans>
    <form-bean name="loginForm" type="com.poweroa.Fusion.struts.form.LoginForm">
    <form-bean name="regeditForm" type="com.poweroa.Fusion.struts.form.RegeditForm">

  </form-beans>

  <global-exceptions>
  <global-forwards>
  <action-mappings>

    ........
   </action-mappings>
  <controller processorclass="org.springframework.web.struts.AutowiringRequestProcessor">
  <message-resources parameter="com.poweroa.Fusion.struts.ApplicationResources">
</struts-config>

 

 

serviceContext.xml

<beans xmlns="&lt;a href=" http:="" www.springframework.org="" schema="" beans"="">http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
   ..........

 

  <bean id="pUserExtService" parent="baseTransactionProxy">
    <property name="target">
      <bean class="com.poweroa.Fusion.service.application.PUserExtService" init-method="init" autowire="byName">
    </property>
  </bean>

 ..............
  </beans>

 

  • 写回答

2条回答 默认 最新

  • llade163 2008-10-17 10:41
    关注

    应该作为context-param存在,而不是ActionServlet的init-param

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用verilog实现tanh函数和softplus函数
  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题