大家好我是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;
}
}
答案 0 :(得分:0)
您创建了一个全新的对象RegistrationForm,因此没有任何价值。它应该是
RegistrationForm objReg = (RegistrationForm) form;
答案 1 :(得分:0)
您正在创建新的RegistrationForm
:
RegistrationForm objReg = new RegistrationForm();
您希望使用框架提供的现有表单:
RegistrationForm objReg = (RegistrationForm) form;
objReg
是一个令人讨厌的名字。
答案 2 :(得分:0)
您在FormForm中提供的表单字段名称和名称应该相同。 例如:
的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">
一定会奏效。