我对InputText
有疑问。单击按钮后尝试获取InputText
值时,值为null
。
当我创建登录页面时,没有问题但使用DataTable会有问题。
package controller;
import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import model.Groups;
import model.Student;
import util.DataHolder;
@ManagedBean(name = "edit", eager = true)
@SessionScoped
@RequestScoped
public class EditController implements Serializable {
private static final long serialVersionUID = 1L;
private List<Student> studentList;
private Groups group;
@PostConstruct
public void init() {
this.group = DataHolder.getGroup();
}
public List<Student> getStudentList() {
studentList = group.getStudents();
return studentList;
}
public void saveGrade(Student student) {
System.out.println(student.getName() + " " + student.getSurname() + "grade =" + student.getGrade());
}
public Groups getGroup() {
return group;
}
public void setGroup(Groups group) {
this.group = group;
}
public void setStudentList(List<Student> studentList) {
this.studentList = studentList;
}
}
<?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">
<h:head>
<title>Edit Page</title>
<link rel="stylesheet"
href="https://cdn.datatables.net/1.10.11/css/jquery.dataTables.min.css"></link>
<script src="https://code.jquery.com/jquery-1.12.0.min.js"></script>
<script
src="https://cdn.datatables.net/1.10.11/js/jquery.dataTables.min.js"></script>
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"></link>
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css"></link>
<script>
$(document).ready(function() {
$('#example').DataTable();
});
</script>
<h:body>
<div class="container">
<h:dataTable value="#{edit.studentList}" var="student"
styleClass="table table-striped table-bordered" id="example">
<h:column headerText="Student">
<f:facet name="header">
<h:outputLabel>Student</h:outputLabel>
</f:facet>
<h:outputText value="#{edit.studentList.indexOf(student) + 1}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputLabel>Number</h:outputLabel>
</f:facet>
<h:outputText value="#{student.no}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputLabel>Name</h:outputLabel>
</f:facet>
<h:outputText value="#{student.name}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputLabel>Surname</h:outputLabel>
</f:facet>
<h:outputText value="#{student.surname}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputLabel>Grade</h:outputLabel>
</f:facet>
<h:column>
<h:form>
<h:inputText value="#{student.grade}" class="form-control"></h:inputText>
</h:form>
</h:column>
</h:column>
<h:column>
<h:form>
<h:commandButton class="btn btn-danger btn-sm" value="Save"
action="#{edit.saveGrade(student)}" />
</h:form>
</h:column>
<h:column>
<h:form>
<h:commandButton class="btn btn-danger btn-sm" value="Delete"
action="#{edit.deleteStudent(student)}" />
</h:form>
</h:column>
</h:dataTable>
</div>
</h:body>
</html>