我正在尝试使用带有CDI的纯JSF 2.2来更新来自Bootstrap Modal的h:dataTable中的值。
这是dataTable:
<h:dataTable id="tabNotas" value="#{notaBean.notas}" var="nota"
styleClass="table table-hover">
<h:column>
<f:facet name="header">
<h:outputText value="ALUNO" />
</f:facet>
<h:outputText value="#{nota.aluno.nome}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Disciplina" />
</f:facet>
<h:outputText value="#{nota.disciplina.nome}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="TIPO" />
</f:facet>
<h:outputText value="#{nota.tipo.descricao}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="NOTA" />
</f:facet>
<h:outputText value="#{nota.valor}" />
</h:column>
<h:column>
<h:commandLink class="btn btn-primary" value="Alterar">
<f:passThroughAttribute name="data-toggle" value="modal" />
<f:passThroughAttribute name="data-target" value="#myModal" />
<f:ajax listener="#{notaBean.editarNota(nota)}"
render="formModalNotas" />
</h:commandLink>
</h:column>
</h:dataTable>
这是模态:
<div id="myModal" class="modal fade">
<h:form id="formModalNotas" role="form">
<div class="modal-dialog">
<div class="modal-content">
<h:messages showSummary="true" errorClass="alert alert-danger"
infoClass="alert alert-success" for="formModalNotas" />
<input type="hidden" name="id" value="#{notaBean.nota.aluno.id}" />
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"
aria-hidden="true"></button>
<h4 class="modal-title">Alterar</h4>
</div>
<div class="modal-body">
<div class="form-group ">
<label for="tipoNota">Tipo da Nota:</label>
<h:inputText value="#{notaBean.nota.tipo.descricao}"
styleClass="form-control" id="tipoNota" disabled="true" />
</div>
<div class="form-group ">
<label for="nota">Nota:</label>
<h:inputText value="#{notaBean.nota.valor}"
styleClass="form-control" id="nota" />
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default"
data-dismiss="modal">Close</button>
<h:commandButton value="Salvar" action="#{notaBean.alterar}"
styleClass="btn btn-primary">
</h:commandButton>
</div>
</div>
</div>
</h:form>
</div>
模态工作正常,问题出在action =“#{notaBean.alterar}”中,从未调用过。
我做错了吗?有人曾经这样做过吗?
修改
我正在使用Tomcat 8.这是NotaBean的代码,我还没有更改为ViewScoped = /
@Named(value = "notaBean")
@ViewScoped
public class NotaBean implements Serializable {
private static final long serialVersionUID = 5134990385063835570L;
@Inject
private NotaDao notaDao;
@Inject
private Dao<Curso> daoCurso;
@Inject
private Dao<Disciplina> disciplinaDao;
@Inject
private Nota nota;
private Aluno aluno;
private List<Disciplina> disciplinas;
@PostConstruct
public void init() {
nota.setDisciplina(new Disciplina());
}
public Nota getNota() {
return nota;
}
public void setNota(Nota nota) {
this.nota = nota;
}
public Aluno getAluno() {
return aluno;
}
public void setAluno(Aluno aluno) {
this.aluno = aluno;
}
public List<Disciplina> getDisciplinas() {
if (aluno == null || aluno.getCurso().getId() == null) {
disciplinas = null;
} else {
Curso cursoSelecionado = daoCurso.buscaPorId(aluno.getCurso()
.getId());
disciplinas = cursoSelecionado.getDisciplinas();
}
return disciplinas;
}
public void setDisciplinas(List<Disciplina> disciplinas) {
this.disciplinas = disciplinas;
}
@Transacional
public void cadastrar() {
try {
nota.setAluno(aluno);
nota.setDisciplina(disciplinaDao.buscaPorId(nota.getDisciplina()
.getId()));
validar(nota);
notaDao.adiciona(nota);
FacesContext facesContext = FacesContext.getCurrentInstance();
FacesMessage facesMessage = new FacesMessage(
"Nota cadastrada com sucesso!");
facesMessage.setSeverity(FacesMessage.SEVERITY_INFO);
facesContext.addMessage(null, facesMessage);
} catch (Exception e) {
e.printStackTrace();
FacesContext facesContext = FacesContext.getCurrentInstance();
FacesMessage facesMessage = new FacesMessage(e.getMessage());
facesMessage.setSeverity(FacesMessage.SEVERITY_ERROR);
facesContext.addMessage(null, facesMessage);
}
}
private void validar(Nota nota) throws Exception {
if (nota.getValor() == null) {
throw new Exception("Valor da nota � obrigat�rio");
}
if (nota.getValor() < 0 || nota.getValor() > 10) {
throw new Exception("Valor da nota deve ser entre 0 e 10");
}
for (Nota notaAluno : aluno.getNotas()) {
if (notaAluno.getDisciplina().equals(nota.getDisciplina())) {
if (notaAluno.getTipo().equals(nota.getTipo())) {
throw new Exception("Nota de " + nota.getTipo()
+ " para a disciplina " + notaAluno.getDisciplina()
+ " j� cadastrada");
}
}
}
}
public List<TipoNota> getTiposNota() {
return Arrays.asList(TipoNota.values());
}
public List<Nota> getNotas() {
return notaDao.buscarNotasAluno(aluno.getId());
}
public void editarNota(Nota nota) {
this.nota = nota;
}
@Transacional
public void alterar() {
try {
validar(nota);
notaDao.atualiza(nota);
FacesContext facesContext = FacesContext.getCurrentInstance();
FacesMessage facesMessage = new FacesMessage(
"Nota alterada com sucesso!");
facesMessage.setSeverity(FacesMessage.SEVERITY_INFO);
facesContext.addMessage(null, facesMessage);
} catch (Exception e) {
e.printStackTrace();
FacesContext facesContext = FacesContext.getCurrentInstance();
FacesMessage facesMessage = new FacesMessage(e.getMessage());
facesMessage.setSeverity(FacesMessage.SEVERITY_ERROR);
facesContext.addMessage(null, facesMessage);
}
}
}
答案 0 :(得分:0)
如果将<h:form></h:form>
放在模态之外,问题就会解决:
<h:form>
.... modal.....
</h:form>
根据我的模态,命令按钮应与<f:ajax></f:ajax>
一起使用,否则它将重新加载页面。