java - 未找到 JSF 方法,而应该是

标签 java jsf

 <h:form id="login_frm">
                <h2>Login:</h2>
                <table width="250" border="0" cellspacing="0" cellpadding="2">
                    <tr>
                        <td colspan="2">
                            <h:message for="leagueName" />
                            <p></p>
                            <h:message for="leagueNation" />
                            <p></p>
                            <h:message for="leagueSize" />
                            <p></p>
                            <h:message for="leagueLogo" />
                            <p></p>
                        </td>
                    </tr>

                        <td><h:outputText value="League Name : "/></td>
                        <td><h:inputText id="leagueName" value="#{DBConnection.leagueName}" required="true" requiredMessage="League Name required" /></td>

                    <tr>
                        <td><h:outputText value="League Nation : "/></td>
                        <td><h:inputText id="leagueNation" value="#{DBConnection.leagueNation}" required="true" requiredMessage="League Nation required!" /></td>
                    </tr>
                    <tr>
                        <td><h:outputText value="League Size : "/></td>
                        <td><h:inputText id="leagueSize" value="#{DBConnection.leagueSize}" required="true" requiredMessage="League Size required" /></td>
                    </tr>
                    <tr>
                        <td><h:outputText value="League Logo : "/></td>
                        <td><h:inputText id="leagueLogo" value="#{DBConnection.leagueLogo}" required="true" requiredMessage="League Logo required!" /></td>
                    </tr>
                    <tr>
                        <td colspan="2" align="center">
                            <h:commandButton actionListener="#{DBConnection.insertLeague()}" value="Insert" type="submit"/></td>
                    </tr>
                </table>
            </h:form>

这是我在 DBConnection 类中的方法

package database;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import java.sql.*;
import java.util.*;

@ManagedBean(name="DBConnection")
@RequestScoped
public class DBConnection {
    private String username;
    private String password;
    private String dbusername;

    public String getDbpassword() {
        return dbpassword;
    }
    public String getDbusername() {
        return dbusername;
    }

    private String leagueName;
    private String leagueNation;
    private int leagueSize;
    private String leagueLogo;

    public String getLeagueName() {
        return leagueName;
    }

    public void setLeagueName(String leagueName) {
        this.leagueName = leagueName;
    }

    public String getLeagueNation() {
        return leagueNation;
    }

    public void setLeagueNation(String leagueNation) {
        this.leagueNation = leagueNation;
    }

    public int getLeagueSize() {
        return leagueSize;
    }

    public void setLeagueSize(int leagueSize) {
        this.leagueSize = leagueSize;
    }

    public String getLeagueLogo() {
        return leagueLogo;
    }

    public void setLeagueLogo(String leagueLogo) {
        this.leagueLogo = leagueLogo;
    }



    private String dbpassword;
    Connection con;
    Statement ps;
    ResultSet rs;
    String SQL_Str;

    public void dbData(String UName)
    {
        try
        {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/footballdb","root","");
            ps = con.createStatement();
            SQL_Str="Select * from users where user_name like ('" + UName +"')";
            rs=ps.executeQuery(SQL_Str);
            rs.next();
            dbusername=rs.getString(2).toString();
            dbpassword=rs.getString(4).toString();
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
            System.out.println("Exception Occur :" + ex);
        }
    }
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }
    public String checkValidUser()
    {
        dbData(username);

        if(username.equalsIgnoreCase(dbusername))
        {

            if(password.equals(dbpassword))
                return "home.xhtml";
            else
            {
                return "invalid";
            }
        }
        else
        {
            return "invalid";
        }
    }


    List<Player> players = new ArrayList<Player>();

    public List<Player> getPlayers() {
        players = displayPlayers();
        return players;
    }

    public void setPlayers(List<Player> players) {
        this.players = players;
    }

    public String insertLeague(String leagueName, String leagueNation, String leagueSize, String leagueLogo) {
        try
        {
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/footballdb","root","");
            ps = con.createStatement();
            SQL_Str="INSERT INTO league(LeagueName, LeagueNation, LeagueSize, LeagueLogo) VALUES('"+leagueName+"', '"+leagueNation+"','"+leagueSize+"','"+leagueLogo+"')";
            ps.executeQuery(SQL_Str);
            return "displayLeagues.xhtml";
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
            System.out.println("Exception Occur :" + ex);
            return "displayLeagues.xhtml";
        }
    }
}

正如您所看到的,我的类中确实有该方法,我拥有所有必需的 getter 和 setter 方法,并且我已经在 insertLeague 页面上尝试了操作和 actionListener。任何人都知道为什么会发生这种情况,这真的让我很困惑。

我还尝试过引用带括号和不带括号的方法

堆栈跟踪: https://pastebin.com/613dJYUp

最佳答案

您应该将参数传递给 insertLeague 方法,如下所示:

#{DBConnection.insertLeague(DBConnection.leagueName, DBConnection.leagueNation, DBConnection.leagueSize, DBConnection.leagueLogo)}

对于更新查询,您应该使用 executeUpdate() 而不是 executeQuery()

关于java - 未找到 JSF 方法,而应该是,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53661166/

相关文章:

java - 相同的代码块执行时长不同

jsf - 防止双重提交的纯 Java/JSF 实现

java - 如何从 java 或 jsf 中的字符串中获取所需的子字符串?

java - 如何防止显示 p :dialog when no row selected in p:datatable?

java - 我通过 Java 连接到 MySQL,但它不正确。谁能帮我?

java - 同步方法是否在与 UI 线程 (Android) 不同的线程上运行?

java - 如何仅将 Json 字符串的相关部分转换为 Set?

java - package-info.java 包注释影响所有类,包括子包中的类

jsf - 使用 twitter bootstrap 的单选按钮 - 无法获取值

jsf - 为什么即使在服务器重新启动后我的应用程序的值仍会保留?