Posted By:
zangbeto_totovi
Posted On:
Friday, November 3, 2006 09:10 AM
I wan to build a simple struts application to insert one record into my Oracle 1Og table. I created java classes below and deploy them to my tomcat classes folder. I modify my struts-config.xml file and restart tomcat. When I run my application, the input form (newaccount.jsp) is displayed but when I submit values nothing happened. Why? Here is my table structure: ------------------------ Table: dac_account ------------------------ PASSWORD VARCHAR2(80) EMAIL VARCHAR2(200) STATUS VARCHAR2(1) I have created the following java classes: 1) Account.java 2) AccountData.java 3) AddAccountAction
More>>
I wan to build a simple struts application to insert one record into my Oracle 1Og table. I created java classes below and deploy them to my tomcat classes folder. I modify my struts-config.xml file and restart tomcat. When I run my application, the input form (newaccount.jsp) is displayed but when I submit values nothing happened. Why?
Here is my table structure:
------------------------
Table: dac_account
------------------------
PASSWORD VARCHAR2(80)
EMAIL VARCHAR2(200)
STATUS VARCHAR2(1)
I have created the following java classes:
1) Account.java
2) AccountData.java
3) AddAccountAction.java
4) AccountForm.java
and the jsp view and struts-config file.
5) newaccount.jsp
6) struts-config.xml
Here is the full code:
----------------------------------------------------
1) Account.java
----------------------------------------------------
package org.myorg.myapp;
import org.apache.log4j.Logger;
import org.apache.log4j.BasicConfigurator;
/**
*
*/
public class Account {
static Logger logger = Logger.getLogger(Account.class);
protected String email;
protected String password;
protected String status;
// Email
public void setEmail(String email) {
this.email = email;
}
public String getEmail() {
return this.email;
}
// Password
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
// status
public void setStatus(String status) {
this.status = status;
}
public String getStatus() {
return status;
}
}
-----------------------------------------
2) AccountData.java
-----------------------------------------
package org.myorg.myapp;
import javax.sql.DataSource;
import java.util.ArrayList;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.log4j.Logger;
import org.apache.log4j.BasicConfigurator;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
public class AccountData {
static Logger logger = Logger.getLogger(AccountData.class);
/**
*
* @param account
* @param dataSource
* @throws Exception
*/
public static void addAccount(Account account, DataSource dataSource)
throws Exception {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = dataSource.getConnection();
stmt = conn.createStatement();
StringBuffer sqlString =
new StringBuffer("insert into dac_signon ( email, password ) values( ");
sqlString.append("'" +
account.getEmail() + "', ");
sqlString.append("'" +
account.getPassword() + "') ");
logger.info("[Sql string to insert new account: ]" + sqlString.toString());
stmt.execute(sqlString.toString());
}
finally {
if ( rs != null ) {
rs.close();
}
if ( stmt != null ) {
stmt.close();
}
if ( conn != null ) {
conn.close();
}
}
}
}
---------------------------------------------
3) AccountForm.java
---------------------------------------------
package org.myorg.myapp;
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;
import org.apache.log4j.Logger;
import org.apache.log4j.BasicConfigurator;
public class AccountForm extends ActionForm {
static Logger logger = Logger.getLogger(AccountForm.class);
protected String email;
protected String password;
// Email
public void setEmail(String email) {
this.email = email;
}
public String getEmail() {
return this.email;
}
// Password
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
// This method is called with every request. It resets the
// Form attribute prior to setting the values in the new
// request.
public void reset(ActionMapping mapping, HttpServletRequest request) {
this.password = "";
this.email = "";
}
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if ( (email == null) || (email.length() == 0) ) {
errors.add("id",
new ActionError("account.error"));
}
return errors;
}
}
----------------------------------------------------
4) newaccount.jsp
----------------------------------------------------
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
---------------------------------------------------
5) struts-config.xml
---------------------------------------------------
<?xml version="1.0" encoding="ISO-8859-1" ?>
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
property="driverClassName"
value="oracle.jdbc.driver.OracleDriver" />
property="url"
value="jdbc:oracle:thin:@localhost:1521:ORCL" />
property="username"
value="myusername" />
property="password"
value="mypassword" />
property="maxActive"
value="10" />
property="maxWait"
value="5000" />
property="defaultAutoCommit"
value="false" />
property="defaultReadOnly"
value="false" />
type="org.myorg.myapp.AccountForm"/>
path="/Account"
type="org.myorg.myapp.AddAccountAction"
name="AccountForm"
scope="request"
validate="false"
input="/pages/newaccount.jsp">