命令按钮在 JSF 1.2 中的数据表中不起作用
command button not working inside datatable in JSF 1.2
我是 JSF 1.2 的新手,在我的办公室里,我只能使用 JSF 1.2,而不是任何更高版本。当我在 .jspx 文件中创建数据表时,数据正在显示,但是当我添加任何内部数据表时,该按钮不起作用。有人说您必须设置要查看的范围,但在 JSF 1.2 中没有任何视图范围。谁能帮我解决这个问题?
这是在 Eclipse IDE 中为 Web 开发人员使用 JSF 1.2._07 和 Tomcat 7.
View.jspx
<?xml version="1.0" encoding="ISO-8859-1" ?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" version="2.0">
<jsp:directive.page language="java"
contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" />
<jsp:text>
<![CDATA[ <?xml version="1.0" encoding="ISO-8859-1" ?> ]]>
</jsp:text>
<jsp:text>
<![CDATA[ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> ]]>
</jsp:text>
<html xmlns="http://www.w3.org/1999/xhtml">
**<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>View Data</title>
</head>
<body>
<f:view>
<table border="2" cellpadding = "10" cellspacing = "1">
<tr>
<td><f:facet name="header">ID</f:facet></td>
<td><f:facet name="header">Roll No</f:facet></td>
<td><f:facet name="header">Name</f:facet></td>
<td><f:facet name="header">Email</f:facet></td>
<td><f:facet name="header">Phone</f:facet></td>
<td><f:facet name="header">Username</f:facet></td>
<td><f:facet name="header">Password</f:facet></td>
</tr>
</table>
<h:dataTable binding="#{table}" var="data" value="#{studentBean.dataFromDB}" border="2" cellpadding = "10" cellspacing = "1">
<h:column >
<h:outputText value=" #{data.id}"></h:outputText>
</h:column>
<h:column >
<h:outputText value=" #{data.roll}"></h:outputText>
</h:column>
<h:column >
<h:outputText value=" #{data.name}"></h:outputText>
</h:column>
<h:column >
<h:outputText value=" #{data.email}"></h:outputText>
</h:column>
<h:column >
<h:outputText value=" #{data.phone}"></h:outputText>
</h:column>
<h:column >
<h:outputText value=" #{data.username}"></h:outputText>
</h:column>
<h:column >
<h:outputText value=" #{data.password}"></h:outputText>
</h:column>
<h:column>
<h:commandButton value="Update" action="#{studentBean.backLogin}"></h:commandButton>
</h:column>
</h:dataTable>
</f:view>
</body>
</html>
</jsp:root>
StudentBean.java
package com.crud.jsf;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import com.crud.service.ConnectionClass;
public class StudentBean {
private int id;
private String roll,name,email,phone,username,password;
private List dataFromDB;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getRoll() {
return roll;
}
public void setRoll(String roll) {
this.roll = roll;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public List getDataFromDB() {
return dataFromDB;
}
ConnectionClass cc = new ConnectionClass();
@PostConstruct
public void init()
{
dataFromDB = cc.getUserList();
}
public String login()
{
boolean valid = cc.validate(username, password);
if(valid)
{
return "view";
}
else
{
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN,
"Incorrect username or password","Please enter correct Username and Password!!"));
return "login";
}
}
public String register()
{
return "register";
}
public String addUser(StudentBean S)
{
boolean added = cc.add(S);
if(added)
{
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, new FacesMessage("Registered Successfull!!"));
return "login";
}
else
{
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN,
"Error","Please enter all the details!!"));
return "register";
}
}
public String backLogin()
{
return "login";
}
public String delete(int id)
{
System.out.println(id);
boolean valid = cc.delete(id);
if(valid)
{
return "view";
}
else
{
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN,
"Error","Please try again!!!"));
return "error";
}
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>CrudJSF</display-name>
<welcome-file-list>
<welcome-file>faces/login.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<context-param>
<description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>resources.application</param-value>
</context-param>
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
</web-app>
面孔配置
<?xml version="1.0" encoding="UTF-8"?>
<faces-config
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd"
version="1.2">
<managed-bean>
<display-name>Student</display-name>
<managed-bean-name>studentBean</managed-bean-name>
<managed-bean-class>com.crud.jsf.StudentBean</managed-bean-class>
<managed-bean-scope>application</managed-bean-scope>
</managed-bean>
<navigation-rule>
<display-name>Login</display-name>
<from-view-id>/login.jsp</from-view-id>
<navigation-case>
<from-outcome>view</from-outcome>
<to-view-id>/view.jspx</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>login</from-outcome>
<to-view-id>/login.jsp</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>register</from-outcome>
<to-view-id>/register.jsp</to-view-id>
</navigation-case>
</navigation-rule>
<navigation-rule>
<display-name>Register</display-name>
<from-view-id>/register.jsp</from-view-id>
<navigation-case>
<from-outcome>login</from-outcome>
<to-view-id>/login.jsp</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>register</from-outcome>
<to-view-id>/register.jsp</to-view-id>
</navigation-case>
</navigation-rule>
<navigation-rule>
<display-name>View</display-name>
<from-view-id>/view.jspx</from-view-id>
<navigation-case>
<from-outcome>view</from-outcome>
<to-view-id>/view.jspx</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>error</from-outcome>
<to-view-id>/view.jspx</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>login</from-outcome>
<to-view-id>/login.jsp</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>
首先,我将范围设置为会话,然后我尝试使其成为应用程序,但仍然得到相同的结果,因为我的 h:commandbutton 无法正常工作....!
Output
我使用用户命令 Link 代替命令按钮将 ID 传递给 Bean Class 并获得所需的输出。
<h:commandLink id="deleteLink" action="#{studentBean.delete}">
<h:outputText value="Delete"></h:outputText>
<f:param name="id" value="#{data.id}"></f:param>
我是 JSF 1.2 的新手,在我的办公室里,我只能使用 JSF 1.2,而不是任何更高版本。当我在 .jspx 文件中创建数据表时,数据正在显示,但是当我添加任何内部数据表时,该按钮不起作用。有人说您必须设置要查看的范围,但在 JSF 1.2 中没有任何视图范围。谁能帮我解决这个问题?
这是在 Eclipse IDE 中为 Web 开发人员使用 JSF 1.2._07 和 Tomcat 7.
View.jspx
<?xml version="1.0" encoding="ISO-8859-1" ?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" version="2.0">
<jsp:directive.page language="java"
contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" />
<jsp:text>
<![CDATA[ <?xml version="1.0" encoding="ISO-8859-1" ?> ]]>
</jsp:text>
<jsp:text>
<![CDATA[ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> ]]>
</jsp:text>
<html xmlns="http://www.w3.org/1999/xhtml">
**<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>View Data</title>
</head>
<body>
<f:view>
<table border="2" cellpadding = "10" cellspacing = "1">
<tr>
<td><f:facet name="header">ID</f:facet></td>
<td><f:facet name="header">Roll No</f:facet></td>
<td><f:facet name="header">Name</f:facet></td>
<td><f:facet name="header">Email</f:facet></td>
<td><f:facet name="header">Phone</f:facet></td>
<td><f:facet name="header">Username</f:facet></td>
<td><f:facet name="header">Password</f:facet></td>
</tr>
</table>
<h:dataTable binding="#{table}" var="data" value="#{studentBean.dataFromDB}" border="2" cellpadding = "10" cellspacing = "1">
<h:column >
<h:outputText value=" #{data.id}"></h:outputText>
</h:column>
<h:column >
<h:outputText value=" #{data.roll}"></h:outputText>
</h:column>
<h:column >
<h:outputText value=" #{data.name}"></h:outputText>
</h:column>
<h:column >
<h:outputText value=" #{data.email}"></h:outputText>
</h:column>
<h:column >
<h:outputText value=" #{data.phone}"></h:outputText>
</h:column>
<h:column >
<h:outputText value=" #{data.username}"></h:outputText>
</h:column>
<h:column >
<h:outputText value=" #{data.password}"></h:outputText>
</h:column>
<h:column>
<h:commandButton value="Update" action="#{studentBean.backLogin}"></h:commandButton>
</h:column>
</h:dataTable>
</f:view>
</body>
</html>
</jsp:root>
StudentBean.java
package com.crud.jsf;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import com.crud.service.ConnectionClass;
public class StudentBean {
private int id;
private String roll,name,email,phone,username,password;
private List dataFromDB;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getRoll() {
return roll;
}
public void setRoll(String roll) {
this.roll = roll;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public List getDataFromDB() {
return dataFromDB;
}
ConnectionClass cc = new ConnectionClass();
@PostConstruct
public void init()
{
dataFromDB = cc.getUserList();
}
public String login()
{
boolean valid = cc.validate(username, password);
if(valid)
{
return "view";
}
else
{
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN,
"Incorrect username or password","Please enter correct Username and Password!!"));
return "login";
}
}
public String register()
{
return "register";
}
public String addUser(StudentBean S)
{
boolean added = cc.add(S);
if(added)
{
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, new FacesMessage("Registered Successfull!!"));
return "login";
}
else
{
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN,
"Error","Please enter all the details!!"));
return "register";
}
}
public String backLogin()
{
return "login";
}
public String delete(int id)
{
System.out.println(id);
boolean valid = cc.delete(id);
if(valid)
{
return "view";
}
else
{
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN,
"Error","Please try again!!!"));
return "error";
}
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>CrudJSF</display-name>
<welcome-file-list>
<welcome-file>faces/login.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<context-param>
<description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>resources.application</param-value>
</context-param>
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
</web-app>
面孔配置
<?xml version="1.0" encoding="UTF-8"?>
<faces-config
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd"
version="1.2">
<managed-bean>
<display-name>Student</display-name>
<managed-bean-name>studentBean</managed-bean-name>
<managed-bean-class>com.crud.jsf.StudentBean</managed-bean-class>
<managed-bean-scope>application</managed-bean-scope>
</managed-bean>
<navigation-rule>
<display-name>Login</display-name>
<from-view-id>/login.jsp</from-view-id>
<navigation-case>
<from-outcome>view</from-outcome>
<to-view-id>/view.jspx</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>login</from-outcome>
<to-view-id>/login.jsp</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>register</from-outcome>
<to-view-id>/register.jsp</to-view-id>
</navigation-case>
</navigation-rule>
<navigation-rule>
<display-name>Register</display-name>
<from-view-id>/register.jsp</from-view-id>
<navigation-case>
<from-outcome>login</from-outcome>
<to-view-id>/login.jsp</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>register</from-outcome>
<to-view-id>/register.jsp</to-view-id>
</navigation-case>
</navigation-rule>
<navigation-rule>
<display-name>View</display-name>
<from-view-id>/view.jspx</from-view-id>
<navigation-case>
<from-outcome>view</from-outcome>
<to-view-id>/view.jspx</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>error</from-outcome>
<to-view-id>/view.jspx</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>login</from-outcome>
<to-view-id>/login.jsp</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>
首先,我将范围设置为会话,然后我尝试使其成为应用程序,但仍然得到相同的结果,因为我的 h:commandbutton 无法正常工作....! Output
我使用用户命令 Link 代替命令按钮将 ID 传递给 Bean Class 并获得所需的输出。
<h:commandLink id="deleteLink" action="#{studentBean.delete}">
<h:outputText value="Delete"></h:outputText>
<f:param name="id" value="#{data.id}"></f:param>