0

I have this ManagedBean:

@ManagedBean(name="studentItem")
@ViewScoped 
public class StudentBean implements Serializable {

    private static final long serialVersionUID = 1L;

    @ManagedProperty("#{StudentService}")
    private StudentService studentService;

    private int regId;
    private String firstName;
    private String lastName;

    //getters and setters


    public void saveStudent(StudentBean student) {
        //calling from xhtml to save form data

        studentService.addStudent(student);
    }
}

and this service implementation:

@Component
@Service("StudentService")
public class StudentServiceImpl implements StudentService {
    @Autowired
    private UserDao<Student> studentDao;
    @Override
    @Transactional
    public void addStudent(StudentBean student) {
         Student stu=new Student();
         stu.setRegId(student.getRegId());
         stu.setFirstName(student.getFirstName());
         stu.setLastName(student.getLastName());
         studentDao.addItem(stu);
    }
}

as you can see, I had to convert my StudentBean managed-bean object to Student object type to save it in database using DAO methods. Is there any standard way other than ugly copying properties one by one?

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
Majid
  • 13,853
  • 15
  • 77
  • 113
  • You can save the StudentBean as it is in case it is the same object. – Σωτήρης Ραφαήλ Nov 08 '18 at 14:34
  • This question is not related to Spring. Start reading https://stackoverflow.com/questions/30639785/jsf-controller-service-and-dao first lots of relevant info. But this is sort of a duplicate:The 'scatter-gatther' antipattern https://stackoverflow.com/questions/10301363/jpa-entity-as-jsf-bean And read https://stackoverflow.com/questions/7223055/making-distinctions-between-different-kinds-of-jsf-managed-beans/7223910#7223910 and https://stackoverflow.com/questions/8463178/what-to-use-managed-beans-backing-beans-or-entity-beans – Kukeltje Nov 08 '18 at 16:54

2 Answers2

1

You are violating the MVC (Model View Controller) pattern! You have 3 parts (the Model=Student, the View (your facelet) and the Controller=StudentBean) which should be independent.

If I were you I'll proceed like this:

@ManagedBean(name="studentItem")
@ViewScoped 
public class StudentBean implements Serializable {
  private Student currentStudent;
  //getter/setter 

  @ManagedProperty("#{StudentService}")
  private StudentService studentService;      

  public String renderStudentForm(){
    //create a new student when you load the form
    currentStudent = new Student();
  }

  public void saveStudent(){
    studentService.addStudent(currentStudent);
  }
}

In your form view you can call student properties using EL #{studentItem.currentStudent.name}

You got the idea.

akuma8
  • 4,160
  • 5
  • 46
  • 82
-1

There are a few utilities out there that help you in this situation. You can try BeanUtils copy properties or DozerMapper

Chris
  • 448
  • 2
  • 8
  • That would be a way to uphold a bad design. Better to do what the duplicates and the answer suggest – Kukeltje Nov 08 '18 at 21:03