我需要一些帮助来解决一些我不理解的事情:
所以我想删除一个项目从一个selectOneListBox使用命令按钮,但问题是,当我按下这个按钮,它使用setter方法,但值总是空…我要显示代码
farm.xhtml
<h:form>
<h:panelGrid columns="1" columnClasses="label, value"
cellpadding="10">
<h:panelGroup style="width: 90%">
<p:inputText id="cropVariation" style="margin-right: 10px"
label="#{msg['farms.form.farmBasicInformation.cropVariation']}"
value="#{farmsController.cropVariation}" />
<p:inputText id="cropRotation" style="margin-right: 10px"
label="#{msg['farms.form.farmBasicInformation.cropRotation']}"
value="#{farmsController.cropRotation}" />
<p:watermark for="cropVariation"
value="#{msg['farms.form.farmBasicInformation.cropVariation']}" />
<p:watermark for="cropRotation"
value="#{msg['farms.form.farmBasicInformation.cropRotation']}" />
<p:commandButton value="#{msg['buttons.add']}"
action="#{farmsController.addCrop()}" ajax="true"
icon="ui-icon-plus"
update="majorCropsSelect cropRotation cropVariation"
process="cropVariation cropRotation @this" />
</h:panelGroup>
</h:panelGrid>
<p:fragment autoUpdate="true">
<h:panelGrid columns="2" columnClasses="label, value">
<p:selectOneListbox id="majorCropsSelect"
style="width: 450px"
value="#{farmsController.selectedCrop}"
converter="#{selectOneListBoxConverter}">
<f:selectItems value="#{farmsController.majorCrops}"
var="majorCrop" itemLabel="#{majorCrop}"
itemValue="#{majorCrop}" />
<p:column>
<h:outputText value="#{majorCrop}" />
</p:column>
</p:selectOneListbox>
<p:commandButton value="#{msg['buttons.remove']}"
action="#{farmsController.removeCrop()}" ajax="true"
icon="ui-icon-minus"/>
</h:panelGrid>
</p:fragment>
</h:form>
FarmsController.java
@ManagedBean
@Controller
@ViewScoped
public class FarmsController extends BaseController {
@Autowired
private FarmsService farmsService;
@Autowired
private SecurityService securityService;
public static String BEAN_PAGE = "farm.jsf";
public static String LIST_PAGE = "farmsManagement.jsf";
private Farm currentFarm;
private List<MajorCrop> majorCrops = new ArrayList<MajorCrop>();
private MajorCrop selectedCrop = null;
private String cropVariation;
private String cropRotation;
public void removeCrop() {
if (selectedCrop != null) {
majorCrops.remove(selectedCrop);
}
}
public MajorCrop getSelectedCrop() {
return selectedCrop;
}
public void setSelectedCrop(MajorCrop selectedCrop) {
this.selectedCrop = selectedCrop; (here the value 'selectedCrop' is always null)
}
public List<MajorCrop> getMajorCrops() {
return majorCrops;
}
public void setMajorCrops(List<MajorCrop> majorCrops) {
this.majorCrops = majorCrops;
}
}
SelectOneListBoxConverter.java
@Component("selectOneListBoxConverter")
@FacesConverter(forClass=FarmsController.class, value="selectOneListBoxConverter")
public class SelectOneListBoxConverter implements Converter {
@Override
public Object getAsObject(FacesContext context, UIComponent component,
String value) {
SelectOneListbox p = (SelectOneListbox) component;
MajorCrop dl = (MajorCrop) p.getValue();
return dl;
}
@Override
public String getAsString(FacesContext context, UIComponent component,
Object value) {
return value.toString();
}
}
你知道为什么我可以得到selectedCrop值总是为空吗?
非常感谢!
其实我已经找到问题了。
关键是,在转换器中,我不能使用getValue()属性,因为之前没有设置任何值,所以没有值。
我必须使用value String变量并处理它以获得我想要的对象。下面是代码:
@Component("selectOneListBoxConverter")
@FacesConverter(forClass = FarmsController.class, value = "selectOneListBoxConverter")
public class SelectOneListBoxConverter implements Converter {
@Override
public Object getAsObject(FacesContext context, UIComponent component,
String value) {
MajorCrop dl = new MajorCrop();
String[] values = value.split("\(");
String name = "";
for (int i = 0; i < values.length - 1; i++) {
name += values[i];
}
dl.setName(name.substring(0, name.length() - 1));
dl.setRotations(values[values.length - 1].substring(0,
values[values.length - 1].length() - 1));
return dl;
}
@Override
public String getAsString(FacesContext context, UIComponent component,
Object value) {
return value.toString();
}
}