Sling printing wrong value inside an href - java

I have to show two different banner in two different locations, first one is inside a thank you page and the second one is inside the confirmation email.
In both the case i need to print some info inside the urls.
In the first case the banner is generated by a script where i pass some values using Sling
<sly data-sly-use.data="........controller.BookingFormModificaController"></sly>
<sly data-sly-use.booking=".......controller.BookingConferma"></sly>
<div id='b_container'>
<script type = 'text/javascript' id='sp_widget' data-hash='00000' datacontainer='b_container' data-size='1080x500' data-tpncy='false' src='example.com/js/booking_sp_widget.js?checkin=${ data.depDateOld }&checkout=${ data.retDateOld }&iata_orr=1&iata=${ data.arrivalAirport }&lang=${ booking.selectedLanguage }&selected_currency=${ booking.ctx.currency } '></script>
</div>
This is the result and is correct:
src="http://www.example.com/00000?fid=1540980292381&checkin=2018-12-17&checkout=2018-12-22&iata_orr=1&iata=MXP&lang=it&selected_currency=EUR&dsize=1080,500&dtpncy=false&dtsize=&dmsize="
In the second case (the email) is not a script but just an img with an a tag like below
<sly data-sly-use.data="........controller.BookingFormModificaController"></sly>
<sly data-sly-use.booking=".......controller.BookingConferma"></sly>
<a href="https://example.com/searchresults.it.html?aid=1503416&checkin=${ data.depDateOld }&checkout=${ data.retDateOld }&iata_orr=1&iata=${ data.arrivalAirport }&lang=${ booking.selectedLanguage }&selected_currency=${ booking.ctx.currency }">
<img src="https://via.placeholder.com/1080x300" width="100%" alt="" style="margin:0; padding:0;border:none;display:block;" />
</a>
but in this case, after the email is sent, it has the following url:
href="https://example.com/searchresults.it.html?aid=1503416&checkin=&checkout=&iata_orr=1&iata=&lang=&selected_currency="
My goal is generate an URL like the first one.

When attaching queryParameter to a link in HTL/Sightly you can easily use the build in link builder. Simply build a Map in a SlingModel you are good to go.
HTL
<a class="a-link" href="${ myModel.linkPath # extension='json', query=myModel.queryParameterMap }">...</a>
Sling Model
public static Map<String, String> getQueryParameterMap(final Page currentPage)
{
final Map<String, String> map = new HashMap<>();
if (Objects.nonNull(currentPage))
{
map.put("someParameterA", "someValueA");
map.put("someParameterB", "someValueB");
}
return map;
}

Related

SELENIUM JAVA - Verify partial content of a string

I would like to verify the partial value of a string I get from a web page. I give an example the string is "Incident 1946721 Updated" and I would like to insert a check that verifies that the two words Incident are present as prefix and Updated as suffix. how can I do?
the html code where the text is present this is:
<div class="modal-body"><button type="button" class="bootbox-close-button close" data-dismiss="modal" aria-hidden="true" style="margin-top: -10px;">×</button><div class="bootbox-body">Incident 1946721 Updated</div></div>
Use below code :
String value = driver.findElement(By.xpath("//div[#class='bootbox-body']")).getText();
if(value.startsWith("Incident") && value.endsWith("Updated")) {
System.out.println("Test Pass");
}else {
System.out.println("Test Fail");
}
Let me know if you have further query.

How to get the list in controller through jQuery

