Java 如果您使用不安全的操作,则可以将字符串放入列表中: List<Integer> intList = new ArrayList<Integer>(); List list = intList; list.add("1");
编辑:我不熟悉JSF,但我认为您的猜测是正确的。一个解决方案是使Java 如果您使用不安全的操作,则可以将字符串放入列表中: List<Integer> intList = new ArrayList<Integer>(); List list = intList; list.add("1");,java,jsf,jdbc,Java,Jsf,Jdbc,编辑:我不熟悉JSF,但我认为您的猜测是正确的。一个解决方案是使currentSubscriptionsaList无处不在(这似乎是JSF所期望的)。然后,get(0)将返回一个字符串,您可以将其解析为一个整数。可能有一种更干净的方法,但这应该有效。如果使用不安全的操作,可以将字符串放入列表中: List<Integer> intList = new ArrayList<Integer>(); List list = intList; list.add("1"); 编辑
currentSubscriptions
aList
无处不在(这似乎是JSF所期望的)。然后,get(0)
将返回一个字符串
,您可以将其解析为一个整数
。可能有一种更干净的方法,但这应该有效。如果使用不安全的操作,可以将字符串放入列表中:
List<Integer> intList = new ArrayList<Integer>();
List list = intList;
list.add("1");
编辑:我不熟悉JSF,但我认为您的猜测是正确的。一个解决方案是使currentSubscriptions
aList
无处不在(这似乎是JSF所期望的)。然后,get(0)
将返回一个字符串
,您可以将其解析为一个整数
。可能有一个更干净的方法,但这应该是可行的。您需要指示h:selectManyCheckbox
通过指定javax.faces.Integer
作为转换器将值转换为整数。泛型类型在EL中是未知的,它默认将参数视为String
<h:selectManyCheckbox converter="javax.faces.Integer">
无需使用列表
,这只会导致bean中更弱的类型混乱。您需要指示h:selectManyCheckbox
将值转换为整数,方法是指定javax.faces.Integer
作为转换器。泛型类型在EL中是未知的,它默认将参数视为String
<h:selectManyCheckbox converter="javax.faces.Integer">
不需要使用列表
,这只会在bean中导致更弱的类型混乱。但我没有做任何这样的事情。我怎样才能摆脱这个?我需要做哪些更改来代替列表?@Nitesh,使用该代码打印每个元素的类。这将告诉你这是否是问题所在。嗨,马修,是的,这确实是问题所在。我得到这个:-信息:class java.lang.String
。我怎样才能解决这个问题?为什么它被过度聪明?即使我在其中传递整数?@Nitesh,也要发布更多的代码(任何以某种方式连接到currentSubscriptions
)。很明显,你在某个地方有一个不安全的引用,你正在使用它添加一个字符串。嗨,Matthew Flaschen,请阅读我编辑的帖子。至少编辑的代码应该可以工作吗?但即使这样也不起作用。但我没有做任何这样的事情。我怎样才能摆脱这个?我需要做哪些更改来代替列表?@Nitesh,使用该代码打印每个元素的类。这将告诉你这是否是问题所在。嗨,马修,是的,这确实是问题所在。我得到这个:-信息:class java.lang.String
。我怎样才能解决这个问题?为什么它被过度聪明?即使我在其中传递整数?@Nitesh,也要发布更多的代码(任何以某种方式连接到currentSubscriptions
)。很明显,你在某个地方有一个不安全的引用,你正在使用它添加一个字符串。嗨,Matthew Flaschen,请阅读我编辑的帖子。至少编辑的代码应该可以工作吗?但即使这样也不起作用。setInt
实际上是发生错误的地方吗PreparedStatement.setInt
只有一个重载,包含两个整数参数;您的List.get
应该自动取消装箱,并且应该可以正常工作。但是Utils.getPreparedStatement
呢?这是自定义方法还是在自定义库中?在Google上搜索特定的静态方法只会返回此页面。currentSubscriptions.get(0).getClass().getName()
返回什么?@Paul,currentSubscriptions.get(0)
将导致ClassCastException
。他需要分配给一个列表,正如我在回答中所示。嗨,布莱恩,是的,这只是我的自定义方法。我发现了问题,但没有找到解决方案。你能发布整个stacktrace吗?setInt
实际上是发生错误的地方吗PreparedStatement.setInt
只有一个重载,包含两个整数参数;您的List.get
应该自动取消装箱,并且应该可以正常工作。但是Utils.getPreparedStatement
呢?这是自定义方法还是在自定义库中?在Google上搜索特定的静态方法只会返回此页面。currentSubscriptions.get(0).getClass().getName()
返回什么?@Paul,currentSubscriptions.get(0)
将导致ClassCastException
。他需要分配给一个列表,正如我在回答中所示。嗨,布莱恩,是的,这只是我的自定义方法。我发现了问题,但没有找到解决方案。你能发布整个stacktrace吗?非常感谢我所寻找的!谢谢你,这正是我想要的!
package beans;
import entities.Customer;
import entities.Subscription;
import java.io.IOException;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Savepoint;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import misc.Utils;
@ManagedBean
@ViewScoped
public class AddSubscriptionBean implements Serializable {
private Customer customer;
private List<Integer> currentSubscriptions;
private List<Subscription> subscriptionList;
public List<Subscription> getSubscriptionList() {
return subscriptionList;
}
public void setSubscriptionList(List<Subscription> subscriptionList) {
this.subscriptionList = subscriptionList;
}
public List<Integer> getCurrentSubscriptions() {
return currentSubscriptions;
}
public void setCurrentSubscriptions(List<Integer> currentSubscriptions) {
this.currentSubscriptions = currentSubscriptions;
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
/** Creates a new instance of AddSubscriptionBean */
public AddSubscriptionBean() throws IOException, SQLException {
Connection objConnection = null;
try {
HttpServletRequest objHttpServletRequest = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
int intCustomerId = Integer.parseInt(objHttpServletRequest.getParameter("cid"));
String strQuery = "Select * from customer Where CustomerID = " + intCustomerId;
ResultSet objResultSet = Utils.executeResultSet(objConnection, strQuery);
if (objResultSet.next()) {
String strFirstName = objResultSet.getString("FirstName");
String strLastName = objResultSet.getString("LastName");
customer = new Customer(intCustomerId, strFirstName, strLastName);
}
currentSubscriptions = new ArrayList<Integer>();
for (Subscription objSubscription : customer.getSubscriptionList()) {
currentSubscriptions.add(objSubscription.getSubscriptionId());
}
subscriptionList = new ArrayList<Subscription>();
strQuery = "Select * from subscription";
objResultSet = Utils.executeResultSet(objConnection, strQuery);
while (objResultSet.next()) {
int intSubscriptionId = objResultSet.getInt("SubscriptionId");
String strSubsriptionTitle = objResultSet.getString("Title");
String strSubsriptionType = objResultSet.getString("Type");
Subscription objSubscription = new Subscription(intSubscriptionId, strSubsriptionTitle, strSubsriptionType);
subscriptionList.add(objSubscription);
}
} catch (Exception ex) {
ex.printStackTrace();
FacesContext.getCurrentInstance().getExternalContext().redirect("index.jsf");
} finally {
if (objConnection != null) {
objConnection.close();
}
}
}
public void save() throws SQLException {
Connection objConnection = null;
Savepoint objSavepoint = null;
try {
objConnection = Utils.getConnection();
objConnection.setAutoCommit(false);
objSavepoint = objConnection.setSavepoint();
String strQuery = "Delete From cust_subs Where CustomerId = " + customer.getCustomerId();
if (!Utils.executeQuery(objConnection, strQuery)) {
throw new Exception();
}
strQuery = "Insert Into cust_subs (CustomerId,SubscriptionId) Values (?,?)";
int intCustomerId = customer.getCustomerId();
PreparedStatement objPreparedStatement = Utils.getPreparedStatement(objConnection, strQuery);
for (int intIndex = 0; intIndex < currentSubscriptions.size(); intIndex++) {
objPreparedStatement.setInt(1, intCustomerId);
int intSubscriptionId = Integer.parseInt( currentSubscriptions.get(0).toString());
objPreparedStatement.setInt(2, intSubscriptionId );
objPreparedStatement.addBatch();
}
objPreparedStatement.executeBatch();
objConnection.commit();
} catch (Exception ex) {
ex.printStackTrace();
if (objConnection != null) {
objConnection.rollback(objSavepoint);
}
} finally {
if (objConnection != null) {
objConnection.close();
}
}
}
}
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:msc="http://mscit/jsf">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<center>
<h:form>
<h1>Add Subscription</h1>
<b> Customer Name :</b> <h:outputText value="#{addSubscriptionBean.customer.firstName} #{addSubscriptionBean.customer.lastName}"/>
<h:selectManyCheckbox value="#{addSubscriptionBean.currentSubscriptions}">
<f:selectItems value="#{addSubscriptionBean.subscriptionList}" var="row" itemLabel="#{row.title}" itemValue="#{row.subscriptionId}" />
</h:selectManyCheckbox>
<h:commandButton value="Save" actionListener="#{addSubscriptionBean.save}"/>
</h:form>
</center>
</h:body>
</html>
List<Integer> intList = new ArrayList<Integer>();
List list = intList;
list.add("1");
List currentSubscriptionsUnsafe = currentSubscriptions;
for(Object o : currentSubscriptionsUnsafe)
{
System.out.println(o.getClass());
}
<h:selectManyCheckbox converter="javax.faces.Integer">