Struts 2 e Spring - Fazendo a Action funcionar

2 respostas
berlotto

Pessoal, eu jah trabalhava com o Struts 1, e agora estou criando com Struts 2 e Spring.
Meu projeto está com Struts 2, Spring e Hibernate, fazendo a Action chamar um Service que chama o DAO.
Mas estou com um grande problema: Na action, nao sei como criar o acesso ao meu service bean.

Com o request e result, eu consegui através do " implements RequestAware, ParameterAware " na minha classe de action…

Meu codigo está assim:

public class OrdemServicoAction extends DispatchActionSupport implements RequestAware, ParameterAware  {

    private Log log = LogFactory.getLog(this.getClass());
	private Map request;
	private Map parameter;
	private String AHAM;

	public OrdemServicoAction() {
	}

	public String defaultMethod() {
		
		log.debug("getServletRequest getAHAM=" + getAHAM() );
		
		log.debug("getServletRequest parameter.getAHAM=" + parameter.get("AHAM") );

		log.debug("entering 'delete' method...");
		
		WebApplicationContext ctx = getWebApplicationContext();
		
		log.debug("applicationContext exist ? " + (ctx!=null));
		
		OrdemServicoService mgr = (OrdemServicoService) ctx.getBean("ordemServicoService");
		
		log.debug("ordemServicoService exist ? " + (mgr!=null));

		return Action.SUCCESS;
	}

E a variavel ctx está sempre NULL;

Meu applicationContext:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" autowire="autodetect" />
    
    <bean name="usuarioLogado" class="thabit.web.DadosUsuarioLogado" scope="session" autowire="autodetect" >
    	<aop:scoped-proxy/> 
    </bean>

    <import resource="application-services.xml" />

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="database" value="POSTGRESQL" />
                <property name="showSql" value="true" />
            </bean>
        </property>
    </bean>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        ...
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager" />

    <import resource="application-actions.xml"/>

</beans>

O application-services.xml

...

    <!-- SERVICES -->
    <bean id="personService" class="thabit.persist.service.impl.PersonServiceImpl" autowire="autodetect"/>
    <bean id="ordemServicoService" class="thabit.persist.service.impl.OrdemServicoServiceImpl" autowire="autodetect" />

</beans>

e o application-actions.xml

...
	<!-- STRUTS2 ACTIONS -->
    <bean id="personAction" 	  scope="prototype" class="thabit.web.action.PersonAction"   >
    </bean>
    <bean id="ordemServicoAction" scope="prototype" class="thabit.web.action.OrdemServicoAction">
    </bean>
...

Na real eu poderia passar em cada action seu service correto por injeção de classes, por parametro ou pelo constructor da classe... mas pq o "getWebApplicationContext" nao funciona sendo que minha action extende "DispatchActionSupport" ?

Obrigado …

2 Respostas

W

Você deveria deixar o spring injetar as dependências do seu service diretamente na action

public class OrdemServicoAction extends DispatchActionSupport implements RequestAware, ParameterAware  {  

 private OrdemServicoService ordemServicoService;


public void setOrdemServicoService(OrdemServicoService ordemServicoService){
this.ordemServicoService = ordemServicoService;
}

public OrdemServicoService  getOrdemServicoService(){
return ordemServicoService;
}

...
}

e o application-actions.xml

&lt;bean id="personAction"    scope="prototype" class="thabit.web.action.PersonAction"   &gt;  
     &lt;/bean&gt;  
     &lt;bean id="ordemServicoAction" scope="prototype" class="thabit.web.action.OrdemServicoAction"&gt;  
          &lt;property name="ordemServicoService" ref="ordemServicoService" /&gt;
     &lt;/bean&gt;
berlotto

Pois é williammafra !
Mas me diz uma coisa, pq será que com autowire byName ou automatic nao funciona ?
Se nao funcionar, vou ter que acrescentar a injeção na mão para cada bean que eu desejar, sendo que pelo byName, se eu declaro no meu bean, igual ao que você exemplificou, simplesmente o bean aparecerá, sem que tenha que configurar 1 a 1 ! :slight_smile:

E volto a questionar, pq o “getWebApplicationContext” nao funciona sendo que minha action extende “DispatchActionSupport” ?

Criado 15 de junho de 2008
Ultima resposta 16 de jun. de 2008
Respostas 2
Participantes 2