Struts

Name:
Location: Bangalore, India

Sunday, October 28, 2007

DispatchAction:

Tutorial link:

public class ABCAction extends DispatchAction{

public ActionForward addABCComments(
ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {

public ActionForward addXYZComments(
ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {

JSP Page:

<a href="#P" class="actionButtonUp" id="addComment" onclick="document.forms[0].action='addRequestComment.do?method=addABCComments';document.forms[0].submit();"/>
<bean:message key="common.btn.add" bundle="gtem"/>
</a>

In the jsp page as part of request parameter we had added method, which we define in action-mapping as part of parameter attribute.

<a href="#P" class="actionButtonUp" id="addComment" onclick="document.forms[0].action='addRequestComment.do?method=addXYZComments';document.forms[0].submit();"/>
<bean:message key="common.btn.add" bundle="gtem"/>
</a>

struts-config:

<struts-config>
<form-beans >
<form-bean name="RequestForm" type="com.ram.guru.form.RequestForm"/>
</form-beans >

<action-mappings >
<action
path="/app/addRequestComment"
attribute="RequestForm"
name="RequestForm"
scope="request"
validate="false"
input="/jsp/request/jsp/Request.jsp"
type="com.ram.guru.action.request.ABCAction"
parameter="method">
<forward name="success" path="/jsp/request/jsp/Request.jsp" />

</action>

</action-mappings >

<global-exceptions />
<data-sources />
</struts-config>

Monday, October 22, 2007

To iterate a list in JSP Page

<logic:present name="RequestForm" property="objSearchResultsList">
<logic:notEmpty name="RequestForm" property="objSearchResultsList">
<logic:iterate id="row" name="RequestForm" property="objSearchResultsList">
<tr>
<td> <bean:write name="row" property='bankName' /></td>
<td> <bean:write name="row" property='requestAmount' /></td>
</tr>
</logic:iterate>
</logic:notEmpty>
</logic:present>

Here we don’t have the tag bean:define to define the bean and then iterate since we are directly iterating using logic:iterate.

The id attribute in logic:iterate points to the bean objects inside the list with which you can iterate. We will be using the value of id attribute in logic:iterate as the value of name attribute in bean:write.


The id attribute specifies a scriptlet variable that is assigned the current item at the start of each iteration, namely employee.

<logic:iterate id="element"
name="bean"
property="stringArray"
indexId="index">


The indexId attribute specifies a scriptlet variable called index, which holds that current index number of the loop. This is useful if you want to generate row numbers for long listings.

Before we iterate a List or a Map we need to check whether the List is present.

<logic:present name="BankForm" property="countryNameKeyMap">
<logic:present name="CountryForm" property="gtemStatusList">

Enable/Disable Textbox based on radio button selection.

<html:radio property="requestAmountActive" value="PartialReturn" onClick="document.forms[0].requestAmount.disabled=false;"> Partial Return < /html:radio>
<html:radio property="requestAmountActive" value="FullReturn" onClick="document.forms[0].requestAmount.disabled=true;"> Full Return< /html:radio>

<html:text property="requestAmount" name="RequestForm" disabled="true" />

When a radio button is selected and main submit is hit, the value that's selected can be anything like a number (or) String. The value is given in value attribute.

Example:

value="PartialReturn"
value="1"
value="True"
value="T"

Displaying Date using Struts tag

Instead of hard-coding the date format in format attribute we are specifying it in formatKey in application resources and using bundle property to retrieve it.

<logic:iterate id="row" name="RequestForm" property="objSearchList">
<tr class="rmt-tableRow_oddBG">
<td nowrap class="rmt-tableCellTxt"> <bean:write name="row" bundle="abc" formatKey="common.format.date" property='maxMaturityDate' /></td>
<td nowrap class="rmt-tableCellTxt"> <bean:write name="row" property='tbuName' /></td>
</tr>
</logic:iterate>


To hide a control display from user









Submitting the data from JSP to Java Layer

onClick="document.forms[0].action='blockLimit.do';document.forms[0].submit();"

bean:define

To Define a new bean at runtime
<bean:define id="row" name="BankForm" property="countryNameKeyMap" />

So the rules for attribute usage are as follows:

id is used to define a new bean
name is used to refer to an existing bean
property refers to an existing property of the form that’s defined using name property.

For Iterating
<bean:define id="row" name="BankForm" property="countryNameKeyMap" />
<html:options collection="row" property="value" labelProperty="key" />

Collection – refers to the newly created bean.
Property – Refers to the corresponding key (int) value in the drop downs in the screen. The select value(int value) will be sent to java layer.
labelProperty - Refers to the string values that are displayed in the drop down in UI screen for user to select.

To iterate a List/Map

<logic:present name="BankForm" property="countryNameKeyMap">
<html:select size="1" name="BankForm" property="countryDetailsKey">
<html:option value="">Select Country :</html:option>
<bean:define id="row" name="BankForm" property="countryNameKeyMap" />
<html:options collection="row" property="value" labelProperty="key" />
</html:select>
</logic:present>

To write static content to screen as plain text we use bean write tag

<bean:write name="RequestForm" property="requestStatus" />

Thursday, October 18, 2007

Struts Documentation

Struts Docs

bean:define tag

Tutorial

Tuesday, October 16, 2007

How to print a page

<a href="#" onClick="window.print();">Send to printer</a>

How to go back to the previous page

<input type="button" value="Cancel" class="primaryButton" onClick="javascript:history.back();">


How to close a pop-up window on click of close button

<a href="#" onClick="window.close();">Close</a>

How to avoid request parameters

First declare the property that you need to send in as a request parameter in html:hidden

<html:hidden name="RequestForm" property="requestDetailsKey"/>

Or in html:text
<div style="display:none">
<html:text name="PricingForm" property="searchType"/>
<html:text name="PricingForm" property="pricingTemplateKey"/>
</div>

Calling the JS code


<logic:present name="RequestForm" property="objSearchResultsList">
<logic:iterate id="row" name="RequestForm" property="objSearchResultsList">
<td nowrap class="rmt-tableCellTxt">
<a href="javascript:viewRequest(<bean:write name="row" property='requestDetailsKey'/>);">

<logic:notEmpty name="row" property='countryName'>
<bean:write name="row" property='countryName' />
</logic:notEmpty>

<logic:empty name="row" property='countryName'>
----
</logic:empty>
</a>
</td>

JS Code:

function viewRequest(ptkey) {

var requestDetailsKey = document.forms[0].requestDetailsKey;
requestDetailsKey.value=ptkey;
document.forms[0].action='viewRequest.do';
document.forms[0].submit();
}

What are we doing here?

We are sending the requestdetailskey as input to the JS script. With this approach we are NOT appending the requestdetailskey to the URL which is an obvious security threat and NOT a professional approach.

Here we are also checking for conditions like whether countryName is null or not. If it is null then show dashes as link or display the country name as link.

It was handled earlier as request parameters.

<td nowrap class="rmt-tableCellTxt"> <a href="viewRequest.do?requestDetailsKey=<bean:write name="row" property="requestDetailsKey"/>
"><bean:write name="row" property='countryName' />

How to avoid scriplets in JSP Pages.

Thursday, October 04, 2007

Why Struts?

http://sandbox.sourcelabs.com/kosta/sashstarter-2.0/docs/presentation/html/text4.html