ssh小例で登録を実現する


今日の内容はsshフレームを使って登録を実現する機能です.springはspring-42.5のバージョンを使用しています.struts 2はstruts-23.4.1のバージョンを使用しています.1、まずデータベーステーブルを作成します.3、必要なjarバッグをロードします.spring(初心者は、すべてのreleaseのjarを案内します):hibernate(requiredフォルダにあるすべてのjarパッケージを追加しました):
struts 2(springとstruts 2のサポートパッケージを含む):4、web.xmlを修正します.strutsとspringを配置します.内容は以下の通りです.


    
    
        struts2   org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
   
   
   
       struts2
       /*
    
    
    
     
     
        org.springframework.web.context.ContextLoaderListener
         
     
    
            contextConfigLocation
            
            
                /WEB-INF/classes/applicationContext.xml
            
    

5、srcディレクトリの下でhibernate.cfg.xmlを作成し、データベースに接続するために使用します.

    
        
        com.mysql.jdbc.Driver
        jdbc:mysql://localhost:3306/book
        root
        sll
        org.hibernate.dialect.MySQLDialect
        
        true
        
    
6、User.hbm.xmlを作成します.私が置いているディレクトリはsl/hibernate/modelです.User.hbm.xmlはデータベースのuserテーブルに対応します.


    


    
        
        
        
    
7、srcディレクトリの下にappication Contact.xmlファイルを新規作成します.appication Contect.xmlにsession Factoryのbeanを追加します.そして、hibernate.cfg.xmlとUser.hbm.xmlの情報を配置します.ここでは、IDをインクルージョンとして追加したbeanです.対応するクラスはsl.actions.LoginnActionです.(この類の具体的な内容は後に示す)


         
  

   
   
    
   
      
        sll/hibernate/model/User.hbm.xml
      
  
 


     
         
     
 


8、srcディレクトリの下でstruts.xmlを作成します.struts配置アクションの情報.Actionは、レイヤからの要求を受信し、要求パラメータを受信しながら、モデル方法を呼び出してトラフィックロジックの処理を完了し、最後の制御プログラムの論理は、適切なビューを選択して、結果をクライアントに表示する.前にappication Contect.xmlでIDをlogin Actionとして定義したbeanですので、ここではactionを定義します.loginといいます.classはappication Controtextで定義されているbean:loginActionです.

 
  
    
    
        
            /login_success.jsp
            /login_error.jsp
        
    
9、srcディレクトリの下でstruts.propertiesを新規作成します.
struts.objectFactory=spring
10、LoginnAction.java(事実上、コントロール、業務、モデル層を分けるべきですが、シンプルに考えると、主な目的はsshフレームワークを使うので、内容を全部LoginnAction.javaに書いています.)
public class LoginAction {
    
    private static final long    serialVersionUID    = 4833662754330237479L;

    private String name;
    private String password;
    private SessionFactory sessionFactory;
    
    public String execute(){
        Session session = sessionFactory.openSession();
        //    from         ,  applicationContext.xml    javabean     。
        String hql = "from LoginAction where name=? and password=?";
        Query q = session.createQuery(hql);
        //      ,    name        ,            
        q.setParameter(0, name);
        q.setParameter(1, password);
        List user = q.list();
        session.close();
        if (user.size() > 0) {
            return "success";
        } else {
            return "error";
        }
    }

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}
11、login.jsp

    
        

:
:
12、login_success.jsp

  
    

!

13、login_error.jsp

  
    

13、配置運行が成功した