我有mainpage.xhtml
,它在我的数据库中显示整个记录,并允许我编辑它们。如果是编辑按钮,则调用mainPageController.edit()
函数填充MainPageConroller类中的_bilgi变量,然后重定向到edit.xhtml
页面。
在 edit.xhtml 页面中,它基本上显示了在MainPageController class
之前填充的_bilgi值。这里没有问题(可能有,但我不知道。)
当我想更新其中一个变量,例如 baslik 时,我在 MainPageController 中创建了一个方法,该方法在 bilgiDAO类中调用updateBilgi(_bilgi)
。< /强>
在UpdateBilgi
中,很清楚它的作用是什么,但它没有更新表格。有趣的是,当我调试并遵循变量时,一切顺利但最终它不会更新。
另一件事是,当我在BilgilerDAO中只添加一个字符串时ps.setString(1, "aqa1")
它可以正常工作,但ps.setString(2, bilgi.getBaslik())
不会事件bilgi.getBaslik()返回更改后的值。
可能有什么不对? 提前谢谢你的答案。
我希望我足够清楚。
编辑:我发现了故障。在BilgilerDAO函数updateBilgi(Bilgi bilgi)
变量bilgi
不会从_bilgi获取id,而是获取其他变量。为什么它不会得到身份证?
mainpage.xhtml
<h:form id="bilgiForm">
<h:dataTable id="bilgiTable" value="#{mainPageController.bilgiListesi}"
var="bilgiler" border="1">
<h:column>
<f:facet name="header">
<h:outputText value="Başlık" />
</f:facet>
<h:outputText value="#{bilgiler.baslik}" />
<f:facet name="footer">
<h:outputText value="Başlık" />
</f:facet>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Bilgi Metni" />
</f:facet>
<h:outputText value="#{bilgiler.bilgi}" />
<f:facet name="footer">
<h:outputText value="Bilgi Metni" />
</f:facet>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Bilgi Metni" />
</f:facet>
<h:outputText value="#{bilgiler.bilgi}" />
<f:facet name="footer">
<h:outputText value="Bilgi Metni" />
</f:facet>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Bilgi Metni" />
</f:facet>
<h:graphicImage
value="https://canakkale.000webhostapp.com/showImage.php/?name=#{bilgiler.img.name}" />
<f:facet name="footer">
<h:outputText value="Bilgi Metni" />
</f:facet>
</h:column>
<h:column>
<fcore:facet name="header">Edit</fcore:facet>
<h:commandButton action="edit" value="Edit"
actionListener="#{mainPageController.edit()}">
<fcore:param name="id" value="#{bilgiler.id}" />
</h:commandButton>
</h:column>
</h:dataTable>
</h:form>
<fcore:view>
<h:form>
<fcore:view>
<h:form>
<h1>Update</h1>
<h:panelGrid border="" cellpadding="20" cellspacing="20"
columns="3">
<h:outputLabel value="Bilgi ID" />
<h:inputText value="#{mainPageController._bilgi.id}"
readonly="true" required="true" requiredMessage="Id is Required"
id="bilgiID">
</h:inputText>
<h:message for="bilgiID" />
<h:outputLabel value="Tag" />
<h:inputText value="#{mainPageController._bilgi.tag}"
readonly="true" required="true"
requiredMessage="Tag Girmeniz Gereklidir." id="bilgiTag">
</h:inputText>
<h:message for="bilgiTag" />
<h:outputLabel value="Başlık" />
<h:inputText value="#{mainPageController._bilgi.baslik}"
required="true" requiredMessage="Başlık Girmeniz Gereklidir."
id="bilgiBaslik">
</h:inputText>
<h:message for="bilgiBaslik" />
<h:outputLabel value="Bilgi Metni" />
<h:inputTextarea value="#{mainPageController._bilgi.bilgi}"
required="true" requiredMessage="Başlık Girmeniz Gereklidir."
id="bilgiBilgi">
</h:inputTextarea>
<h:message for="bilgiBilgi" />
<h:outputLabel value="Image URL" />
<h:inputText value="#{mainPageController._bilgi.img.name}"
required="true" requiredMessage="Başlık Girmeniz Gereklidir."
readonly="true" id="bilgiImgName">
</h:inputText>
<h:message for="bilgiImgName" />
<h:outputLabel value="Başlık" />
<input type="file" value="#{mainPageController._bilgi.img.image}" />
<h:commandButton action="#{mainPageController.updateRecord()}"
value="Update">
</h:commandButton>
</h:panelGrid>
</h:form>
</fcore:view>
</h:form>
</fcore:view>
</h:body>
</html>
MainPageController类
@ManagedBean
public class MainPageController implements Serializable {
List<Bilgi> bilgiListesi = new ArrayList<Bilgi>();
private Bilgi _bilgi = new Bilgi();
private static final long serialVersionUID = 1L;
BilgilerDAO bilgiDao = new BilgilerDAO();
ImageDAO imgDao = new ImageDAO();
public MainPageController() {// Default Constructor
try {
bilgiListesi = showAllRecords();
} catch (SQLException e) {
e.printStackTrace();
}
}
public List<Bilgi> showAllRecords() throws SQLException {
return bilgiDao.getAllBilgiler();
}
public List<Image> showAllImages() throws Exception {
return imgDao.getAllImages();
}
public void updateRecord() {
try {
bilgiDao.updateBilgi(_bilgi);
//imgDao.updateImage(_bilgi.getImg());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void edit() {
FacesContext fc = FacesContext.getCurrentInstance();
int id;
HttpServletRequest request = (HttpServletRequest) fc.getExternalContext().getRequest();
id = Integer.parseInt(request.getParameter("id"));
try {
bilgiListesi = bilgiDao.getAllBilgiler();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (Bilgi bilgi : bilgiListesi) {
if (bilgi.getId() == id) {
_bilgi.setBaslik(bilgi.getBaslik());
_bilgi.setBilgi(bilgi.getBilgi());
_bilgi.setImg(bilgi.getImg());
_bilgi.setTag(bilgi.getTag());
_bilgi.setId(bilgi.getId());
}
}
}
public void edit(Bilgi bilgi) {
for (Bilgi existing : getBilgiListesi()) {
existing.setEditable(false);
}
bilgi.setEditable(true);
}
}
BilgilerDAO
@Override
public String updateBilgi(Bilgi bilgi) throws Exception {
String sql = "UPDATE bilgiler SET bilgi=?, baslik =? WHERE id=? ";
ps = DatabaseConnection.getConnection().prepareStatement(sql);
ps.setString(1, "aqa1");
ps.setString(2, bilgi.getBaslik());
ps.setInt(3, bilgi.getId());
int ex = ps.executeUpdate();
if (ex > 0) {
System.err.println("coodld");
}
ps.close();
return "mainpage";
}
答案 0 :(得分:0)
我有点解决了这个问题。
<h:inputText value="#{mainPageController._bilgi.id}" readonly="true" required="true" requiredMessage="Id is Required" id="bilgiID">
在这个xhtml行 readonly =&#34; true&#34; 导致它返回null。我删除它现在它按我想要的方式工作。虽然,我仍然希望它们显示但不可编辑。