命令按钮在JSF 1.2中的数据表内部不起作用

时间:2018-12-20 11:44:27

标签: jsf datatable jsf-1.2

我是JSF 1.2的新手,在我的办公室中,我只需要使用JSF 1.2,而无需使用任何更高版本。当我在.jspx文件中创建数据表时,将显示数据,但是当我在数据表中添加任何内容时,该按钮将不起作用。有人说您必须将范围设置为视图,但是在JSF 1.2中没有任何视图范围。有人可以帮我吗?

这是在带有JSF 1.2._07和Tomcat 7的Web Developer的Eclipse IDE中。

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

1 个答案:

答案 0 :(得分:0)

我有用户Command Link而不是Command按钮,用于将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>