Question

When I add the annotation @Transactional in method of cap business, jqGrid doesn't load data. But less annotation works fine, Why?

@Service
@Scope("request")
public class MarcacionServiceImp implements MarcacionService{

@Autowired
MarcacionMapper mapper;

@Transactional  
public List<Marcacion> lista() throws Exception {
    List<Marcacion> lista = mapper.listado();
    return lista;
}

 }

<action name="cargarMarcacion" class="cargaMarcacionAction">
        <result type="json" />
</action>


<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="configLocation" value="/WEB-INF/mybatis-config.xml" />
</bean>

<bean id="dataSource" 
       class="org.springframework.jdbc.datasource.DriverManagerDataSource"
    p:driverClassName="com.mysql.jdbc.Driver" p:url="jdbc:mysql://localhost/asistencia"
    p:username="root" p:password="root" />

<bean id="transactionManager"     
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
  p:dataSource-ref="dataSource" />

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="mapper" />
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>

<bean id="usuarioAction" class="action.UsuarioAction" >
    <property name="service" ref="usuarioService" />    
</bean>  
<bean id="usuarioService" class="negocio.UsuarioServiceImp" />

<bean id="cargaMarcacionAction" class="action.CargaMarcacionAction">
    <property name="service" ref="marcacionService" />
</bean> 
<bean id="marcacionService" class="negocio.MarcacionServiceImp" />

Not get JSON data in my page. I am using Struts2-Spring and MyBatis-Spring. I mean that problem is @Transactional not return my JSON data.

Was it helpful?

Solution

When struts2 return "json" result by default it's configured to serialize the action instance itself. But I think it's not needed for the jqGrid and it requires only the relevant data. You could use the parameters of the "json" result to narrow properties required for serialization. First, you should define a property getter by renaming a method which return data for serialization.

@Transactional  
public List<Marcacion> getLista() throws Exception {
    List<Marcacion> lista = mapper.listado();
    return lista;
}

Second, add the "root" parameter to the result

<result type="json">
  <param name="root">lista</param>
</result>

@Transactional is not related to the JSON data serialization but it requires if you retrieve data from the database.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top