简体   繁体   English

如何在后备bean方法中访问两个不同的Java实体作为参数

[英]how to access two different java entities as parameters in backing bean method

Mojarra 2.1.5 / Java Mojarra 2.1.5 / Java

I have two entities : ie 我有两个实体:

class Primary { String name; String age }
class Second { String dept; String hour }
...

In my managed bean I developed a function to generate PDF regarding my front-end prime-faces radio button (Primary or Second). 在我的托管bean中,我开发了一个函数,用于生成有关我的前端素面单选按钮(Primary或​​Second)的PDF。

If I select in radio button the Primary option, the managed bean method will fire generatePDF() and inside the generatePDF I have : 如果我在单选按钮中选择Primary选项,则托管bean方法将触发generatePDF(),并且在generatePDF内包含:

Primary pr = new Primary();
pr.name = xxxxx;
pr.age = yyyyy;
...
...

But how can I do to re-utilize the same method generatePDF for both entities (Primary and Second ? I need to access both entity properties regarding my radio selection. 但是,我该如何为两个实体重新使用相同的方法generatePDF(Primary和Second ??我需要访问有关无线电选择的两个实体属性。

I need to instantiate the entities dynamically (Or I instantiate Primary or I intantiaty Second at a time) 我需要动态实例化实体(或者一次实例化Primary或​​Itanityty Second)

What about do something like this. 怎么做这样的事情。

interface Pdfeable{  String writeToPDF();}
class Primary implements Pdfeable { String name; String age }
class Second impleaments Pdfeable { String dept; String hour }

Just override with the statements you want to send data to the PDF. 只需用要向PDF发送数据的语句覆盖即可。

class Primary implements Pdfeable {
   String name; String age;
   public String writeToPDF(){
      return getName() +  "" + getAge();
   }
}

And write your code using the interface definition not concrete classes. 并使用接口定义而不是具体的类编写代码。

Assuming as per your question you need one class instantiation at a time as per radio button selection ,I would suggest you should create a valueChangeListener to your radio button.I have hardcoded the values of radio selectItem you can use any way either by bean-binding or hardcoded. 假设根据您的问题每次选择单选按钮一次都需要一个类实例化,建议您为单选按钮创建一个valueChangeListener。我已对radio selectItem的值进行了硬编码,可以通过bean绑定使用任何方式或硬编码。

<h:selectOneRadio value="#{myBean.myRadioValue}" ... onclick="this.form.submit();" valueChangeListener="#{myBean.generatePDF}">
   <f:selectItem itemValue="Radio1" itemLabel="Primary-radio" />
    <f:selectItem itemValue="Radio2" itemLabel="Secondary-radio" />
   </h:selectOneRadio>

This code will submit the form where the radio button are contained when the onclick Javascript event is detected. 当检测到onclick Javascript事件时,此代码将提交包含单选按钮的表单。 On the server side, the action generatePDF will be executed. 在服务器端,将执行generatePDF操作。 In this method, you can do your requiste as on Submit action getter and setter will be called and you can check which radio is selected by comparing using if () and do your stuff: 在这种方法中,您可以像在Submit操作上那样执行请求,将调用getter和setter,并且可以通过使用if()比较来检查选择了哪个单选,然后执行以下操作:

public void generatePDF(ValueChangeEvent evt) {
    if(getMyRadioValue.equals(Radio1)){
     Primary pr = new Primary();
pr.name = xxxxx;
pr.age = yyyyy;
}
else if(getMyRadioValue.equals(Radio2)){
 Secondary s = new Secondary();
s.dept = xxx;
s.hour = yyyy;}

... ... ……

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM