0

Getting Execution Function name for debugging.

FROM: JAVA FORUM

The "standard" way to accomplish this is to raise an Exception and then parse its stack-trace. If you encapsulate that inside your "myDebugger()" call, you'd want the second method on the stack. Take a look at StackElement (http://java.sun.com/j2se/1.4.2/docs/api/java/lang/StackTraceElement.html) for the kind of info you can get.There are aspect-oriented libraries out there that hide this from you - you could look at them for entry/exit debuging.Or, you could use CPP and just plug __FILE__ and __LINE__ into your code...Grant

class Debugger {
public static String getWhoCalledMe() {
StackTraceElement
stack[] = new Throwable().getStackTrace();
return
(stack[1].getMethodName());
}
}

|
0

Maintining CheckBox State during Pagination

SOURCE :EXPERT EXCHANGE

Given the zone and the phrasing of the question, I understand this to be a question about a JSP web application, rather than a Swing or AWT-based desktop application. If that's the case, here's how I would approach it.

First of all, to clarify the assumptions I'm making, I'll explain my understanding of your application. You are paging through a set of records, and each record has as one of its elements a checkbox. As the pages through the records, he can check some items, indicating that they should be acted on in some way. At some point, the user has checked all the desired records on several pages, and wants to submit them all at once for some action.

I'm assuming that each record has some sort of unique identifier, and when the user has browsed through the record set and checked all of the necessary records, the submit action can use these unique identifiers to process the correct records. I'm assuming you're using EL and JSTL tags, and not scriptlets, since that's the best practice. In the example code, you can change the "name" attributes on the form controls; I'm just using what seems natural to me. Same goes for EL variable names. If you need clarification, please ask. I'm not sure what framework you're using on the server side, so I'll just show the standard servlet API implementation. It should be easy to adapt that to whatever framework one might use, but that's outside the scope of this question.

Here's a skeleton of the form I'd use:

<c:url var="action" value="/viewpage" /><form
action="${action}"
method="post"><fieldset><table><tbody>
<c:forEach items="${records}" var="rec">
<tr>
<td>

<!-- Display the
records fields as desired --><c:out value="${rec.id}" />

</td>
<td>

<!-- Here's
the crucial part... -->

<input type="hidden"
name="records" value="${rec.id}" />

<c:choose>
<c:when test="${sessionScope.myCheckBoxes[rec.id]}">
<checkbox checked="checked" name="checks" value="${rec.id}" />

</c:when>

<c:otherwise>
<checkbox name="checks" value="${rec.id}" />
</c:otherwise>
</c:choose>


</td>
</tr>

</c:forEach>
</tbody>
</table>
</fieldset>

<fieldset>
<button type="submit" name="page"
value="${page - 1}">
Previous
</button>

<button type="submit" name="page" value="${page + 1}">
Next
</button>

<button type="submit"
name="process" value="true">
Done
</button>
</fieldset>
</form>

Then, on the
servlet (mapped to /viewpage) , doPost would contain something like this:

protected void doPost(HttpServletRequest req, HttpServletResponse res){

...String[] records = req.getParameterValues("records");

if
(records != null)Set checks = new HashSet();
String[] tmp =
req.getParameterValues("checks");
if (tmp != null) {
for (int idx = 0;
idx < tmp.length; ++idx)
checks.add(tmp[idx]);
}

Map
myCheckBoxes = (Map) req.getSession().getAttribute("myCheckBoxes");

if
(myCheckBoxes == null)
myCheckBoxes = new HashMap();

for (int idx =
0; idx < records.length; ++idx) {
if (checks.contains(records[idx]))
myCheckBoxes.put(records[idx], Boolean.TRUE);
else
myCheckBoxes.remove(records[idx]);
}
...

if
(Boolean.valueOf(req.getParameter("process")).booleanValue()) {

/* The
user pressed the "Done" button; do your work here. */

Iterator recIds =
myCheckBoxes.keySet().iterator();
while (recIds.hasNext()) {
String id =
(String) recIds.next();...
}
/* Clear the check state from the session
(probably, depends on your requirements. */
req.getSession().removeAttribute("myCheckBoxes");
}
else {
req.getSession().setAttribute("myCheckBoxes", myCheckBoxes);
}
...
}




NOTE : Sorry, I missed some braces in the code; the block guarded by "if (records != null)" should extend to the end of the "for" loop over "records"

