我一直在以有限的成功尝试编写JSF应用程序。在应用程序的一个部分中,我需要用户从一个选择菜单中进行选择,该菜单显示了可选状态值的列表。Status类(如下所示)用于填充选择菜单中显示的List,它是一个由两个String组成的简单类:一个是用于在数据库中查找描述的代码,另一个是易于理解的描述。我试图找出我是否完全需要转换器,如果需要,如何最好地实现转换器。这是一个使用Java 1.5的JSF 1.1项目
我在JSP中使用以下代码:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%> <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%> <f:view> <html> <h:graphicImage id="image" url="/images/appname.jpg" alt="app name" /> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <jsp:include page="/jsp/menu.jsp" /> </head> <body> <h:outputText value="Add Value" style="font-size:20px;" /> <h:messages errorStyle="color: red" infoStyle="color: green" layout="table" /> <h:form id="statusReasonEditForm"> <table> <tr> <td><h:outputText id="txtvalue" value="Status" /></td> <td><h:selectOneMenu id="selectStatus" value="#{pc_statusReasonBacker.status}" binding="#{pc_statusReasonBacker.selectItem}"> <f:selectItems value="#{pc_statusReasonBacker.selectStatuses}" /> <f:converter converterId="statusConverter" /> </h:selectOneMenu> <td><h:outputText id="txtvaluereason" value="Status Reason" /></td> <td><h:inputText id="txtinputreason" value="#{pc_statusReasonBacker.statusReason.statusReason}" maxlength="100" size="40" /></td> <td><h:outputText id="txtvaluereasondesc" value="Status Reason Desc" /></td> <td><h:inputText id="txtinputreasondesc" value="#{pc_statusReasonBacker.statusReason.statusReasonDesc}" maxlength="100" size="40" /></td> </tr> </table> <tr> <td><h:commandButton id="savebutton" value="Save" action="#{pc_statusReasonBacker.save}" /></td> <td><h:commandButton id="cancelbutton" value="Cancel" action="#{pc_statusReasonBacker.cancel}" /></td> </tr> </h:form> <hr /> </body> </html> </f:view>
此处显示了支持bean(为清晰起见,删除了一些无关的部分,例如分页):
公共类StatusReasonBacker {
private List<StatusReason> statusReasonList; private List<Status> statusList; private List<SelectItem> selectStatuses; private StatusReason statusReason; private StatusDao sDao; private Status status; private UIData statusReasonTable; private HtmlSelectOneMenu selectItem; private String selectedStatus = ""; public StatusReasonBacker() { sDao = new StatusDao(); statusReason = new StatusReason(); selectStatuses = new ArrayList<SelectItem>(); status = new Status(); selectItem = new HtmlSelectOneMenu(); } public String insert() { status.setStatusCde(""); statusReason.setStatus(status); statusReason.setStatusReason(""); statusReason.setStatusReasonCde(""); statusReason.setStatusReasonDesc(""); return "success"; } public String edit() { this.statusReason = (StatusReason) statusReasonTable.getRowData(); selectItem.setValue(statusReason.getStatus().getStatusCde()); return "success"; } public String update() { if ("".equalsIgnoreCase(statusReason.getStatusReason().trim())) { Message .addErrorMessage("You must enter a value for the status reason."); return "failure"; } else if (("".equalsIgnoreCase(statusReason.getStatusReasonDesc() .trim()))) { Message .addErrorMessage("You must enter a value for the status reason description."); return "failure"; } sDao.updateStatusReason(statusReason); return "statusreasons"; } public String delete() { StatusReason statReason = (StatusReason) statusReasonTable.getRowData(); sDao.deleteStatusReason(statReason); return "statusreasons"; } public String cancel() { return "statusreasons"; } public String save() { statusReason.setStatus(status); sDao.insertStatusReason(statusReason); return "statusreasons"; } ... public StatusDao getSDao() { return sDao; } public void setSDao(StatusDao dao) { sDao = dao; } public List<StatusReason> getStatusReasonList() { statusReasonList = sDao.getStatusReasons(); return statusReasonList; } public void setStatusReasonList(List<StatusReason> statusReasonList) { this.statusReasonList = statusReasonList; } public UIData getStatusReasonTable() { return statusReasonTable; } public void setStatusReasonTable(UIData statusReasonTable) { this.statusReasonTable = statusReasonTable; } public StatusReason getStatusReason() { return statusReason; } public void setStatusReason(StatusReason statusReason) { this.statusReason = statusReason; } public List<Status> getStatusList() { statusList = sDao.getStatuses(); return statusList; } public void setStatusList(List<Status> statusList) { this.statusList = statusList; } public List<SelectItem> getSelectStatuses() { selectStatuses.clear(); if (statusList == null) { statusList = this.getStatusList(); } for (Status sr : statusList) { SelectItem si = new SelectItem(); si.setValue(sr.getStatusCde()); si.setLabel(sr.toString()); si.setDescription(sr.toString()); selectStatuses.add(si); } return selectStatuses; } public void setSelectStatuses(List<SelectItem> selectStatuses) { this.selectStatuses = selectStatuses; } public String getSelectedStatus() { selectedStatus = statusReason.getStatusDesc(); return selectedStatus; } public void setSelectedStatus(String selectedStatus) { this.selectedStatus = selectedStatus; } public Status getStatus() { return status; } public void setStatus(Status status) { this.status = status; } public HtmlSelectOneMenu getSelectItem() { return selectItem; } public void setSelectItem(HtmlSelectOneMenu selectItem) { this.selectItem = selectItem; } }
谢谢!
我试图找出我是否完全需要转换器,如果需要,如何最好地实现转换器。
每当要将非标准Java对象从HTTP请求传递到另一个HTTP请求时,都需要一个转换器。非标准我的意思是 不是 一个String,Number或Boolean。这全是因为HTTP请求参数 只能 是Strings。这Number和Boolean作品是因为EL可以识别它们,并已内置的强制它。
String
Number
Boolean
Strings
对于非标准Java对象,您需要在javax.faces.convert.Converter将其显示为HTML之前,在方法内部实现一个将Object转换为a String(或所需的a Number,例如Long id,可以是数据库表中相关行的PK)的a getAsString()。您可以getAsObject()在处理请求参数的过程中对方法进行另一种处理(例如,通过DAO从DAO获取关联的对象id)。
javax.faces.convert.Converter
Long id
getAsString()
getAsObject()
id
你可以找到这里的如何使用的例子Converter的h:selectOneMenu。您会看到本文也包含其他方法,但是您需要在后备bean中做更多的工作来自己转换(映射)对象。
Converter
h:selectOneMenu