gpt4 book ai didi

java - 这个简单的类是否需要定制的 JSF 转换器?

转载 作者:行者123 更新时间:2023-11-29 07:22:42 25 4
gpt4 key购买 nike

我一直在尝试编写 JSF 应用程序,但收效甚微。在应用程序的一部分中,我需要用户从显示可选状态值列表的选择菜单中进行选择。用于填充选择菜单中显示的列表的 Status 类(如下所示)是一个由两个字符串组成的简单类:一个是用于在数据库中查找描述的代码,另一个是人类可读的描述。我试图找出我是否需要转换器,如果需要,如何最好地实现转换器。这是一个使用 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(为清楚起见,删除了一些不相关的部分,例如分页):

公共(public)类 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;
}

}

谢谢!

最佳答案

I am trying to find out if I need a converter here at all, and if so, how best to implement the converter.

每当您想要将非标准 Java 对象从一个 HTTP 请求传递到另一个 HTTP 请求时,您都需要一个转换器。对于非标准,我的意思是不是 String , NumberBoolean .这一切都是因为 HTTP 请求参数只能字符串NumberBoolean 起作用是因为 EL可以识别它们并为其内置强制转换。

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

你可以找到here如何将 Converter 用于 h:selectOneMenu 的示例。您会看到本文还包含一个替代方案,但您需要在支持 bean 中做更多的工作才能自己转换(映射)对象。

关于java - 这个简单的类是否需要定制的 JSF 转换器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1921381/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com