我编写了以下代码,以便在底部有一个文本字段,后跟一个添加按钮和一个保存按钮。
我希望修复第一个文本字段和添加按钮,但是每当用户点击添加按钮时,就会在当前文本字段下方添加一个文本字段,并且添加按钮和保存按钮会关闭。
我有以下代码,但它似乎不起作用。
<?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:c="http://java.sun.com/jstl/core"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Dashboard | BlueWhale Admin</title>
<link rel="stylesheet" type="text/css" href="../css/reset.css" media="screen" />
<link rel="stylesheet" type="text/css" href="../css/text.css" media="screen" />
<link rel="stylesheet" type="text/css" href="../css/grid.css" media="screen" />
<link rel="stylesheet" type="text/css" href="../css/layout.css" media="screen" />
<link rel="stylesheet" type="text/css" href="../css/nav.css" media="screen" />
</h:head>
<body>
<h:form>
<hr/>
<h:dataTable id="newsinputs" value="#{newsAlerts.values}" var="item" cellspacing="10">
<h:column>
<h:outputLabel value="#{item.label}" />
</h:column>
<h:column>
<h:inputText value="#{item.news}" size="100" /><br/>
</h:column>
</h:dataTable>
<h:commandButton styleClass="btn btn-blue" action="#{newsAlerts.add()}" value="Add"></h:commandButton>
<hr/>
<h:commandButton styleClass="btn btn-blue" action="#{newsAlerts.submit}" value="Save" />
</h:form>
</body>
</html>
bean类如下
package com.kc.aop.bean;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import com.kc.aop.VO.NewsVO;
@ManagedBean(name = "newsAlerts")
@ViewScoped
public class News
{
private List<NewsVO> values;
public News()
{
this.values = new ArrayList<NewsVO>();
NewsVO newsVO = new NewsVO();
newsVO.setLabel("News "+ this.values.size()+1);
getValues().add(newsVO);
}
public String submit() {
for(NewsVO newsVO : this.values)
{
System.out.println(newsVO.getNews());
System.out.println(newsVO.getLabel());
}
return null;
// save values in database
}
public List<NewsVO> getValues() {
return values;
}
public void setValues(List<NewsVO> values) {
this.values = values;
}
public String add()
{
NewsVO newsVO = new NewsVO();
newsVO.setLabel("News "+ this.values.size()+1);
this.values.add(newsVO);
return "success";
}
}