这个简单的类需要一个自定义的JSF转换器吗?

时间:2009-12-17 12:18:45

标签: java jsf

我一直在尝试编写JSF应用程序的成功有限。在应用程序的一个部分中,我需要用户从选择菜单中进行选择,该菜单显示可选状态值列表。 Status类(如下所示),用于填充选择菜单中显示的List,是一个由两个字符串组成的简单类:一个是用于在数据库中查找描述的代码,另一个是人类可读的描述。我试图找出我是否需要转换器,如果是这样,如何最好地实现转换器。这是一个使用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;
}

 }

谢谢!

1 个答案:

答案 0 :(得分:6)

  

我试图找出我是否需要转换器,如果是的话,最好如何实现转换器。

只要您希望将非标准Java对象从HTTP请求传递到另一个HTTP请求,就需要转换器。对于非标准,我的意思是 StringNumberBoolean。这一切都只是因为HTTP请求参数StringsNumberBoolean的作用是因为EL可以识别它们并且内置了强制措施。

对于非标准Java对象,您需要实现javax.faces.convert.Converter,将对象转换为String(或Number所需的内容,例如Long id在以HTML格式显示之前,getAsString()方法内的数据库表中关联行的PK可以是PK。在处理请求参数期间,您在getAsObject()方法中执行相反的操作(例如,通过其id从DAO获取关联对象)。

您可以找到here示例,了解如何Converter用于h:selectOneMenu。您看到本文还包含一个替代方案,但您需要在辅助bean中进行更多工作以自行转换(映射)对象。