Displaying Image with Jakarta Faces, JavaBeans in Java [duplicate] - jsf

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Conditionally displaying JSF components
(3 answers)
Ajax update/render does not work on a component which has rendered attribute
(1 answer)
Closed 6 days ago.
I'm new to Java, I'm writing a webapp using Jakarta Faces and Prime faces that, based on the user's birthdate input, will display their Chinese zodiac animal and corresponding image. I've got the message working to display what the animal is, but I'm having issues getting the image to render on the web page. Any suggestions are wildly appreciated. My code is pasted below. I believe it is due to the rendering of the graphic image:
<p:outputPanel id="outputPanel">
<p:graphicImage value="#{currentPersonCalculatorRequestScopedView.zodiacSignImage}"
rendered="${currentPersonCalculatorRequestScopedView.zodiacSignImage}"
width="120px"
height="120px"
alt="Zodiac Image #{currentPersonCalculatorRequestScopedView.zodiacSignImage}"
/>
</p:outputPanel>
<div>
<p:commandButton id="buttonChineseZodiac"
value="Find Chinese Zodiac"
action="#{currentPersonCalculatorRequestScopedView.onZodiac()}"
update=":messages :growl" />
</div>
The Jakarta faces:
public void onZodiac() {
Messages.addGlobalInfo("Chinese Zodiac Symbol is {0}", currentPerson.chineseZodiac());
//zodiacSignValue = currentPerson.chineseZodiac();
if (currentPerson.chineseZodiac() == "Monkey") {
zodiacSignImage = _zodiacImages[0];
} else if (currentPerson.chineseZodiac() == "Rooster") {
zodiacSignImage = _zodiacImages[1];
} else if (currentPerson.chineseZodiac() == "Dog") {
zodiacSignImage = _zodiacImages[2];
} else if (currentPerson.chineseZodiac() == "Pig") {
zodiacSignImage = _zodiacImages[3];
} else if (currentPerson.chineseZodiac() == "Rat") {
zodiacSignImage = _zodiacImages[4];
} else if (currentPerson.chineseZodiac() == "Ox") {
zodiacSignImage = _zodiacImages[5];
} else if (currentPerson.chineseZodiac() == "Tiger") {
zodiacSignImage = _zodiacImages[6];
} else if (currentPerson.chineseZodiac() == "Rabbit") {
zodiacSignImage = _zodiacImages[7];
} else if (currentPerson.chineseZodiac() == "Dragon") {
zodiacSignImage = _zodiacImages[8];
} else if (currentPerson.chineseZodiac() == "Snake") {
zodiacSignImage = _zodiacImages[9];
} else if (currentPerson.chineseZodiac() == "horse") {
zodiacSignImage = _zodiacImages[10];
} else {
zodiacSignImage = _zodiacImages[11];
}
}

Try updating the top panel where the button is. After the action, if your java function works fine, the image should take the new value. I don't understand rendered condition of graphicImage element, it should be a boolean condition.
<div>
<p:commandButton id="buttonChineseZodiac"
value="Find Chinese Zodiac"
action="#{currentPersonCalculatorRequestScopedView.onZodiac()}"
update=":messages :growl, :outputPanel" />
</div>

Related

Problem with getEmail() Google sheet, not working for other persons?

I have a problem with getting email.
I have checked a lot of threads but I have not found the solution.
So I am the leader of the group. I am using Google Sheets for the Checklist.
I would like to simplify for every person so that it would be easier and no reason not to fulfill checklists.
basically: every time there is a checkbox clicked, automatic put in of date and email. But email works only for me. I have tried with a friend's account and I can not get it working.
is this Security Politics? Can i change it somehow?
I also updated the appsscript.json
{
"oauthScopes":
[
"https://www.googleapis.com/auth/spreadsheets.readonly",
"https://www.googleapis.com/auth/userinfo.email"
],
"timeZone": "Europe/Paris",
"dependencies": {
},
"exceptionLogging": "STACKDRIVER",
"runtimeVersion": "V8"
}
the script.
function onEdit(e) {
if(e.value != "TRUE" ) return;
e.source.getActiveSheet().getRange(e.range.rowStart,e.range.columnStart+2).setValue(new Date());
var text = e.user.getEmail();
if (text == "xxx1#gmail.com")
{
text = "xxx1";
}
else if (text == "Pxxx2#gmail.com")
{
text = "Pxxx2";
}
else if (text == "Kxxx2#gmail.com")
{
text = "Kxxx2";
}
else if (text == "kxxx1#gmail.com")
{
text = "Kxxx1";
}
else if (text == "jxxx1#gmail.com")
{
text = "Jxxx1";
}
else if (text == "jxxx2#gmail.com")
{
text = "Jxxx2";
}
else if (text == "")
{
text = "ID not found";
}
else
{
text = e.user.getEmail();
}
e.source.getActiveSheet().getRange(e.range.rowStart,e.range.columnStart+4).setValue(text);
}

