2013-08-06 2 views
2

클래스 Categoria에 대한이 속성 편집기가 있으며 서비스에 자동 연결하려고합니다. 서비스가 null 값을 계속 유지하는 문제입니다. 또한 이것은 분리 된 것 같아요. 적어도 컨트롤러에서 같은 클래스의 필드를 자동 배선했기 때문에, 내가 무슨 생각을하는지, 그렇게 생각합니다. 그 당시에는 전혀 작동하지 않았습니다.@Autowired 필드는 null을 얻습니다.

변환기

package com.carloscortina.paidosSimple.converter; 

import java.beans.PropertyEditorSupport; 

import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 
import org.springframework.beans.factory.annotation.Autowired; 

import com.carloscortina.paidosSimple.model.Categoria; 
import com.carloscortina.paidosSimple.service.CategoriaService; 

public class CategoriaConverter extends PropertyEditorSupport{ 

    private final Logger logger = LoggerFactory.getLogger(CategoriaConverter.class); 

    @Autowired 
    private CategoriaService categoriaService; 

    @Override 
    public void setAsText(String categoria) { 

     logger.info(categoria); 
     Categoria cat = new Categoria(); 
     cat = categoriaService.getCategoria(Integer.parseInt(categoria)); 
     setValue(cat); 
    } 


} 

여기에 서비스

package com.carloscortina.paidosSimple.service; 

import java.util.List; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Service; 
import org.springframework.transaction.annotation.Transactional; 

import com.carloscortina.paidosSimple.dao.CategoriaDao; 
import com.carloscortina.paidosSimple.model.Categoria; 

@Service 
@Transactional 
public class CategoriaServiceImpl implements CategoriaService{ 

    @Autowired 
    private CategoriaDao categoriaDao; 

    @Override 
    public Categoria getCategoria(int id) { 
     // TODO Auto-generated method stub 
     return categoriaDao.getCategoria(id); 
    } 

    @Override 
    public List<Categoria> getCategorias() { 
     return categoriaDao.getCategorias(); 
    } 

} 

그것은 않는 일

컨트롤러

package com.carloscortina.paidosSimple.controller; 

import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 

import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.WebDataBinder; 
import org.springframework.web.bind.annotation.InitBinder; 
import org.springframework.web.bind.annotation.ModelAttribute; 
import org.springframework.web.bind.annotation.PathVariable; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.servlet.ModelAndView; 

import com.carloscortina.paidosSimple.converter.CategoriaConverter; 
import com.carloscortina.paidosSimple.model.Categoria; 
import com.carloscortina.paidosSimple.model.Personal; 
import com.carloscortina.paidosSimple.model.Usuario; 
import com.carloscortina.paidosSimple.service.CategoriaService; 
import com.carloscortina.paidosSimple.service.PersonalService; 
import com.carloscortina.paidosSimple.service.UsuarioService; 

@Controller 
public class PersonalController { 

    private static final Logger logger = LoggerFactory.getLogger(PersonalController.class); 

    @Autowired 
    private PersonalService personalService; 

    @Autowired 
    private UsuarioService usuarioService; 

    @Autowired 
    private CategoriaService categoriaService; 

    @RequestMapping(value="/usuario/add") 
    public ModelAndView addUsuarioPage(){ 
     ModelAndView modelAndView = new ModelAndView("add-usuario-form"); 
     modelAndView.addObject("categorias",categorias()); 
     modelAndView.addObject("user", new Usuario()); 
     return modelAndView; 
    } 

    @RequestMapping(value="/usuario/add/process",method=RequestMethod.POST) 
    public ModelAndView addingUsuario(@ModelAttribute Usuario user){ 
     ModelAndView modelAndView = new ModelAndView("add-personal-form"); 
     usuarioService.addUsuario(user); 
     logger.info(modelAndView.toString()); 
     String message= "Usuario Agregado Correctamente."; 
     modelAndView.addObject("message",message); 
     modelAndView.addObject("staff",new Personal()); 

     return modelAndView; 
    } 

    @RequestMapping(value="/personal/list") 
    public ModelAndView listOfPersonal(){ 
     ModelAndView modelAndView = new ModelAndView("list-of-personal"); 

     List<Personal> staffMembers = personalService.getAllPersonal(); 
     logger.info(staffMembers.get(0).getpNombre()); 
     modelAndView.addObject("staffMembers",staffMembers); 

     return modelAndView; 
    } 

