0

大家好,我是java struts的初学者,我正在创建简单的注册表单,但在提交表单后,null正在插入数据库表中。我已经创建了表单类名称作为RegistrationForm.java程序正在运行,但null正在插入我的操作代码是---

public class RegistrationAction extends Action {    
     private Connection con = null;    
     public ActionForward execute(ActionMapping mapping, ActionForm form,  
                HttpServletRequest request, HttpServletResponse response)  
                throws Exception {  

            ActionErrors errors = new ActionErrors();  

            RegistrationForm objReg = new RegistrationForm();
            String userName = (String) objReg.getTxtusername();
            String userPass = (String) objReg.getTxtpassword();  

            Class.forName("com.mysql.jdbc.Driver").newInstance();    
            con = DriverManager.getConnection("jdbc:mysql:///test", "root", "");    
            if (!con.isClosed()) {   
                PreparedStatement ps = (PreparedStatement)             con.prepareStatement("INSERT   >INTO tblUserdetails(fldUsername,fldPassword) VALUES (?,?) ");  

                ps.setString(1, userName);  
                ps.setString(2, userPass);  

                ps.execute();  
            } else {  
                errors.add("SQL", new ActionMessage("error.SQLConnectivity"));  
            }  
            return mapping.findForward(SUCCESS);  
       }  
}

package com.vaannila.form;

import org.apache.struts.action.ActionForm;

public class RegistrationForm extends ActionForm {
    private String txtusername;
    private String txtpassword;
    private String txtrepassword;
    private String txtemail;

    public String getTxtusername() {
        return txtusername;
    }

    public void setTxtusername(String txtusername) {
        this.txtusername = txtusername;
    }

    public String getTxtpassword() {
        return txtpassword;
    }

    public void setTxtpassword(String txtpassword) {
        this.txtpassword = txtpassword;
    }
}
4

3 回答 3

0

您创建了一个全新的对象 RegistrationForm,因此它没有任何价值。它应该是

RegistrationForm objReg = (RegistrationForm) form;
于 2013-01-22T16:06:15.020 回答
0

您正在创建一个新的RegistrationForm

RegistrationForm objReg = new RegistrationForm();

您想使用框架提供的现有表单:

RegistrationForm objReg = (RegistrationForm) form;

objReg是一个令人讨厌的名字。

于 2013-01-22T16:06:24.383 回答
0

您的表单字段名称和您在 ActionForm 中提供的名称应该相同。例如:

helloWorld.jsp

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Hello World</title>
</head>
<body>
<form action="login.do">
<pre>
User Name : <input type="text" name="uname"/>
Password : <input type="password" name="pass"/>
<input type="submit" value="LogIN">
</pre>
</form>
</body>
</html>

helloWorldForm.java

private String uname,pass;

and in struts ur entry should like

    <action path="/login" type="com.app.core.biswa.AppLoginAction" name="helloWorldForm" scope="session" validate="true" input="/helloWorld.jsp">

它肯定会奏效。

于 2013-12-25T15:35:28.427 回答