How can I add a search button to this existing accordion search code?

I am using the code by Rick Sibley from the first answer on this post: Search within an accordion
Rick mentions that a search button can be added to run the script onclick, in addition to pressing enter to submit and run the search script. Can any body help me add the search 'button' functionality to this, please?
Thanks so much!
I figured it out! I feel like a genius - though I am obviously very bad at this.
Here's what I added in addition to the keyup event listener:
//Search Accordings; Highlight & Open Matching Areas ON SEARCH BUTTON CLICK
function searchBtn() {
var input, filter, i, acc, panels, txtValue, searchText, searchTitle;
input = document.getElementById("keywordSearch");
filter = input.value.toUpperCase();
acc = document.getElementsByClassName("accordion");
panels = document.getElementsByClassName("panel");
for (i = 0; i < panels.length; i++) {
for (i = 0; i < acc.length; i++) {
searchText = panels[i].textContent || panels[i].innerText;
searchTitle = acc[i].textContent || acc[i].innerText;
if (input.value !== "") {
if (searchText.toUpperCase().indexOf(filter) > -1 || searchTitle.toUpperCase().indexOf(filter) > -1) {
if (!acc[i].classList.contains("active")) {
acc[i].classList.add("active");
}
highlightIndex.apply(filter);
panels[i].style.maxHeight = panels[i].scrollHeight + "px";
panels[i].scrollIntoView({
behavior: 'smooth'
});
} else {
if (acc[i].classList.contains("active")) {
acc[i].classList.remove("active");
}
panels[i].style.maxHeight = null;
}
} else {
highlightIndex.remove();
if (acc[i].classList.contains("active")) {
acc[i].classList.remove("active");
}
panels[i].style.maxHeight = null;
}
}
}
}
With the following button added in html
<button type="submit" id="searchBtn" onclick="searchBtn();">Search</button>

Collapsing/expanding main component if any of module is enabled/disabled in joomla 2.5 custom template

