谁有JSF实现添加 删除 修改 的程序!!  给我一份 [email protected]谢谢~~~

解决方案 »

  1.   

    哪本jsf的书上增删改查都应该是基础的例子吧?
      

  2.   

    <%@page contentType="text/html; charset=GBK"%>
    <%@taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
    <%@taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
    <html>
    <head>
    <title>reg</title>
    </head>
    <body bgcolor="#ffffff">
    <h1>注册</h1>
    <f:view>
      <h:form id="regForm">
        <h:outputLabel value="姓名" for="name">    </h:outputLabel>
        <h:inputText id="name" value="#{UserBean.name}" required="true">    </h:inputText>
        <h:message for="name">    </h:message>
        <p>    </p>
        <h:outputLabel value="年龄" for="age">    </h:outputLabel>
        <h:inputText id="age" value="#{UserBean.age}" required="true">    </h:inputText>
        <h:message for="age">    </h:message>
        <p></p>
        <h:commandButton id="submit" action="#{UserBean.add}" value="提交">    </h:commandButton>
      </h:form>
    </f:view>
    </body>
    </html>
    ========================================package jsforacle;
    import java.util.ArrayList;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.sql.ResultSet;
    import java.sql.Statement;
    import java.sql.PreparedStatement;public class UserBean {
        private String name;
        private int age;
        private int id;    private Connection getCon(){
            Connection con = null;
            String user = "magus";
            String password = "magus";
            try {
                Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
                con = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:magus", user, password);
            } catch (ClassNotFoundException ex) {
                System.out.println(ex);
            } catch (IllegalAccessException ex) {
                System.out.println(ex);
            } catch (InstantiationException ex) {
                System.out.println(ex);
            } catch (SQLException e) {
                System.out.println(e);
            }
            return con;
        }    private int getMaxId(){
            int maxId = 1;
            Connection con = getCon();
            ResultSet rs = null;
            Statement stmt = null;
            String sql = "select max(id) as maxid from users";
            try {
                stmt = con.createStatement();
                rs = stmt.executeQuery(sql);
                if(rs.next()){
                    maxId = rs.getInt("maxid");
                }
            } catch (SQLException ex) {
                System.out.println(ex);
            }finally{
                try {
                    con.close();
                } catch (SQLException ex1) {
                    System.out.println(ex1);
                }
            }
            return maxId;
        }    public String add() {
            Connection con = getCon();
            try {
                PreparedStatement pstmt = con.prepareStatement(
                        "insert into users values (?,?,?)");
                pstmt.setString(1,name);
                pstmt.setInt(2,age);
                pstmt.setInt(3,getMaxId()+1);
                pstmt.executeUpdate();
                return "success";
            } catch (SQLException ex) {
                System.out.println(ex);
                return "fales";
            } finally {
                try {
                    con.close();
                } catch (SQLException ex1) {
                    System.out.println(ex1);
                }
            }
        }    public UserBean() {
        }    public void setName(String name) {
            this.name = name;
        }    public void setAge(int age) {
            this.age = age;
        }    public void setId(int id) {
            this.id = id;
        }    public String getName() {
            return name;
        }    public int getAge() {
            return age;
        }    public int getId() {
            return id;
        }
    }==========================================<?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE faces-config PUBLIC "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN" "http://java.sun.com/dtd/web-facesconfig_1_1.dtd"><faces-config xmlns="http://java.sun.com/JSF/Configuration">
      <managed-bean>
        <managed-bean-name>UserBean</managed-bean-name>
        <managed-bean-class>jsforacle.UserBean</managed-bean-class>
        <managed-bean-scope>session</managed-bean-scope>
      </managed-bean>
      <navigation-rule>
        <from-view-id>/reg.jsp</from-view-id>
        <navigation-case>
          <from-outcome>success</from-outcome>
          <to-view-id>/result.jsp</to-view-id>
        </navigation-case>
      </navigation-rule>
      <navigation-rule>
        <from-view-id>/reg.jsp</from-view-id>
        <navigation-case>
          <from-outcome>false</from-outcome>
          <to-view-id>/result2.jsp</to-view-id>
        </navigation-case>
      </navigation-rule>
    </faces-config>===========================================<?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
      <display-name>WebModule1</display-name>
      <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>
    </web-app>
      

  3.   

    实现其他的功能无非就是在受管理bean里再添加 del , update, select 的方法,都是 jdbc 基础,搂主不会这么懒吧?