【发布时间】:2018-11-08 14:32:26
【问题描述】:
我有这个 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);
}
}
还有这个服务实现:
@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);
}
}
如您所见,我必须使用 DAO 方法将我的 StudentBean managed-bean 对象转换为 Student 对象类型以将其保存在数据库中。除了丑陋的一一复制属性,还有什么标准的方法吗?
【问题讨论】:
-
您可以按原样保存 StudentBean,以防它是同一个对象。
-
这个问题与Spring无关。开始阅读stackoverflow.com/questions/30639785/… 第一批相关信息。但这有点重复:'scatter-gatther' 反模式stackoverflow.com/questions/10301363/jpa-entity-as-jsf-bean 并阅读stackoverflow.com/questions/7223055/… 和stackoverflow.com/questions/8463178/…
标签: spring jsf managed-bean