I have made a custom (Joomla 2.5.20) template. I have used Bootstrap 3 framework for my front end coding. I have a 4 column layout with 3 modules and a main component. The issue is when I disable/enable any of my module the main component should collapse/expand according to the layout.
I have used the following condition in the head but not finding the result but the solution is in the condition because I am not able to make ("and / or") logic.
<?php
if ($this->countModules('Left and special and Right'))
{
$span = "col-md-3";
}
elseif (!$this->countModules('Left and special'))
{
$span = "col-md-6";
}
elseif (!$this->countModules('Left and Right'))
{
$span = "col-md-6";
}
elseif (!$this->countModules('special and Right'))
{
$span = "col-md-6";
}
else
{
$span = "col-md-12";
}
?>
The module positions in the body is correct so I think it would be appropriate not mentioning it.
Try This:
<?php
if ($this->countModules('Left and special and Right'))
{
$span = "col-md-3";
}
elseif ($this->countModules('Left and special')) || ($this->countModules('Left and Right')) || ($this->countModules('Right and special'))
{
$span = "col-md-6";
}
elseif ($this->countModules('Left)) || ($this->countModules('Right')) || ($this->countModules('special'))
{
$span = "col-md-9";
}
else
{
$span = "col-md-12";
}
?>

primefaces remoteCommand action is not getting called

I have the following code:
<p:remoteCommand name="navigateTo" actionListener="#{searchControllerHelper.dummyActionListener}"
action="#{searchControllerHelper.navigateTo}" update="searchPanelForm"
onstart="plsWaitDlg.show()" onsuccess="plsWaitDlg.hide()"/>
Controller Code as under:
Thank you Lars. Code of the bean as under:
public String navigateTo() throws FetchException {
if (null == this.getSelectedMenuLink()) {
return null;
}
if (this.getSelectedMenuLink().longValue() == 1L) {
this.prevSelectedMenuLink = this.selectedMenuLink;
this.location = null;
return searchCRE();
}
else if (this.getSelectedMenuLink().longValue() == 2L) {
this.prevSelectedMenuLink = this.selectedMenuLink;
this.selectedLoanType=null;
return searchLoans();
}
else if (this.getSelectedMenuLink().longValue() == 3L) {
this.prevSelectedMenuLink = this.selectedMenuLink;
this.selectedLoanType=null;
return searchLoans();
}
else if (this.getSelectedMenuLink().longValue() == 4L) {
this.prevSelectedMenuLink = this.selectedMenuLink;
return otherOpportunities();
}
else if (this.getSelectedMenuLink().longValue() == 5L) {
//The link needs to be 'activated' once a service provider is selected.
this.prevSelectedMenuLink= this.selectedMenuLink;
return this.navigateToHome();
}
return null;
}
I added the actionlistener to check if the controller was getting fired or not, and it is.
What is happening is the following: The page loads, and before it is completely done loading if the user clicks the button that calls this remoteCommand then strangely the actionListener gets called, but the action method does not.
Any ideas what I could do to alleviate this issue?
Thank you
Karthik

h:commandButton/h:commandLink does not work on first click, works only on second click

We have an ajax navigation menu which updates a dynamic include. The include files have each their own forms.
<h:form>
<h:commandButton value="Add" action="#{navigator.setUrl('AddUser')}">
<f:ajax render=":propertiesArea" />
</h:commandButton>
</h:form>
<h:panelGroup id="propertiesArea" layout="block">
<ui:include src="#{navigator.selectedLevel.url}" />
</h:panelGroup>
It works correctly, but any command button in the include file doesn't work on first click. It works only on second click and forth.
I found this question commandButton/commandLink/ajax action/listener method not invoked or input value not updated and my problem is described in point 9.
I understand that I need to explicitly include the ID of the <h:form> in the include in the <f:ajax render> to solve it.
<f:ajax render=":propertiesArea :propertiesArea:someFormId" />
In my case, however, the form ID is not known beforehand. Also this form will not be available in the context initally.
Is there any solution to the above scenario?
You can use the following script to fix the Mojarra 2.0/2.1/2.2 bug (note: this doesn't manifest in MyFaces). This script will create the javax.faces.ViewState hidden field for forms which did not retrieve any view state after ajax update.
jsf.ajax.addOnEvent(function(data) {
if (data.status == "success") {
fixViewState(data.responseXML);
}
});
function fixViewState(responseXML) {
var viewState = getViewState(responseXML);
if (viewState) {
for (var i = 0; i < document.forms.length; i++) {
var form = document.forms[i];
if (form.method == "post") {
if (!hasViewState(form)) {
createViewState(form, viewState);
}
}
else { // PrimeFaces also adds them to GET forms!
removeViewState(form);
}
}
}
}
function getViewState(responseXML) {
var updates = responseXML.getElementsByTagName("update");
for (var i = 0; i < updates.length; i++) {
var update = updates[i];
if (update.getAttribute("id").match(/^([\w]+:)?javax\.faces\.ViewState(:[0-9]+)?$/)) {
return update.textContent || update.innerText;
}
}
return null;
}
function hasViewState(form) {
for (var i = 0; i < form.elements.length; i++) {
if (form.elements[i].name == "javax.faces.ViewState") {
return true;
}
}
return false;
}
function createViewState(form, viewState) {
var hidden;
try {
hidden = document.createElement("<input name='javax.faces.ViewState'>"); // IE6-8.
} catch(e) {
hidden = document.createElement("input");
hidden.setAttribute("name", "javax.faces.ViewState");
}
hidden.setAttribute("type", "hidden");
hidden.setAttribute("value", viewState);
hidden.setAttribute("autocomplete", "off");
form.appendChild(hidden);
}
function removeViewState(form) {
for (var i = 0; i < form.elements.length; i++) {
var element = form.elements[i];
if (element.name == "javax.faces.ViewState") {
element.parentNode.removeChild(element);
}
}
}
Just include it as <h:outputScript name="some.js" target="head"> inside the <h:body> of the error page. If you can't guarantee that the page in question uses JSF <f:ajax>, which would trigger auto-inclusion of jsf.js, then you might want to add an additional if (typeof jsf !== 'undefined') check before jsf.ajax.addOnEvent() call, or to explicitly include it by
<h:outputScript library="javax.faces" name="jsf.js" target="head" />
Note that jsf.ajax.addOnEvent only covers standard JSF <f:ajax> and not e.g. PrimeFaces <p:ajax> or <p:commandXxx> as they use under the covers jQuery for the job. To cover PrimeFaces ajax requests as well, add the following:
$(document).ajaxComplete(function(event, xhr, options) {
if (typeof xhr.responseXML != 'undefined') { // It's undefined when plain $.ajax(), $.get(), etc is used instead of PrimeFaces ajax.
fixViewState(xhr.responseXML);
}
}
Update if you're using JSF utility library OmniFaces, it's good to know that the above has since 1.7 become part of OmniFaces. It's just a matter of declaring the following script in the <h:body>. See also the showcase.
<h:body>
<h:outputScript library="omnifaces" name="fixviewstate.js" target="head" />
...
</h:body>
Thanks to BalusC since his answer is really great (as usual :) ). But I have to add that this approach does not work for ajax requests coming from RichFaces 4. They have several issues with ajax and one of them is that the JSF-ajax-handlers are not being invoked. Thus, when doing a rerender on some container holding a form using RichFaces-components, the fixViewState-function is not called and the ViewState is missing then.
In the RichFaces Component Reference, they state how to register callbacks for "their" ajax-requests (in fact they're utilizing jQuery to hook on all ajax-requests).
But using this, I was not able to get the ajax-response which is used by BalusC's script above to get the ViewState.
So based on BalusC's fix, i worked out a very similar one. My script saves all ViewState-values of all forms on the current page in a map before the ajax-request is being processed by the browser. After the update of the DOM, I try to restore all ViewStates which have been saved before (for all forms which are missing the ViewState now).
Move on:
jQuery(document).ready(function() {
jQuery(document).on("ajaxbeforedomupdate", function(args) {
// the callback will be triggered for each received JSF AJAX for the current page
// store the current view-states of all forms in a map
storeViewStates(args.currentTarget.forms);
});
jQuery(document).on("ajaxcomplete", function(args) {
// the callback will be triggered for each completed JSF AJAX for the current page
// restore all view-states of all forms which do not have one
restoreViewStates(args.currentTarget.forms);
});
});
var storedFormViewStates = {};
function storeViewStates(forms) {
storedFormViewStates = {};
for (var formIndex = 0; formIndex < forms.length; formIndex++) {
var form = forms[formIndex];
var formId = form.getAttribute("id");
for (var formChildIndex = 0; formChildIndex < form.children.length; formChildIndex++) {
var formChild = form.children[formChildIndex];
if ((formChild.hasAttribute("name")) && (formChild.getAttribute("name").match(/^([\w]+:)?javax\.faces\.ViewState(:[0-9]+)?$/))) {
storedFormViewStates[formId] = formChild.value;
break;
}
}
}
}
function restoreViewStates(forms) {
for (var formIndexd = 0; formIndexd < forms.length; formIndexd++) {
var form = forms[formIndexd];
var formId = form.getAttribute("id");
var viewStateFound = false;
for (var formChildIndex = 0; formChildIndex < form.children.length; formChildIndex++) {
var formChild = form.children[formChildIndex];
if ((formChild.hasAttribute("name")) && (formChild.getAttribute("name").match(/^([\w]+:)?javax\.faces\.ViewState(:[0-9]+)?$/))) {
viewStateFound = true;
break;
}
}
if ((!viewStateFound) && (storedFormViewStates.hasOwnProperty(formId))) {
createViewState(form, storedFormViewStates[formId]);
}
}
}
function createViewState(form, viewState) {
var hidden;
try {
hidden = document.createElement("<input name='javax.faces.ViewState'>"); // IE6-8.
} catch(e) {
hidden = document.createElement("input");
hidden.setAttribute("name", "javax.faces.ViewState");
}
hidden.setAttribute("type", "hidden");
hidden.setAttribute("value", viewState);
hidden.setAttribute("autocomplete", "off");
form.appendChild(hidden);
}
Since I am not an JavaScript-expert, I guess that this may be improved further. But it definitely works on FF 17, Chromium 24, Chrome 12 and IE 11.
Two additional questions to this approach:
Is it feasible to use the same ViewState-value again? I.e. is JSF assigning the same ViewState-value to each form for every request/response? My approach is based on this assumption (and I have not found any related information).
Does someone expect any problems with this JavaScript-code or already ran into some using any browser?

Resources