我需要通过托管bean(bean.java)将值(studentId)从jsf视图(list.xhtml)传递到另一个jsf页面(editor.xhtml)。我可以在bean页面中获取值,但我无法访问编辑器中的值。你能告诉我出了什么问题吗? 希望听到一些建议
list.xhtml
<h:dataTable value="#{student.createList()}" var="student" styleClass="studentTable"
columnClasses=",,,fixedWidth" border="1" cellspacing="2" cellpadding="2">
<h:column>
<f:facet name="header">Student ID</f:facet>
<h:outputText value="#{student.studentId}"></h:outputText>
</h:column>
<h:column>
<f:facet name="header">Name</f:facet>
<h:outputText value="#{student.fname}"></h:outputText>
</h:column>
<h:column>
<f:facet name="header">Surname</f:facet>
<h:outputText value="#{student.lname}"></h:outputText>
</h:column>
<h:commandLink actionListener="#{student.edit()}" value ="Editt">
<f:param name="id" value="#{student.studentId}"/>
</h:commandLink>
editor.xhtml
<h:panelGrid border="" cellpadding="20" cellspacing="20" columns="3">
<h:outputLabel value="Student Id" />
<h:inputText value="#{student.studentId}" readonly="true" required="true" requiredMessage="Id is Required" id="Eid">
</h:inputText> <h:message for="Eid"/>
<h:outputLabel value="Student Name" />
<h:inputText value="#{student.fname}" required="true" requiredMessage="Name is Required" id="Ename">
</h:inputText> <h:message for="Ename"/>
<h:outputLabel value="Student Age" />
<h:inputText value="#{student.lname}" required="true" requiredMessage="age is Required" id="Eage">
<h:commandLink value="Update" action="userRecords" actionListener="#{student.update()}">
</h:commandLink>
student.java
public void edit(){ System.out.println("called from edit()"+ this.StudentId); List<Student> arrList = createList(); FacesContext fc = FacesContext.getCurrentInstance(); mapParam=fc.getExternalContext().getInitParameterMap(); String idStudent; HttpServletRequest request = (HttpServletRequest) fc.getExternalContext().getRequest(); for (Student studentManagedBean : arrList) { if (studentManagedBean.getStudentId() == idStudent) { this.setStudentId(studentManagedBean.getStudentId());//Error this.setFname(studentManagedBean.getFname()); this.setLname(studentManagedBean.getLname()); } System.out.println(" we have "+ this.getStudentId() + this.getFname() ); } }
public void update() {
Connection conn=null;
try{
Class.forName("org.apache.derby.jdbc.ClientDriver");
conn = DriverManager.getConnection("jdbc:derby://localhost:1527/XX","XX","XXX");
}catch(Exception e){
}
String str = "Update student set FNAME=?,LNAME=? where STUDENTID=?";
FacesContext fc = FacesContext.getCurrentInstance();
HttpServletRequest request = (HttpServletRequest) fc.getExternalContext().getRequest();
int idStudent = Integer.parseInt(request.getParameter("id"));
try {
PreparedStatement pstmt = conn.prepareStatement(str);
pstmt.setString(1, this.getFname());
pstmt.setString(2, this.getLname());
pstmt.setInt(3, idStudent);
// System.out.println("Id Student Update :" + idStudent);//Error
int executeUpdate = pstmt.executeUpdate();
if (executeUpdate > 0) {
System.out.println("Update SuccessFully");
}
} catch (SQLException ex) {
Logger.getLogger(Student.class.getName()).log(Level.SEVERE, null, ex);
} finally {
}