I have a jsp page , from which I am getting the list of the checkboxes selected by a user.
And as per the selected checkboxes I want to call my controller using the jQuery.
<input type="checkbox" name="XYZ" value="hello1"/>Hello1<br>
<input type="checkbox" name="XYZ" value="hello2"/>Hello2<br>
I obtained all the values in the jQuery and set all the parameters into a variable as given below.
var allVals = [];
$("input[name=XYZ]:checked").each(function() {
allVals.push($(this).val());
});
and I am using an AUI to call my controller, I am passing the value as parameter.
AUI().use('aui-base',
'aui-io-request',
'liferay-portlet-url',
function(A) {
var A = AUI();
var myResourceURL = Liferay.PortletURL.createResourceURL();
myResourceURL.setResourceId('getABC');
myResourceURL.setParameter("action",'ZYX');
myResourceURL.setPortletId(pid);
myResourceURL.setParameter("list",allVals);
A.io.request(myResourceURL.toString(),
{
method : 'post',
dataType : 'text',
on : {
start : function() {
.
.
.});
And in my controller I want to obtain the list and my controller is like this:
System.out.println(request.getParamter("list"));
This statement is just giving me the first selected element, not the complete list of the elements.
Is there something I am missing , or any other way to do it??
I think you will need to go with 'getParameterNames()' instead http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#getParameterNames()
Hope it helps

Html Slurping in Groovy

I am trying to parse HTML that comes to me as a giant String. When I get to Line 13, NodeChild page = it.parent()
I am able to find the key that I am looking for, but the data comes to me like This Is Value One In My KeyThis is Value Two in my KeyThis is Value Three In My Key and so on. I see a recurring trend where the seperator between the two is always UppercaseUppercase (withoutSpaces).
I would like to put it into an ArrayList one way or another. Is there a method that I am missing from the docs that is able to automatically do this? Is there a better way to parse this together?
class htmlParsingStuff{
private def slurper = new XmlSlurper(new Parser())
private void slurpItUp(String rawHTMLString){
ArrayList urlList = []
def htmlParser = slurper.parseText(rawHTMLString)
htmlParser.depthFirst().findAll() {
//Loop through all of the HTML Tags to get to the key that I am looking for
//EDIT: I see that I am able to iterate through the parent object, I just need a way to figure out how to get into that object
boolean trigger = it.text() == 'someKey'
if (trigger){
//I found the key that I am looking for
NodeChild page = it.parent()
page = page.replace('someKey', '')
LazyMap row = ["page": page, "type": "Some Type"]
urlList.add(row)
}
}
}
}
I can't provide you with working code since I don't know your specific html.
But: don't use XmlSlurper for parsing HTML, HTML is not well formed and therefor XmlSlurper is not the right tool for the job.
For HTML use a library like JSoup. You will find it much easier to use especially if you have some JQuery knowledge. Since you didn't post your HTML snippet I made up my own example:
#Grab(group='org.jsoup', module='jsoup', version='1.10.1')
import org.jsoup.Jsoup
def html = """
<html>
<body>
<table>
<tr><td>Key 1</td></tr>
<tr><td>Key 2</td></tr>
<tr><td>Key 3</td></tr>
<tr><td>Key 4</td></tr>
<tr><td>Key 5</td></tr>
</table>
</body>
</html>"""
def doc = Jsoup.parse(html)
def elements = doc.select('td')
def result = elements.collect {it.text()}
// contains ['Key 1', 'Key 2', 'Key 3', 'Key 4', 'Key 5']
To manipulate the document you would use
def doc = Jsoup.parse(html)
def elements = doc.select('td')
elements.each { oldElement ->
def newElement = new Element(Tag.valueOf('td'), '')
newElement.text('Another key')
oldElement.replaceWith(newElement)
}
println doc.outerHtml()

Setting id attribute on input field using Wicket MultiFileUploadField

In my panel class I have the following code:
private Fragment fileUploadField(String id, UploadFeedbackPanel feedbackPanel, ComponentFeedbackPanel componentFeedbackPanel) {
String uploadType = isJSEnabled ? "multiple" : "single";
Fragment uploadFragment = new Fragment( "uploadContainer", uploadType, this );
if (isJSEnabled) {
multipleUpload = new MultiFileUploadField( id, new PropertyModel<Collection<FileUpload>>( this, "multiUploads" ), MAX_FILES );
uploadFragment.add( multipleUpload = multipleUpload);
multipleUpload.add( newOnChangeAjaxBehavior( feedbackPanel, componentFeedbackPanel ) );
} else {
uploadFragment.add( singleUpload = new FileUploadField( id ) );
singleUpload.add( newOnChangeAjaxBehavior( feedbackPanel, componentFeedbackPanel ) );
}
return uploadFragment;
}
I want to add a label for this field but I'm unable to get the actual input fields ID. You can see this working for the single upload field because the input field itself is render without any surrounding elements. This however doesn't seem to be exposed when using MultiFileUploadField.
An alternative acceptable answer would be using FileUploadField and a collection of files with the multiple=true attribute. However I am unsure how to limit the number of files to be MAX_FILES only.
<label wicket:for="file"><wicket:msg key="file">File:</wicket:msg></label>
<div wicket:id="uploadContainer" class="col-right">[upload fragment shows here]</div>
<wicket:fragment wicket:id="single">
<input wicket:id="file" type="file"/>
</wicket:fragment>
<wicket:fragment wicket:id="multiple">
<div wicket:id="file" class="mfuex"></div>
</wicket:fragment>
Wicket version 6.15.0.
MultiFileUploadField uses JavaScript to generate the input fields: https://github.com/apache/wicket/blob/master/wicket-core/src/main/java/org/apache/wicket/markup/html/form/upload/MultiFileUploadField.js#L91
See whether you can plug there somehow. If you find an elegant way we would be glad to include it in the next version of Wicket!
If you use 'multiple' attribute then check:
How do I limit the number of file upload in html?

How to pass the parameter using showModalDialog

I have one question.
I write the following in a JSP file.
<s:url id="printURL" action="actMod" method="printout">
<s:param name="txt_ActEdit_accid"><s:property value="%{txt_ActEdit_accid}" /></s:param>
<s:param name="txt_ActEdit_accffname"><s:property value="%{txt_ActEdit_accffname}" /></s:param>
<s:set var="loginPassword"><%=OpeCommon.LOGIN_PASSWORD %></s:set>
<s:param name="%{#loginPassword}"><%=OpeCommon.encriptPassword(p_userID, p_passCode)%></s:param>
</s:url>
<s:submit name="btn_ActList_print" cssClass="dispLvl3 mediumbutton" value="%{getFieldName('S05AccountEdit.print_button')}"
onclick="javascript:popUp('%{printURL}','elastic',500,500);return false;"/>
I write the following in a js file.
var newWin = null;
function popUp(strURL, strType, strHeight, strWidth) {
if (newWin != null && !newWin.closed)
newWin.close();
var strOptions="";
if (strType=="console")
strOptions="resizable,height="+
strHeight+",width="+strWidth;
if (strType=="fixed")
strOptions="status,height="+
strHeight+",width="+strWidth;
if (strType=="elastic")
strOptions="toolbar,menubar,scrollbars,"+
"resizable,location,height="+
strHeight+",width="+strWidth;
newWin = window.open(strURL, 'newWin', strOptions);
newWin.focus();
}
it knows all parameter value but when I change the JavaScript function name and script coding, it does not work. I means it knows only first parameter value (txt_ActEdit_accid).
<s:submit name="btn_ActList_print" cssClass="dispLvl3 mediumbutton" value="%{getFieldName('S05AccountEdit.print_button')}"
onclick="javascript:printWindow('%{printURL}','',500,500);return false;"/>
function printWindow(x_URL, x_ARG, x_WIDTH, x_HEIGHT)
{
var x_OPT = "dialogHeight: "+x_HEIGHT+"px; "
+"dialogWidth: "+x_WIDTH+"px; "
+"edge: Raised; center: Yes; resizable: Yes; status: Yes;";
window.showModalDialog(x_URL, x_ARG, x_OPT);
}
How to fix this?
Now, you have the problem with s:url tag.
Struts tags usually escape the value of the tag. But it prevents the javascript to work correctly.
You need to add escapeAmp="false" to the s:url tag to get other parameters.

Categories