使用struts编写的bug管理(二)

本文介绍如何使用Struts框架实现用户登录功能,包括LoginAction和LoginForm的编写,以及数据库验证过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

接上文,Class.forName("[drivername]")你也可以使用(一)里面的方法将他们放在一起,将forname写在Return语句前。

OK, 一个简单的数据库连接类完成了,测试通过(具体方法见用户登陆),之后我们将扩展这个类。

6_    类的设计:用户登陆

根据struts的习惯,我建了两个.java文件:LoginAction.java和LoginForm.java,分别放在com.[your project name].action和com.[your project name].actioncom.[your project name].actionform两个包中。

这是struts很常见的文件,struts-config.xml中搭建了两者的桥梁,让我们看看他们是怎么工作的:

  • LoginForm

/*
 * loginForm.java
 *
 * Created on 2006/4/11,PM 10:03
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.esc.actionform;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author tony
 */
public class loginForm extends ActionForm{
   
    String username = null;
    String password = null;
   
    public String getUsername(){
        return this.username;
    }
    public void setUsername(String username){
        this.username = username;
    }
   
    public String getPassword(){
        return this.password;
    }
    public void setPassword(String password){
        this.password = password;
    }

/*

如你所见,一兜的get和set方法,怎么用呢,这里注意变量的名字,或许你在login.jsp页面文件的某个地方会发现他们的踪迹;

*/

    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
        ActionErrors errors = new ActionErrors();
        if(username == null || username.length()<1){
            errors.add("username",new ActionError("errors.username.required"));
        }
        if(password == null || password.length()<1){
             errors.add("password",new ActionError("errors.password.required"));
        }
        return errors;
    }

/*

这个方法大家不会陌生,在super中可以找到,很简单的验证和报错方法,但注意不要定义错资源文件的位置;

*/
   
}

给出login.jsp片断以方便理解,jsp我不是很熟,这里不方便班门弄斧,见谅:

...... ......

<html:errors/>

<html:form action = "/LoginSubmit" focus = "username">
    <table border="0" width = "100%">
        <tr>
            <th align = "right">Username:</th>
            <td align = "left"><html:text property="username"/></td>
        </tr>
        <tr>
            <th align="right">Password:</th>
            <td align="left"><html:password property="password" redisplay="false"/></td>
        </tr>
        <tr>
            <td align="right"><html:submit/></td>
            <td align="left"><html:reset/></td>
        </tr>
    </table>

</html:form>

...... ......

属性和LoginForm中定义的变量是一致的,那么怎么知道此form不是彼form呢?

我们的重量级文件struts-config.xml可以说明这件事情,打开你的文件,找到下面这段语句:

<form-beans>
        <form-bean name = "loginForm" type = "com.[your project name].actionform.loginForm"/>
</form-beans>

以及

<action-mappings>
        <action path="/LoginSubmit"
                type ="com.esc.action.loginAction"
                name = "loginForm"
                scope = "post"
                validate = "true"
                input = "/login.jsp">
                <forward name="success"  path="/your path"/>
                <forward name="failed"  path="/your path"/>
        </action>

</action-mappings>

没有的话,添加一个。不同的颜色,不同的对应关系,希望能够看懂。

相对的,可以知道action和actionform是怎么捆绑起来的了。

  • Login

上面提到了组装,我这么叫,这里就不提了,看看源码,我将验证的过程写在这里,其实,曾经想过写在单独的类里,后来觉得没什么必要,那就先写在这里好了,如果,你不知道数据库的类怎么测试,那么下面的代码可以说明问题:

/*
 * loginAction.java
 *
 * Created on 2006/4/11, PM 10:52
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.esc.action;

import java.sql.Connection;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import java.io.IOException;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.sql.Statement;

import com.esc.actionform.loginForm;
import com.esc.database.database;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;

/**
 *
 * @author tony
 */
public class loginAction extends Action {

    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){
       
        Connection conn = null;
        String command = null;
        String password = null;
        loginForm lf = (loginForm)form;
        database db = new database();
        try{
           
            conn = db.getConnection();
            command = "SELECT USER_PASSWORD FROM ESC_ADMIN.USERS WHERE USER_NAME = '" +   lf.getUsername() + "'";
            Statement smt = conn.createStatement();
            ResultSet rs = smt.executeQuery(command);
           
            if (rs.next()){
                password = rs.getString("USER_PASSWORD");
                if(!lf.getPassword().equals(password)){
                    return mapping.findForward("failed");
                }else{
                    HttpSession session = request.getSession();
                    session.setAttribute("user", lf.getUsername());
                }
            }else{
                return mapping.findForward("failed");
            }
        }catch(SQLException e1){
            e1.printStackTrace();
            return mapping.findForward("failed");
        }catch(IOException e2){
            e2.printStackTrace();
            return mapping.findForward("failed");
        }catch(ClassNotFoundException e3){
            e3.printStackTrace();
            return mapping.findForward("failed");
        }finally{
            if(conn != null){
                try{
                    conn.close();
                }catch(Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return mapping.findForward("success");
    }

}

没有多余的项要在struts-config.xml中设定,因此可以直接运行写好的程序了。netbeans自动分发以及打包发布,很简便的就可以看到成果了。

注:

<message-resources parameter="/resource/ApplicationResource"/> ,这是资源文件在路径,写在struts-config.xml里面。

上面的程序定义了两个error项,定义为:

errors.username.required = <LI>User Name is required</LI>
errors.password.required = <LI>Password is required</LI>

后面的值将取代Login.jsp中的<html:errors/>,得到我们需要的反馈信息。

... ...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值