Grundsätzlich brauche ich eine CRUD-Anwendung für Zahlungen. Jede Zahlung ist einem Konto zugeordnet.form: select setzt den ausgewählten Wert nicht
Meine JSP-Seite zeigt die korrekte Liste der "Konto" -Objekte, aber es stellt nicht das ausgewählte Konto.
Frage: Wie kann ich eine Drop-Down-Box mit dem zugewiesenen Account vorausgewählt erreichen?
Frage: Die Zuordnung (Konto zur Zahlung) funktioniert, aber nur mit dem folgenden Code in meiner PaymentDaoImpl.java (als Workaround markiert). Warum ist das der Fall?
PaymentDaoImpl.java
..
@Override
@Transactional
public int insertRow(Payment obj) {
// Session session = HibernateUtil.getSessionFactory().openSession();
Session session = sessionFactory.openSession();
// !!!! workaround?? If I don't do this, account won't be assigned
int accountId = obj.getAccount().getId();
Account account = (Account) session.get(Account.class, accountId);
obj.setAccount(account);
Transaction tx = session.beginTransaction();
session.saveOrUpdate(obj);
tx.commit();
Serializable id = session.getIdentifier(obj);
session.close();
return (Integer) id;
}
..
jsp:
<form:select path="account.id" >
<form:option value="-1" label="Select Account" />
<form:options items="${accountList}" itemValue="id" itemLabel="iban" />
</form:select>
Domain Account.java:
package com.beingjavaguys.domain;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.*;
import org.springframework.beans.factory.annotation.Autowired;
@Entity
public class Account {
@Id
@GeneratedValue
private int id;
private String iban;
private String bank;
private String beschreibung;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getIban() {
return iban;
}
public void setIban(String iban) {
this.iban = iban;
}
public String getBank() {
return bank;
}
public void setBank(String bank) {
this.bank = bank;
}
public String getBeschreibung() {
return beschreibung;
}
public void setBeschreibung(String beschreibung) {
this.beschreibung = beschreibung;
}
}
Domain Payment
PaymentController.java
package com.beingjavaguys.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.beingjavaguys.domain.Payment;
import com.beingjavaguys.services.AccountService;
import com.beingjavaguys.services.PaymentService;
@Controller
@RequestMapping("/payment")
public class PaymentController {
@Autowired
PaymentService dataService;
@Autowired
AccountService accountService;
@RequestMapping("form")
public ModelAndView getForm(@ModelAttribute Payment obj) {
ModelAndView mav = new ModelAndView("payment/form");
mav.addObject("accountList", accountService.getList());
return mav;
}
@RequestMapping("insert")
public ModelAndView insert(@ModelAttribute Payment obj) {
dataService.insertRow(obj);
return new ModelAndView("redirect:list");
}
@RequestMapping("list")
public ModelAndView getList() {
List objList = dataService.getList();
return new ModelAndView("payment/list","objList",objList);
}
@RequestMapping("delete")
public ModelAndView deleteUser(@RequestParam int id) {
dataService.deleteRow(id);
return new ModelAndView("redirect:list");
}
@RequestMapping("edit")
public ModelAndView editUser(@RequestParam int id,@ModelAttribute Payment obj) {
ModelAndView mav = new ModelAndView("payment/form");
Payment paymentObj = dataService.getRowById(id);
mav.addObject("accountList", accountService.getList());
mav.addObject("paymentObj", paymentObj);
return mav;
}
@RequestMapping("update")
public ModelAndView updateUser(@ModelAttribute Payment obj) {
dataService.updateRow(obj);
return new ModelAndView("redirect:list");
}
}
Können Sie einen Blick auf meine Umsetzung der AccountEditor? Ich benötige den AccountService, um den Account zu suchen, oder nicht? Aber ich bekomme den Dienst hier nicht instanziiert ..
public class AccountEditor extends PropertyEditorSupport {
@Autowired
AccountService dataService; // == null ??
@Override
public void setAsText(String text) {
Account account = lookupAccount(text); // lookup account by accountId
// text
setValue(account);
}
private Account lookupAccount(String text) {
int id = Integer.parseInt(text);
return dataService.getRowById(id);
}
}
Können Sie einen Blick auf meine Implementierung des AccountEditor? – tobi
Es ist perfekt. Um die AccountService-Abhängigkeit zu injizieren, fügen Sie Ihren AccountEditor als Bean in Ihren Kontext ein und injizieren ihn in Ihren Controller. –
Danke, das hat meine zweite Frage gelöst. Ich konnte die Frage noch nicht lösen. – tobi