Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jsf 如何限制未登录用户的CRUD功能?_Jsf_Jakarta Ee_Netbeans_Crud - Fatal编程技术网

Jsf 如何限制未登录用户的CRUD功能?

Jsf 如何限制未登录用户的CRUD功能?,jsf,jakarta-ee,netbeans,crud,Jsf,Jakarta Ee,Netbeans,Crud,我按照本教程编写了一个CRUD应用程序: 以下是我的ManagedBean: package model_controller; import com.sun.net.httpserver.HttpsServer; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLE

我按照本教程编写了一个CRUD应用程序: 以下是我的ManagedBean:

package model_controller;

import com.sun.net.httpserver.HttpsServer;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.inject.Named;
import javax.enterprise.context.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;


@Named(value = "studentManagedBean")
@RequestScoped
public class StudentManagedBean {

private int id, wiek;
private String nazwisko, email, adres;

public StudentManagedBean() {
}

public StudentManagedBean(int id, int wiek, String nazwisko, String email, String adres) {
    //konstruktory
    this.id = id;
    this.wiek = wiek;
    this.nazwisko = nazwisko;
    this.email = email;
    this.adres = adres;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public int getWiek() {
    return wiek;
}

public void setWiek(int wiek) {
    this.wiek = wiek;
}

public String getNazwisko() {
    return nazwisko;
}

public void setNazwisko(String nazwisko) {
    this.nazwisko = nazwisko;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getAdres() {
    return adres;
}

public void setAdres(String adres) {
    this.adres = adres;
}

//
public static Connection conn = null;
public static PreparedStatement pstmt = null;
public static ResultSet rs = null;
private String str = "";

//
public static Connection getConnection() {

    try {
        Class.forName("com.mysql.jdbc.Driver");
        //Alt+enter
        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/studenci?zeroDateTimeBehavior=convertToNull", "root", "");
    } catch (ClassNotFoundException ex) {
        Logger.getLogger(StudentManagedBean.class.getName()).log(Level.SEVERE, null, ex);
    } catch (SQLException ex) {
        Logger.getLogger(StudentManagedBean.class.getName()).log(Level.SEVERE, null, ex);
    }
    return conn;
}

public static void closeAll(Connection conn, PreparedStatement pstmt, ResultSet rs) {

    if (conn != null) {
        try {
            conn.close();
        } catch (SQLException ex) {
            Logger.getLogger(StudentManagedBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    if (pstmt != null) {
        try {
            pstmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(StudentManagedBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    if (rs != null) {
        try {
            rs.close();
        } catch (SQLException ex) {
            Logger.getLogger(StudentManagedBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}


public ArrayList<StudentManagedBean> GetAllStudent() {

    ArrayList<StudentManagedBean> arr = new ArrayList<>();
    str = "SELECT s.id, s.nazwisko, s.wiek, s.adres, s.email FROM student s";
    getConnection();
    try {
        pstmt = conn.prepareStatement(str);
        rs = pstmt.executeQuery();
        while (rs.next()) {

            StudentManagedBean st = new StudentManagedBean();
            st.setId(rs.getInt("id"));
            st.setNazwisko(rs.getString("nazwisko"));
            st.setWiek(rs.getInt("wiek"));
            st.setAdres(rs.getString("adres"));
            st.setEmail(rs.getString("email"));
            //
            arr.add(st);
        }
    } catch (SQLException ex) {
        Logger.getLogger(StudentManagedBean.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        closeAll(conn, pstmt, rs);
    }
    return arr;

}

public void add() {

    getConnection();
    str = "insert into student(nazwisko, wiek, adres, email) values(?,?,?,?)";
    try {
        pstmt = conn.prepareStatement(str);
        pstmt.setString(1, this.getNazwisko());
        pstmt.setInt(2, this.getWiek());
        pstmt.setString(3, this.getAdres());
        pstmt.setString(4, this.getEmail());
        int executeUpdate = pstmt.executeUpdate();
        if (executeUpdate > 0) {

            System.out.println("Zaktualizowano dane");
        }
    } catch (SQLException ex) {
        Logger.getLogger(StudentManagedBean.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        closeAll(conn, pstmt, rs);
    }
}

public void Edit() {

    ArrayList<StudentManagedBean> arrList = GetAllStudent();
    FacesContext fc = FacesContext.getCurrentInstance();
//        Map<String,String> mapParam = fc.getExternalContext().getInitParameterMap();
//        idStudent = mapParam.get("id");
    int idStudent;
    HttpServletRequest request = (HttpServletRequest) fc.getExternalContext().getRequest();
    idStudent = Integer.parseInt(request.getParameter("id"));
    //
    for (StudentManagedBean studentManagedBean : arrList) {

        if (studentManagedBean.getId() == idStudent) {

            this.setId(studentManagedBean.getId());//błąd
            this.setNazwisko(studentManagedBean.getNazwisko());
            this.setWiek(studentManagedBean.getWiek());
            this.setAdres(studentManagedBean.getAdres());
            this.setEmail(studentManagedBean.getEmail());
        }
    }

    setId(idStudent);
}

public void update() {

    getConnection();
    str = "update student set nazwisko=?, wiek=?, adres=?, email=? where id=?";
    //        Map<String,String> mapParam = fc.getExternalContext().getInitParameterMap();
    //        idStudent = mapParam.get("id");
    FacesContext fc = FacesContext.getCurrentInstance();

    HttpServletRequest request = (HttpServletRequest) fc.getExternalContext().getRequest();
    int idStudent = Integer.parseInt(request.getParameter("id"));

    try {
        pstmt = conn.prepareStatement(str);
        pstmt.setString(1, this.getNazwisko());
        pstmt.setInt(2, this.getWiek());
        pstmt.setString(3, this.getAdres());
        pstmt.setString(4, this.getEmail());
        pstmt.setInt(5, idStudent);

        System.out.println(getNazwisko());
        int executeUpdate = pstmt.executeUpdate();
        if (executeUpdate > 0) {

            System.out.println("Zaktualizowano dane");

        }
    } catch (SQLException ex) {
        Logger.getLogger(StudentManagedBean.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        closeAll(conn, pstmt, rs);
    }
}

public void delete() {

    getConnection();
    str = "DELETE FROM student where id=?";
    FacesContext fc = FacesContext.getCurrentInstance();
    HttpServletRequest request = (HttpServletRequest) fc.getExternalContext().getRequest();
    int idStudent = Integer.parseInt(request.getParameter("id"));

    try {
        pstmt = conn.prepareStatement(str);
        pstmt.setInt(1, idStudent);
        int executeUpdate = pstmt.executeUpdate();
        if (executeUpdate > 0) {
            System.out.println("Usunięto dane");
        }
    } catch (SQLException ex) {
        Logger.getLogger(StudentManagedBean.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        closeAll(conn, pstmt, rs);
    }
}
}
package model\u控制器;
导入com.sun.net.httpserver.HttpsServer;
导入java.sql.Connection;
导入java.sql.DriverManager;
导入java.sql.PreparedStatement;
导入java.sql.ResultSet;
导入java.sql.SQLException;
导入java.util.ArrayList;
导入java.util.Map;
导入java.util.logging.Level;
导入java.util.logging.Logger;
导入javax.inject.Named;
导入javax.enterprise.context.RequestScope;
导入javax.faces.context.FacesContext;
导入javax.servlet.http.HttpServletRequest;
@命名(value=“studentManagedBean”)
@请求范围
公共班级学生管理Bean{
私有int-id,wiek;
私人字符串nazwisko,电子邮件,地址;
公共学生管理Bean(){
}
public StudentManagedBean(整数id、整数wiek、字符串nazwisko、字符串电子邮件、字符串地址){
//结构理论
this.id=id;
this.wiek=wiek;
this.nazwisko=nazwisko;
this.email=电子邮件;
this.adres=adres;
}
公共int getId(){
返回id;
}
公共无效集合id(内部id){
this.id=id;
}
公共int getWiek(){
返回wiek;
}
公共无效setWiek(int wiek){
this.wiek=wiek;
}
公共字符串getNazwisko(){
返回纳兹维斯科;
}
公共无效setNazwisko(字符串nazwisko){
this.nazwisko=nazwisko;
}
公共字符串getEmail(){
回复邮件;
}
公用电子邮件(字符串电子邮件){
this.email=电子邮件;
}
公共字符串getAdres(){
返回地址;
}
公共无效设置地址(字符串地址){
this.adres=adres;
}
//
公共静态连接conn=null;
public static PreparedStatement pstmt=null;
公共静态结果集rs=null;
私有字符串str=“”;
//
公共静态连接getConnection(){
试一试{
Class.forName(“com.mysql.jdbc.Driver”);
//Alt+enter
conn=DriverManager.getConnection(“jdbc:mysql://localhost:3306/studenci?zeroDateTimeBehavior=convertToNull“,”根“,”);
}捕获(ClassNotFoundException ex){
Logger.getLogger(StudentManagedBean.class.getName()).log(Level.SEVERE,null,ex);
}catch(SQLException-ex){
Logger.getLogger(StudentManagedBean.class.getName()).log(Level.SEVERE,null,ex);
}
返回连接;
}
公共静态void closeAll(连接连接、准备语句pstmt、结果集rs){
如果(conn!=null){
试一试{
康涅狄格州关闭();
}catch(SQLException-ex){
Logger.getLogger(StudentManagedBean.class.getName()).log(Level.SEVERE,null,ex);
}
}
如果(pstmt!=null){
试一试{
pstmt.close();
}catch(SQLException-ex){
Logger.getLogger(StudentManagedBean.class.getName()).log(Level.SEVERE,null,ex);
}
}
如果(rs!=null){
试一试{
rs.close();
}catch(SQLException-ex){
Logger.getLogger(StudentManagedBean.class.getName()).log(Level.SEVERE,null,ex);
}
}
}
公共数组列表GetAllStudent(){
ArrayList arr=新的ArrayList();
str=“从学生s处选择s.id、s.nazwisko、s.wiek、s.Address、s.email”;
getConnection();
试一试{
pstmt=连接准备状态(str);
rs=pstmt.executeQuery();
while(rs.next()){
StudentManagedBean st=新的StudentManagedBean();
圣塞提德(rs.getInt(“id”));
圣塞特纳兹维斯科(rs.getString(“纳兹维斯科”);
圣塞特威克(rs.getInt(“威克”));
圣塞塔迪斯(rs.getString(“adres”);
st.setEmail(rs.getString(“电子邮件”);
//
地址(st);
}
}catch(SQLException-ex){
Logger.getLogger(StudentManagedBean.class.getName()).log(Level.SEVERE,null,ex);
}最后{
关闭所有(conn、pstmt、rs);
}
返回arr;
}
公共无效添加(){
getConnection();
str=“插入学生(nazwisko、wiek、adres、电子邮件)值(?,,?)”;
试一试{
pstmt=连接准备状态(str);
pstmt.setString(1,this.getNazwisko());
pstmt.setInt(2,this.getWiek());
pstmt.setString(3,this.getAdres());
pstmt.setString(4,this.getEmail());
int executeUpdate=pstmt.executeUpdate();
如果(执行更新>0){
System.out.println(“Zaktalizowano-dane”);
}
}catch(SQLException-ex){
Logger.getLogger(StudentManagedBean.class.getName()).log(Level.SEVERE,null,ex);
}最后{
关闭所有(conn、pstmt、rs);
}
}
公共作废编辑(){
ArrayList arrList=GetAllStudent();
FacesContext fc=FacesContext.getCurrentInstance();
//Map mapParam=fc.getExternalContext().getInitParameterMap();
//idStudent=mapParam.get(“id”);
国际学生;
HttpServletRequest=(HttpServletRequest)fc.getExternalContext().getRequest();
idStudent=Integer.parseInt(request.getParameter(“id”);
//
for(StudentManagedBean StudentManagedBean:arrList){
if(studentManagedBean.getId()==idStudent){
this.setId(studentManagedBean.getId());//błd
this.setNazwisko(studentManagedBean.getNazwisko());
this.setWiek(studentManagedBean.getWiek());
this.setAdres(studentManagedBean.getAdres());
this.setEmail(studentManagedBean.getEmail());
}
}
setId(idStudent);
}
公共无效更新(){
getConnection();
str=“更新学生集nazwisko=?,wiek=?,adres=?,email=?其中id=?”;
//Map mapParam=fc.getExternalContext().getInitParameterMap();
//idStudent=mapParam.get(“id”);
FacesContext fc=FacesContext.getCurrentInstance();
HttpServletRequest=(HttpServletRequest)fc.getExternalContext().getRequest();
int idStudent=Integer.parseInt(request.getParameter(“id”);
试一试{
pstmt=连接准备状态(str);
pstmt.setString(1,this.getNazwisko());
pstmt.setInt(2,this.getWiek());
pstmt.setString(3,this.getAd
// Your form for inserting data to the database
<h:form rendered="#{studentManagedBean.isLoggedIn}">
  ...
</h:form>

// delete button
<h:commandButton action="#{studentManagedBean.delete()}" 
   rendered="#{studentManagedBean.isLoggedIn}" />
private boolean loggedIn;
// getter and setter

public boolean isLoggedIn(){
    return loggedIn;
}