Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

setProperty="*", at my wits end!  RSS feed

 
David Yutzy
tumbleweed and gunslinger
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I POST to the insert.jsp, the Bean properties are not being set automatically when using ="*". Here is the code (sorry long post).
JSP:
<%@ page import="java.util.*" %>
<%@ page import="java.sql.*" %>
<jsp:useBean id="pool" scope="application" class="HMG.Database.ConnectionPool" />
<jsp:useBean id="company" scope="session" class="HMG.Beans.Companies">
<jsp:setProperty name="company" property="*" />
</jsp:useBean>
<%
boolean isValid = false;
if (company.initConnection(pool)) {
//company.setDescription(request.getParameter("Description"));
if (company.isValid()) {
company.insertCompany();
isValid = true;
}
Connection oC = null;
oC = company.getConnection();
if (oC != null) {
pool.releaseConnection(oC);
}
if (!isValid) {
%>
<jsp:forward page="addedit.jsp">
<jsp aram name="Action" value="E" />
<jsp aram name="R" value="-1" />
</jsp:forward>
<%
} else {
%>
<jsp:forward page="display.jsp" />
<%
}
} else {
throw new Exception("Unable to initialize Connection Pool");
}
%>
BEAN:
package HMG.Beans;
import java.io.*;
import java.sql.*;
import java.util.*;
import HMG.Database.ConnectionPool;
public class Companies implements Serializable {
public int id;
public String description = "";
private Connection con = null;

public Companies() throws Exception {
}
public void Reset() throws Exception {
setID(-1);
setDescription("");
}
/*
* Queries
*/
public void updateCompany() throws Exception {
PreparedStatement oPstmt = null;
String SQL = "UPDATE Companies SET Description = ? WHERE CompanyID = ?";

try {
oPstmt = con.prepareStatement(SQL);
oPstmt.setString(1, description);
oPstmt.setInt(2, id);
oPstmt.executeUpdate();

oPstmt.close();
} catch (Exception e) {
throw new Exception(e.getMessage());
}
}
public void insertCompany() throws Exception {
PreparedStatement oPstmt = null;
String SQL = "INSERT INTO Companies (Description) VALUES (?)";

try {
oPstmt = con.prepareStatement(SQL);
oPstmt.setString(1, description);
oPstmt.executeUpdate();

oPstmt.close();
} catch (Exception e) {
throw new Exception(e.getMessage());
}
}

public void getCompany() throws Exception {
Statement oStmt = null;
ResultSet oRS = null;
String SQL = "SELECT CompanyID, Description FROM Companies WHERE CompanyID = " + id;

try {
oStmt = (Statement)con.createStatement();
oRS = (ResultSet)oStmt.executeQuery(SQL);

if (oRS.next()) {
setID(oRS.getInt("CompanyID"));
setDescription(oRS.getString("Description"));
} else {
Reset();
}

oStmt.close();
oRS.close();
} catch (Exception e) {
throw new Exception(e.getMessage());
}
}
public synchronized ResultSet getCompanies() throws Exception {
Statement oStmt = null;
ResultSet oRS = null;
String SQL = "SELECT * FROM Companies";

try {
oStmt = (Statement)con.createStatement();
oRS = (ResultSet)oStmt.executeQuery(SQL);

return oRS;
} catch (Exception e) {
throw new Exception(e.getMessage());
}
}

/*
* Connection
*/
public boolean initConnection(ConnectionPool pool) throws Exception {
Connection oC = null;

if (pool != null) {
if (pool.getDriver() == null) {
pool.setDefaults();
pool.initializePool();
}
oC = pool.getConnection();
if (oC != null) {
con = oC;
return true;
} else {
throw new Exception("Pool initialized but getConnection() returned null");
}
} else {
throw new Exception("Pool returned null");
}
}

public void setConnection(Connection value) throws Exception {
if (value != null) {
con = value;
} else {
throw new Exception("Connection cannot be set to null");
}
}

public synchronized Connection getConnection() throws Exception {
return con;
}

/*
* Properties
*/

public int getID() {
return id;
}

public void setID(int value) throws Exception {
if (value >= -1) {
id = value;
} else {
throw new Exception("ID cannot be set to less than zero (0)");
}
}
public void setDescription(String value) throws Exception {
if (value != null) {
description = value;
} else {
description = "";
}
}

public String getDescription() {
return description;
}

/*
* Validation
*/
public String getPropertyStatusMsg() {
StringBuffer msg = new StringBuffer();
if (!isValid()) {
msg.append("The following values are missing or invalid: ");
msg.append("<UL>");

if (description == null | | description.equals("")) {
msg.append("<LI>Description cannot be blank</LI>");
}

msg.append("</UL>");
}

return msg.toString();
}

public boolean isValid() {
return (description != null) && (description.equals("") == false);
}
}
 
Cameron Park
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you make sure that a session was alreay created?
 
David Yutzy
tumbleweed and gunslinger
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure what you mean did I create a session first?
 
John Bateman
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Check the naming of you INPUT TYPES in the form. They must be a certain case for your get/set methods to work properly.
I've had the exact same problem you had and when I switched my cases, all worked fine.
Ex.
if my bean or class had the following...

and I wanted to set the properties on the SomeUser object...then you should experiment (as I forget the rule) and try using 'firstName OR FirstName' as the 'name' element in your HTML <form>
I.E.

As I said, I forget the rule, but one of these should work.

------------------
SOURCE CODE should be SURROUNDED by "code" tags.
Click here for an example
 
David Yutzy
tumbleweed and gunslinger
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I figured it out. The solution is to set your FORM field names the same as your private property variables.
EXAMPLE
...<in yout bean>
private firstName = "";
public void setFirstName(string value) {
}
public String getFirstName() {
}
...<FORM field name>
<INPUT type="text" name="firstName"
Notice the case of the FORM field and the private varible are the same. This got it working.
Hope this helps as I know there have been many posts on this issue.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!