|
0

Using Oracle Sequence in Hibernate

For using sequence create in oracle in hibernate, make following confguration

<class name="className" table="tableName"schema="databasename">
<id name="id" type="java.lang.Long"unsaved-value="null" column="ID">
<generatorclass="sequence">
<paramname="sequence">USERS_SEQ</param>
</generator>
</id>

|
0

Using Cancel button in struts

Cancel button is a type of submit button which bypasses validation during submission of the form. So, cancel button can be used for submitting a form without validation and to reset form.

  1. Using Cancel button for bypassing validation

    Reset button is also available in struts but reset button resets to the form fields to default if the form has not been submitted or has not be passed through validator. Once the validation has occured and validation failed, the form fields are populated with the values we have send for validation. Now by pressing reset button will retain the typed and not default values. In this case, struts cancel button is used. Struts cancel button can be rendered through
    <html:cancel/>.

    It is rendered in html as
    <input type="reset" value="Reset"><input type="submit"
    name="org.apache.struts.taglib.html.CANCEL" value="Cancel"
    onclick="bCancel=true;">

    The onclick="bCancel=true;" in generated reset button plays a great role for bypassing clientside validation.

    If you are using dispactch action, create a method called 'cancelled' and get the form field values inside that method.

    Cancel button bypassed both serverside and clientside validation.
  2. Using Cancel button as reset button
    If you're using Struts 1.2.9 or above, you must specify cancellable="true" in the Action mapping as;


    <action
    attribute="registrationForm"
    input="..."
    ...
    ...
    <set-property property="cancellable" value="true"/>
    ...
    ...
    </action>
    now its time to reset form fields through action like;

    if(isCancelled(request)){
    registrationForm.set("firstname",
    "");
    }
    [I'm using DynaValidatorForm here]

    If you are using DispatchAction then you have to create a method named cancelled and reset forms there.



|
0

Select initial value in struts

Suppose, you want a default value to be selected during the page load while using DynaValidatorForm or DynaValidatorActionForm, use initial attribute of field-property to define the value that is to be selected by default.

eg; you have a SEX porperty whose value can be either male or female and you use radio button. If you wish male to be selected by default, then put inial=male.

Here is my form-bean definition

<form-bean name="consumerRegistrationForm"
type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="firstname" type="java.lang.String" />
...
...
<form-property name="sex" type="java.lang.String"
initial="male" />
...
...
<form-property
name="userType" type="java.lang.String" initial="homeUser"/>
</form-bean>

Then, while rendering the form, male is initially selected for SEX property.

|
0

Use of formula to concate fields

Suppose you have two fields holding firstname and lastname. And there is a property called fullname which is expected to hold the concatinated firstname and lastname. Then you have to do like follows...


Define a property called fullname in its class

Then in .hbm file, put a porperty like

<property name="fullName" formula="concat(FIRST_NAME,
LAST_NAME)"/>



Remember: FIRST_NAME and LAST_NAME are the database field name and not the class properties. Only two fields can be concatinated. No. of arguments mismatched exception may occur if more than two fields are concatinated.


|
0

Preventing Form Resubmission (due refreshing)

To prevent form resubmission due to page refresh we can use HTTP redirect

To use HTTP redirect feature, the forward is set as follows:



However there is one catch in this method, the actual jsp name is shown.

Therefore we can change the forward action as following:

<forward redirect="true" path="/gotoSuccess.do" name="success">

where gotoSuccess.do is another action mapping using forward action as follows:
<action path="/gotoSuccess" validate="false" parameter="/success.jsp" type="org.apache.actions.ForwardAction">

|
0

Struts Forward redirect=true

excerpted from : Struts : The complete Reference

The forward tag looks up a forward from the Struts configuration file and redirects to it. Based on the way that the forward is defined in the configuration file, this tag will either forward to the URL specified by the forward or perform a redirect to it. If the forward has its redirect attribute set to “true”, as shown in the following example, this tag will perform a redirect:

A redirect sends a response to the user’s browser instructing it to load another page. A forward, on the other hand, simply “forwards” control (on the server side) to another URL without having the browser make another request.

Note that only URLs residing on the same server can be forwarded to, whereas any URL can be redirected to.

|

Copyright © 2009 So That I Can Remember All rights reserved. Theme by Laptop Geek. | Bloggerized by FalconHive.