2016-03-31 11 views
0

Temelde ödemeler için bir CRUD uygulamasına ihtiyacım var. Her ödeme bir hesaba atanır.form: seçili değeri seçmez

Jsp sayfam, "hesap" nesnelerinin doğru listesini gösterir ancak seçilen hesabı ayarlamaz.

  1. Soru: Atanan hesap önceden seçilmiş bir açılır kutuyu nasıl elde edebilirim? Soru: Ödeve (hesaba ödeme) çalışır, ancak yalnızca aşağıdaki ödeme kodumda çalışır (Ödeme olarak işaretlenir) (Ödeme olarak işaretlenir). Bu neden böyle?

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> 

Alan 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; 
    } 

} 

Alan Ödeme

package com.beingjavaguys.domain; 
import javax.persistence.CascadeType; 
import javax.persistence.Entity; 
import javax.persistence.FetchType; 
import javax.persistence.GeneratedValue; 
import javax.persistence.Id; 
import javax.persistence.JoinColumn; 
import javax.persistence.ManyToOne; 

import org.springframework.beans.factory.annotation.Autowired; 



@Entity 
public class Payment { 


    private int id; 


    private Account account; 

    private float amount; 

    private String text; 

    private String comment; 

    @Id 
    @GeneratedValue 
    public int getId() { 
     return id; 
    } 
    public void setId(int id) { 
     this.id = id; 
    } 

    @ManyToOne(cascade = CascadeType.ALL, targetEntity = Account.class, fetch=FetchType.EAGER) 
    @JoinColumn(name="fk_account") 
    public Account getAccount() { 
     return account; 
    } 

    public void setAccount(Account account) { 
     this.account = account; 
    } 

    public float getAmount() { 
     return amount; 
    } 

    public void setAmount(float amount) { 
     this.amount = amount; 
    } 

    public String getText() { 
     return text; 
    } 

    public void setText(String text) { 
     this.text = text; 
    } 

    public String getComment() { 
     return comment; 
    } 

    public void setComment(String comment) { 
     this.comment = comment; 
    } 


} 

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"); 
    } 

} 

sen AccountEditor benim uygulanmasına ilişkin bir bakabilir miyim? Hesabı aramak için Hesap Servisine ihtiyacım var mı, değil mi? Ancak, ben burada örneği hizmet ..

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); 
    } 
} 

cevap

0

Ne gerek Hesap nesneye bir dize accountId dan dönüştürmek için bir PropertyEditor uygulamasıdır alamadım. = "Hesabı" yerine formun seçmek yol: Sonra jsp size yol formu kullanın

public class AccountEditor extends PropertyEditorSupport{ 

    @Override 
    public void setAsText(String text){ 
     Account account = lookupAccount(text); //lookup account by accountId text 
     setValue(account); 
    } 
} 

Sonra da AccountEditor kayıt aşağıdaki gibi = "account.id"

bir PropertyEditor uygulamak seçmek yol senin denetleyici

@InitBinder 
public void initBinder(WebDataBinder binder){ 
    binder.registerCustomEditor(Account.class , new AccountEditor()); 
} 

Yukarıdaki değişikliklerle birlikte geçici çözümünüze ihtiyacınız olmayacaktır. Çözümünüz yalnızca hesabın kimlik özelliğini ayarlıyor ve tüm nesneyi değil, tüm nesneyi değil

+0

AccountEditor uygulamamla ilgili bir göz atabilir misiniz? – tobi

+0

Mükemmel. AccountService bağımlılığının enjekte edilmesine izin vermek için AccountEditor'ınızı bağlamında bir fasulye olarak ekleyin ve kontrolörünüze enjekte edin –

+0

Teşekkürler, bu benim ikinci Soruğumu çözdü. Henüz fisrt Sorusunu çözemedim. – tobi

İlgili konular