- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个带有 primefaces 的 jsf 应用程序。在应用程序中我有几个表。但是,每当我刷新页面或向表中添加新值或重新访问页面时,表中的值都会重复。现在我知道是什么原因造成的。在我的支持 bean 中,我在每个函数中使用驱动程序管理器获取连接来建立数据库连接,然后在函数完成时关闭数据库连接。但是,当我刷新页面时,显然会再次调用获取数据函数并将值附加到表中。我也知道数据库池是这里最好的解决方案,但我无法让它与我的应用程序一起使用。我的问题是如何防止我的 jsf/primefaces 表中的行重复并保持数据库连接以便我可以从数据库中检索这些值?下面是 Animal Bean 类,它具有添加动物的功能和从数据库中检索所有动物的功能。 注意:数据库表中没有重复,只是在jsf View 或jsf表中
@ManagedBean(name = "animal")
@ViewScoped
public class Animal {
public String id;
public String breed;
public String gender;
public double age;
public double weight;
public String description;
public String herd;
public DataSource dataSource;
public Connection DBConn;
public ArrayList list = new ArrayList();
public Animal() {
}
public Animal(String id, String breed, double weight, double age, String gender, String description) {
this.id = id;
this.breed = breed;
this.weight = weight;
this.age = age;
this.gender = gender;
this.description = description;
}
/**
* Creates a new instance of herd
*
* @return
*/
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getBreed() {
return breed;
}
public void setBreed(String breed) {
this.breed = breed;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public double getAge() {
return age;
}
public void setAge(double age) {
this.age = age;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getHerd() {
return herd;
}
public void setHerd(String herd) {
this.herd = herd;
}
public String addAnimal() throws SQLException {
try {
Class.forName("com.mysql.jdbc.Driver");
DBConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/demeter2.0", "root", "root");
} catch (SQLException ex) {
Logger.getLogger(Animal.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(Animal.class.getName()).log(Level.SEVERE, null, ex);
}
int insert = 0;
FacesContext context;
context = FacesContext.getCurrentInstance();
ExternalContext ex = context.getExternalContext();
int value=(int)ex.getSessionMap().get("iduser");
PreparedStatement ps = null;
try {
if (DBConn != null) {
String sql = "INSERT INTO animal(animalsTag,breed,age,weight,description,gender,farmid)VALUES(?,?,?,?,?,?,?)";
ps = (PreparedStatement) DBConn.prepareStatement(sql);
ps.setString(1, id);
ps.setString(2, breed);
ps.setDouble(3, age);
ps.setDouble(4,weight);
ps.setString(5, description);
ps.setString(6, gender);
ps.setInt(7, value);
insert = ps.executeUpdate();
System.out.println(insert);
System.out.println("Data Added Successfully");
}
} catch (Exception e) {
System.out.println(e);
} finally {
try {
ps.close();
DBConn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (insert > 0) {
return "yes";
} else {
return "no";
}
}
public ArrayList<Animal> allAnimals() {
try {
Class.forName("com.mysql.jdbc.Driver");
DBConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/demeter2.0", "root", "root");
} catch (SQLException ex) {
Logger.getLogger(Animal.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(Animal.class.getName()).log(Level.SEVERE, null, ex);
}
PreparedStatement ps = null;
FacesContext context;
context = FacesContext.getCurrentInstance();
ExternalContext ex = context.getExternalContext();
int value=(int)ex.getSessionMap().get("iduser");
try {
if (DBConn != null) {
String sql = "Select animalsTag,breed,age,weight,description,gender FROM animal where farmid = '"
+ value + "'";
ps = (PreparedStatement) DBConn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
if (!rs.next()) {
return null;
} else {
rs.beforeFirst();
while (rs.next()) {
Animal animal = new Animal(
rs.getString("animalsTag"),
rs.getString("breed"),
rs.getDouble("age"),
rs.getDouble("weight"),
rs.getString("description"),
rs.getString("gender"));
list.add(animal);
}//end while
}
}
} catch (Exception e) {
System.out.println(e);
} finally {
try {
ps.close();
DBConn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return list;
}
}
JSF代码
<p:layout fullPage="true">
<p:layoutUnit position="north" size="100" resizable="true" closable="true" collapsible="true">
<div id="banner" style="float:right;">
<p:commandButton value="Log Out" action="#{farm.logout()}" />
</div>
<p:graphicImage url="/resources/images/Demeter1.png" />
</p:layoutUnit>
<p:layoutUnit position="south" size="100" closable="true" collapsible="true">
Footer
</p:layoutUnit>
<p:layoutUnit position="west" size="175" header="Left" collapsible="true">
<p:menu>
<p:submenu label="Index">
<p:menuitem value="Manage Farm" url="managefarm.xhtml"/>
<p:menuitem value="Add Paddock" url="outline.xhtml"/>
<p:menuitem value="Create Grass Wedge" url="grass.xhtml"/>
<p:menuitem value="Herd Management" url="addAnimal.xhtml"/>
</p:submenu>
</p:menu>
</p:layoutUnit>
<p:layoutUnit position="center">
<h1>You can see all animals on your farm here.</h1>
<p:dataTable var="animal" value="#{animal.allAnimals()}">
<p:column headerText="Id">
<h:outputText value="#{animal.id}" />
</p:column>
<p:column headerText="Age">
<h:outputText value="#{animal.age}" />
</p:column>
<p:column headerText="Breed">
<h:outputText value="#{animal.breed}" />
</p:column>
<p:column headerText="Weight">
<h:outputText value="#{animal.weight}" />
</p:column>
</p:dataTable>
</p:layoutUnit>
</p:layout>
最佳答案
您的 bean 是 Viewscoped这意味着当您停留在页面上时,bean 包含的数据仍然有效。为什么这有关系 ?因为您的动物列表仍然包含您之前添加的内容以及您在方法 allAnimals() 中添加的内容。换句话说,将其添加到方法 allAnimals() 的第一行。
list = new ArrayList();
关于java - 如何停止jsf中的表行重复?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31729193/
我试图了解 JSF 实现如何识别用户的各种可能操作。在我放在一起的简单应用程序中,我在 login.xhtml 页面中配置了以下字段。 用户名 - 输入字段 密码 - 密码字段 登录按钮 取消按钮 登
我已经开始学习 JSF,我想知道在我们的类路径中包含什么 JAR 以开始使用 JSF。是jsf-api或 jsf-impl ?或者我们必须同时包含两者?如果两者都是,那么为什么它们不合并? 最佳答案
我是 java server faces (JSF) 的初学者,我需要将文本输入的内容传递到第二页以显示它,同样适用于第二页:我想将单选按钮值传递到第三页。我搜索并尝试了很多但没有成功。例如我试过
我有一个 JSF 页面。我的 CommandButton 操作方法值取决于 bean 变量值。 例子: Bean headerBean 具有可变的 actionValue,值为“someBean.do
我有两个 JSF 页面,假设 A 和 B。从这两个页面 A 和 BI 可以导航到页面 C。现在页面 C 中有一个按钮(确定按钮),单击它应该导航回 A 或 B,具体取决于从哪里(A 或 B)调用 C
我可以在没有 JSTL 标签的情况下使用 JSF 执行条件逻辑吗? 例如,我制作了一个复合组件,并想说明,如果指定了“id”属性,则定义“id”属性,但如果未指定“id”属性,则不要指定“id”属性。
我有一个应用程序,用户可以在其中从我的应用程序的欢迎页面动态切换语言环境。我看到早期的开发人员(在没有太多文档的情况下继承了代码)已经从 ViewHandler 覆盖了以下三个方法,并告诉我这是动态切
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 7年前关闭。 Improve thi
有没有一种方法可以在我的 JSF 2.0 应用程序中处理浏览器刷新事件,以便在浏览器刷新页面时将用户导航到欢迎页面? 这让我想到了另一个问题,即如何在托管 bean 中进行页面导航? 干杯, 最佳答案
我有两页。搜索页面是第一个接受用户输入的页面。第二页显示数据表中的结果集。第二页有 3 个面板,用于结果集、更新和在单个页面中创建。根据单击的按钮,我将面板呈现为真和假。 . . . . . .
由于我们在 Asp.Net 中有 comparevalidator,我们在 JSF 中有什么来验证两个字段的值是否相同?我想验证密码和确认密码字段。 最佳答案 不,这样的验证器在基本的 JSF 实现中
我想构建一个自定义 JSF 组件。现在我从 oracle 阅读了一些文档并看到了一些代码示例。问题是我有点困惑: 似乎有两种方法可以使用 JSF 2.0+ 构建自定义组件。据我了解,自 JSF 2.0
我遇到了与 user1598186 在他的问题中提到的相同的问题:p:commandButton doesn't call bean's method in an page 但是,没有给出解决方案(
这个问题在这里已经有了答案: Ajax update/render does not work on a component which has rendered attribute (1 个回答)
是否有内置机制可以有条件地重定向到另一个 View ?如果他/她已经登录,我希望用户从登录页面重定向到“主页”。 我已经有两种基本方法,但对于第一种我不知道如何实现,第二种是一种肮脏的解决方法。 添加
如何在 JSF 中格式化值 我需要格式化一个数字,如:12345.67 到 12,345.67 可以用模式吗? 最佳答案 尝试使用: 关于jsf - 用逗号格式化为数字 JSF,我们在Sta
根据this blog JSF 正在走向无状态。使用 JSF 的全部意义不在于它使保存和恢复状态成为一件苦差事。 JSF 成为无状态的有什么意义?您能否提供一个有用的示例。 最佳答案 首先,我想澄清
我读到某个地方(不再找到它),可以在资源包中使用EL Expresions,然后在不更改* .xhtml文件的情况下使用它。 some.text=#{someBean.stepsLeft} more
我想看一个简单的登录应用程序,不像this那么简单尽管。 我想要实现的是对 JSF 的工作原理的理解,我开发了很多 ASP.NET,您可以在其中隐藏代码,并且您可以在其中检查是否在登录时创建了 ses
如果#{myBean.birthdate}是java.util.Calendar或java.util.Date类型,我可以格式化吗this 在 EL 本身内部可能使用现有函数,其输出类似于 DateF
我是一名优秀的程序员,十分优秀!