Jboss hosting - JavaSercer Pages Example 17.15. The DBTag’s doAfterBody( )

JavaSercer Pages Example 17.15. The DBTag’s doAfterBody( ) Method public int doAfterBody( ) throws JspException { sqlValue = bodyContent.getString( ); return SKIP_BODY; } The SQL statement may contain question marks as placeholders for values set by nested value actions. As you will see later, the value actions create the appropriate Value subclass and call the DBTag ’s addValue( ) method, shown in Example 17.16. Example 17.16. The DBTag’s addValue( ) Method public void addValue(Value value) { if (values == null) { values = new Vector( ); } values.addElement(value); } This method creates a Vector to hold all values the first time it’s called, and then adds the Value object to the Vector. When called by subsequent value action tag handlers, the Value objects are simply added to the list. The real processing happens in the doEndTag( ) method, shown in Example 17.17. This method is called by the container when the action element’s body has been processed and the end tag is encountered. Example 17.17. The DBTag’s doEndTag( ) Method public int doEndTag( ) throws JspException { Connection conn = getConnection( ); sqlCommandBean.setConnection(conn); sqlCommandBean.setSqlValue(sqlValue); sqlCommandBean.setValues(values); Object result = null; try { result = execute(sqlCommandBean); } catch (SQLException e) { … } catch (UnsupportedTypeException e) { … } finally { … } // Save the result with the specified id in the specified scope if (id != null) { pageContext.setAttribute(id, result, scope); } return EVAL_PAGE; } The private getConnection( ) method is used to get a Connection. The Connection is retrieved either from the DataSource specified by the dataSource attribute value for the or the action itself, or from an enclosing element. We’ll return to getConnection( ) and the exception handling code later when we look at transaction support. The Connection, the SQL statement, and all values (if any) are passed to the bean. Then, the abstract execute( ) method is called to ask the bean to execute the SQL statement. The Object returned by execute( ) is saved in the scope specified by the scope attribute, using the name specified by the id attribute. The implementation of the execute( ) method is the only thing that differs between the tag handlers for the action and the action. The corresponding tag handler classes, QueryTag and UpdateTag, extend the DBTag class and implement the execute( ) method. Example 17.18 shows the implementation in the QueryTag tag handler. page 251

Hint: This post is supported by Gama web hosting php mysql provider

Comments are closed.