SSH框架运行流程

程序入口web.xml

服务器识别web.xml,解析标签查找:

<welcome-file-list>

   <welcome-file>login.jsp</welcome-file>

  </welcome-file-list>

此时程序就像C语言进入main函数,也就是web程序的主页面,接下来解析主页面,服务器查找login.jsp,解析主页面,如果有问题会报错。到这一步后台业务处理代码还没执行。

<body>

<center><form method="post"action="login" >

UserName<input type="text"name="name" id="name"/><br/>

Password<input type="password"name="password" id="password"/><br/>

<input type="submit"value="login" /></form></center>

</body>

此时服务器解析form标签里的action属性,注意程序将根据action的值去Struts.xml配置文件中查找对应的action。Struts.xml配置文件:

<action name="login"class="com.test.action.LoginAction" method="login">

    <result name="login">login.jsp</result>

    <result name="ok">loginOK.jsp</result>

    <result name="error">loginError.jsp</result>

    </action>

可以看到class和method属性,此时程序根据这两个属性锁定后台执行函数。以上过程为前端设计不涉及后台业务流程。接下来进入业务处理函数,以Class LoginAction为起点进入业务处理,此类的作用是把返回结果转化成struts.xml可以识别的result,也就是说返回值对应result的name属性。LoginAction.java:

private String password;

private UserService userService1;

 

public String login(){

    if(name==null||password==null){

        return"login";

    }

    else{

        Booleanu=userService1.login(name, password);

        if(u){

            return"ok";

        }

        else{

            return"error";

        }

    }

由以上代码可以看出开始进行一层一层调用函数,类似C语言的调用其他模块函数,程序开始查找UserService类的login函数,此类的作用是把上一层的结果转化成true或者false。Userservice.login()函数:

publicbooleanlogin(String name, String password) {

        Useru=userDAO.Getuser(name);

        if(u==null){

            returnfalse;

        }

        else{

            if(u.getPassword().equals(password)){

            returntrue;

            }

            else{

                returnfalse;

            }

        }

    }

根据以上代码可以知道该函数调用了userDAO的Getuser()函数传入参数name,返回true或false。继续顺藤摸瓜找到Getuser函数在userDAO类中:

public User Getuser(String name){

    Queryquery = sessionFactory.getCurrentSession().createQuery("from User where username=?");

    query.setString(0,name);

    Useru=(User)query.uniqueResult();

    return u;

    }

值得注意的是这里出现了sql查找,但是使用的并非传统的plsql,而是hibernate封装好的sql语法格式。本人不太理解为什么要进行hibernate永久化,查找资料可能是因为把数据库连接断开操作托管给hibernate,防止项目开发中直接操作数据库出现加锁问题。

以上就完整的从jsp到数据库的数据流程。具体SSH框架的配置搭建网上教程很多。下图为程序运行结构图。

SSH框架运行流程

程序入口web.xml

服务器识别web.xml,解析标签查找:

<welcome-file-list>

   <welcome-file>login.jsp</welcome-file>

  </welcome-file-list>

此时程序就像C语言进入main函数,也就是web程序的主页面,接下来解析主页面,服务器查找login.jsp,解析主页面,如果有问题会报错。到这一步后台业务处理代码还没执行。

<body>

<center><form method="post"action="login" >

UserName<input type="text"name="name" id="name"/><br/>

Password<input type="password"name="password" id="password"/><br/>

<input type="submit"value="login" /></form></center>

</body>

此时服务器解析form标签里的action属性,注意程序将根据action的值去Struts.xml配置文件中查找对应的action。Struts.xml配置文件:

<action name="login"class="com.test.action.LoginAction" method="login">

    <result name="login">login.jsp</result>

    <result name="ok">loginOK.jsp</result>

    <result name="error">loginError.jsp</result>

    </action>

可以看到class和method属性,此时程序根据这两个属性锁定后台执行函数。以上过程为前端设计不涉及后台业务流程。接下来进入业务处理函数,以Class LoginAction为起点进入业务处理,此类的作用是把返回结果转化成struts.xml可以识别的result,也就是说返回值对应result的name属性。LoginAction.java:

private String password;

private UserService userService1;

 

public String login(){

    if(name==null||password==null){

        return"login";

    }

    else{

        Booleanu=userService1.login(name, password);

        if(u){

            return"ok";

        }

        else{

            return"error";

        }

    }

由以上代码可以看出开始进行一层一层调用函数,类似C语言的调用其他模块函数,程序开始查找UserService类的login函数,此类的作用是把上一层的结果转化成true或者false。Userservice.login()函数:

publicbooleanlogin(String name, String password) {

        Useru=userDAO.Getuser(name);

        if(u==null){

            returnfalse;

        }

        else{

            if(u.getPassword().equals(password)){

            returntrue;

            }

            else{

                returnfalse;

            }

        }

    }

根据以上代码可以知道该函数调用了userDAO的Getuser()函数传入参数name,返回true或false。继续顺藤摸瓜找到Getuser函数在userDAO类中:

public User Getuser(String name){

    Queryquery = sessionFactory.getCurrentSession().createQuery("from User where username=?");

    query.setString(0,name);

    Useru=(User)query.uniqueResult();

    return u;

    }

值得注意的是这里出现了sql查找,但是使用的并非传统的plsql,而是hibernate封装好的sql语法格式。本人不太理解为什么要进行hibernate永久化,查找资料可能是因为把数据库连接断开操作托管给hibernate,防止项目开发中直接操作数据库出现加锁问题。

以上就完整的从jsp到数据库的数据流程。具体SSH框架的配置搭建网上教程很多。下图为程序运行结构图。