    @RequestMapping(value="/personal/edit/{id}",method=RequestMethod.GET) 
    public ModelAndView editPersonalPage(@PathVariable int id){ 
     ModelAndView modelAndView = new ModelAndView("edit-personal-form"); 
     Personal staff = personalService.getPersonal(id); 
     logger.info(staff.getpNombre()); 
     modelAndView.addObject("staff",staff); 

     return modelAndView; 
    } 

    @RequestMapping(value="/personal/edit/{id}", method=RequestMethod.POST) 
    public ModelAndView edditingPersonal(@ModelAttribute Personal staff, @PathVariable int id) { 

     ModelAndView modelAndView = new ModelAndView("home"); 

     personalService.updatePersonal(staff); 

     String message = "Personal was successfully edited."; 
     modelAndView.addObject("message", message); 

     return modelAndView; 
    } 

    @RequestMapping(value="/personal/delete/{id}", method=RequestMethod.GET) 
    public ModelAndView deletePersonal(@PathVariable int id) { 
     ModelAndView modelAndView = new ModelAndView("home"); 
     personalService.deletePersonal(id); 
     String message = "Personal was successfully deleted."; 
     modelAndView.addObject("message", message); 
     return modelAndView; 
    } 

    private Map<String,String> categorias(){ 
     List<Categoria> lista = categoriaService.getCategorias(); 

     Map<String,String> categorias = new HashMap<String, String>(); 
     for (Categoria categoria : lista) { 
      categorias.put(Integer.toString(categoria.getId()), categoria.getCategoria()); 
     } 
     return categorias; 
    } 

    @InitBinder 
    public void initBinderAll(WebDataBinder binder){ 
     binder.registerCustomEditor(Categoria.class, new CategoriaConverter()); 
    } 
} 

DAO

package com.carloscortina.paidosSimple.dao; 

import java.util.List; 

import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Repository; 

import com.carloscortina.paidosSimple.converter.CategoriaConverter; 
import com.carloscortina.paidosSimple.model.Categoria; 

@Repository 
public class CategoriaDaoImp implements CategoriaDao { 

    private final Logger logger = LoggerFactory.getLogger(CategoriaConverter.class); 

    @Autowired 
    private SessionFactory sessionFactory; 

    private Session getCurrentSession(){ 
     return sessionFactory.getCurrentSession(); 
    } 

    @Override 
    public Categoria getCategoria(int id) { 

     Categoria rol = (Categoria) getCurrentSession().get(Categoria.class, id); 
     if(rol == null) { 
      logger.debug("Null"); 
     }else{ 
      logger.debug("Not Null"); 
     } 
     logger.info(rol.toString()); 
     return rol; 
    } 

    @Override 
    @SuppressWarnings("unchecked") 
    public List<Categoria> getCategorias() { 
     // TODO Auto-generated method stub 
     return getCurrentSession().createQuery("from Categoria").list(); 
    } 

} 

루트의 context.xml 사전에

<?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:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:util="http://www.springframework.org/schema/util" 
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
     http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd 
     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> 

    <!-- Root Context: defines shared resources visible to all other web components --> 

    <!-- Enable transaction Manager --> 
    <tx:annotation-driven/> 

    <!-- DataSource JNDI --> 
    <jee:jndi-lookup id="dataSource" jndi-name="jdbc/paidos" resource-ref="true" /> 

    <!-- Session factory --> 
    <bean id="sessionFactory" 
     class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" 
     p:dataSource-ref="dataSource" 
     p:hibernateProperties-ref="hibernateProperties" 
     p:packagesToScan="com.carloscortina.paidosSimple.model" /> 

    <!-- Hibernate Properties --> 
    <util:properties id="hibernateProperties"> 
     <prop key="hibernate.dialect"> 
      org.hibernate.dialect.MySQL5InnoDBDialect 
     </prop> 
     <prop key="hibernate.show_sql">false</prop> 
    </util:properties> 

    <bean id="transactionManager" 
     class="org.springframework.orm.hibernate4.HibernateTransactionManager" 
     p:sessionFactory-ref="sessionFactory" /> 

    <context:annotation-config /> 
    <context:component-scan base-package="com.carloscortina.paidosSimple,com.carlosocortina.paidosSimple.converter,com.carlosocortina.paidosSimple.service,com.carlosocortina.paidosSimple.dao" /> 

</beans> 

주셔서 감사합니다 새로운 연산자 (binder.registerCustomEditor(Categoria.class, new CategoriaConverter());) 그래서 서비스입니다 사용하여 PropertyEditor 외부 스프링 컨텍스트를 만드는

답변

4

주사하지 않음 : 문제를 해결하려면 this link의 안내를 따르십시오.

관련 문제