Liferay + JSP + AlloyUI: Button as link and submit - java

I use Liferay 6.2 on JBoss 7.1.1.
Also I am using a JSP as view with AlloyUI.
I want a button as a link and as a submitter of a value to a method in the MVCPortlet class.
The link is inside a java array.
The attribute that is passed to the java method is inside a input field.
It works if I use just one of this two.
I tried to combine them but it doesn't work.
I tried this:
<a href="<%=unread[i][k]%>" target="_blank" >
<aui:button type="submit" />
</a>
It calls the method but it doesnt opens a new tab in my browser. In my browser I can see that it is a link, if I click right on it -> new tab, it opens it correctly in a new tab. But not if I just click on the button.
Second way:
<aui:button type="submit" onclick="window.open('http://www.google.de', '_blank', ''); return false;"/>
It just opens google but doesn't calls the method. Also it is not possible to get an Java attribute inside of a Javascript, but I could get the value out of a hidden input field. But still there would be the problem that it doesn't call the method.

I decided to get on with the second solution.
The first mistake was return false; in my onclick param, so I deleted it.
Then the submit function works as usual.
The next problem was to access a Java variable inside the javascript onclick method, so I used EL and JSTL to get it work:
<c:set var="url" scope="session" value="<%=unread[i][k]%>"/>
<aui:button type="submit" onclick="window.open('${url}', '_blank', '');" />

Related

Spring-Thymeleaf How to invoke with EL an action method from a Bean

I am using Spring/SpringMVC 5.x version, with Thymeleaf and Bootstrap on Tomcat server.
I need to ask something that maybe it might look to you very "st#pid" question.
In my html view I have the following button or a link:
<input type="button" .../>
<a .../>
I don't need to submit something, so I just use a simple button, so I think I don't need any form for it (except if I need for this).
In this html view (because of the thymeleaf library I added in the html tag), I need to add somehow,
(but I don't know how), to this button or in the link, an expression of Spring EL or Thymeleaf EL, so I can invoke a method from a
Spring bean, that I passed in the view, via a model which I added in my controller, e.g.:
${myBean.doSomething()
// or
${myBean.doSomething(parameters)
If this is not understandable I can update my question with some code (I believe that Spring developers
understand what I am talking about).
I don't know how to pass this expression. What attribute of button or link tag to use?
I used "action" attribute for the button:
<input type="button" th:action="${myBean.doSomething()".../>
or "href" attribute in the link tag:
<a th:href= "${myBean.getStringUrlAndDoSomething()"/>
Very significant info
When I started my tomcat running the page, the actions in the EL are run successfuly on the load of the page. When I pressed the button or the link nothing happened.
I know that I cannot use "onclick" attribute because there we write JS code.
But I need to run Java Spring code.
Any ideas about solving my problem?
Thanks in advance
I followed the advice of the #M.Deinum, #Wim Deblauwe, and I did not use
a button for this job. Button needs a form to work.
That is why I used a link, where the method from the bean is called like a charm, like
the following snippet:
<div class="blabla">
<div class="blablabla" th:text="|#{change_lang} EN/GR:|"></div>
<a class="bla" th:href="${localeService.switchLocale()}">
<div th:class="|${localeService.loadCss()}_blabla|"></div>
</a>
<span th:text="${#locale.getLanguage()}"></span>
</div>
And next is a snippet from the bean:
public String switchLocale() {
locale = LocaleContextHolder.getLocale();
if (locale.getLanguage().equals("en")) {
LocaleContextHolder.setLocale(EN_LOCALE);
return "?lang=el";
} else if (locale.getLanguage().equals("el")) {
LocaleContextHolder.setLocale(GR_LOCALE);
return "?lang=en";
} else {
return "";
}
}
So, the code from the bean IS invoked successfuly. I guess this is the solution to my issue.
Thanks a lot from the 2 people #M.Deinum, #Wim Deblauwe, who advised me.

Struts 2: Sending values of form fields from jsp to action class

I know it must be simple, but still I am not able to figure it out.
I have a link on a jsp page.
When this link is clicked I want another tab (of browser) to open up.
The view of this new page is defined by action class, which needs a form field value.
The problem I am facing is that I can not get the values of form fields to the action class without submitting the form. And submitting the form changes the view of the original jsp as well, which defeats the whole purpose of opening a new tab.
So I need a way to get the form field values to action class without resetting the view of original jsp page.
One way I came across was URL re-writing but that would be my last option.
Please suggest something!!
Thanks!!
Firstly I would like to point out that currently possible (to my knowledge anyway) to force a new tab to appear, it is dependent on the users' browser and the settings that they have see here for more infomation.
Now onto your question, since links cannot send form data (on their own) you have 2 options:
You can use a form "submit" button pointing to the URL you want to send the data to and to and add the target="_blank" to the form which will cause a new page to open up when the form is submitted.
You can add a javascript event to your link so that when it is pressed you append the value of the input to the URL and open a new window with that URL.
Personally I would choose the first option.
Here is a simple example of option one which doesn't remove the input value when you submit...
<html>
<body>
<form action="test1.html" method="post" target="_blank">
<input type="text" name="bob" />
<input type="submit" value="Hello"/>
</form>
</body>
</html>
You could do an ajax call, or dynamically build the link url with get parameters in it.

JSF - How do I implement a JavaScript "Are you sure?" prompt for a <h:commandButton>

In my JSF 1.2 webapp I have a page with a <h:commandButton> that invokes an action method on a backing bean. This action will cause data to be removed/replaced in the database, so I want to avoid any situations where the user accidentally clicks on the command button.
I would like to implement a simple "Are you sure?" prompt with "Yes/No" or "OK/Cancel" options using JavaScript. I'm not great with JavaScript and I have never mixed JavaScript with JSF before. Can anyone provide a code snippet to show me how to implement this?
Here is the piece of my JSP page where I declare the command button:
<h:commandButton
id="commandButtonAcceptDraft"
title="#{bundle.tooltipAcceptDraft}"
action="#{controller.actionReplaceCurrentReportWithDraft}"
image="/images/checkmark.gif">
</h:commandButton>
SOLUTION:
The solution provided by BalusC worked just fine. I wanted to also mention that it is easy to use text from a resource bundle as the prompt text. On my page, I load the resource bundle with an element like this:
<f:loadBundle basename="com.jimtough.resource.LocalizationResources" var="bundle" />
The <f:loadBundle> must be inside your <f:view>. Then I add the code provided by BalusC to my command button element but substitute a string from my resource bundle for the 'Are you sure?' text, like this:
<h:commandButton
id="commandButtonAcceptDraft"
title="#{bundle.tooltipAcceptDraft}"
action="#{controller.actionReplaceCurrentReportWithDraft}"
image="/images/checkmark.gif"
onclick="return confirm('#{bundle.confirmationTextAcceptDraft}')">
</h:commandButton>
The line in my English resource file (just a plain text file with key/value pairs) looks like this:
# text displayed in user prompt when calling confirm()
confirmationTextAcceptDraft=This will overwrite the current report and cannot be undone. Are you sure?
Use the JavaScript confirm() function. It returns a boolean value. If it returns false, then the button's default action will be blocked, else it will be continued.
<h:commandButton onclick="return confirm('Are you sure?')" />
Since it already returns boolean, there's absolutely no need to wrap it around in an if a suggested by other answers.
You could add the javascript to the onclick of the button.
<h:commandButton
id="commandButtonAcceptDraft"
title="#{bundle.tooltipAcceptDraft}"
action="#{controller.actionReplaceCurrentReportWithDraft}"
onclick="return confirm('Are you sure?')"
image="/images/checkmark.gif">
</h:commandButton>
This should work. Ideally it should be in a java script file.
<h:commandButton
id="commandButtonAcceptDraft"
title="#{bundle.tooltipAcceptDraft}"
action="#{controller.actionReplaceCurrentReportWithDraft}"
image="/images/checkmark.gif"
onclick="if (!confirm('Are you sure?')) return false">
</h:commandButton>
I'm not sure what event you'll have to listen for (onclick i would assume) as I've never used JSF. Generically speaking, this should work.
var element = document.getElementById('commandButtonAcceptDraft');
element.onclick = function(e){
return confirm('Are you sure etc...');
};

page does not refresh after an action is called

I have commandLink with an actionListener that calls a method to change the value of text. The same commandLink has an action that reloads the page. When I click the commandLink, the actionListener is called. But the action only is completed --showing the updated value of text-- when I refresh the browser. Why isn't the outputText being automatically updated?
Some code:
home.jspx
(...)
<f:view>
<table id="main_table">
<tr><td width="160px"><jsp:directive.include file="./logo.jspx" /></td>
<td><jsp:directive.include file="./header.jspx" /></td></tr>
<tr><td width="160px"><jsp:directive.include file="./vertical_navigation.jspx" /></td>
<td align="center"><ice:outputText value="main" /></ice:outputText></td></tr>
</table>
</f:view>
(...)
customer.jspx is the same, but the value of the outputText is #{customer.text}
vertical_navigation.jspx: many command links like the following:
(...)
<ice:form id="nav_form"><ice:panelGrid columns="1">
<ice:panelCollapsible expanded="true">
<f:facet name="header"><ice:panelGroup>
<ice:outputText value="Customer" />
</ice:panelGroup></f:facet>
<ice:panelGrid columns="1">
<ice:commandLink actionListener="#{client.defineText}"
immediate="true" action="customer" id="list">
<ice:outputText value="List" />
</ice:commandLink>
(...)
the bean:
(...)
public String text;
public void defineText(ActionEvent evt) {
text = ... some text related to the link
}
public String getText() {
return text;
}
Well, everything works fine, except that I have to refresh the page when I click a link so that the value of text is updated. I put some System.out.println() satatements inside the bean methods and noticed that the defineText method is called whenever I click a link. But the getText is called only after a refresh. The output is like this:
// click the link "list"
called defineText for link list
// click the link "new"
called defineText for link new
// click the link "external"
called defineText for link external
// refresh the broswer
called getText // this will show the updated value of "text" for the link "external"
// click the link "new"
// refresh the broswer
called defineText for link new
called getText // this will show the updated value of "text" for the link "new"
I'm working with JSF 1.2 and IceFaces 1.8.2.
I have finally found a sollution. I removed the JSF 1.2 Apache Myfaces and substituted by the Sun RI jars. If anyone can explain why it worked, I'll appreciate.
I don't work with IceFaces, but try to remove immediate="true" from your command link - with standard h:commandLink it could cause the problem.
Its about the cycle and how the events are handled.
from my experience, two ways to fix it:
1) queue the event (check the different type of events) until its time to update the values.
2) work around by trapping with another tag

How to use RichFaces a4j:commandButton not using submit

I have an a4j:commandButton which looks like this
<a4j:commandButton id="stopBtn" type="button" reRender="lastOp"
action="#{MyBacking.stop}" value="Stop" />
</a4j:commandButton>
When the app is deployed, and the button clicked, the stop() method is not being called. All the a4j:commandButton examples refer to forms, but this button is not in a form - it's a button the user is going to use to cause the server to run some back-end logic. At the moment, the method is
public void stopNode() {
logger.info("STOPPING");
setLastOp("Stopped.");
}
Other methods which don't use this type of button are updating the lastOp field, but I'm not seeing anything on the console with this one. Am I right to cast this as a button? Should I put this in a h:form tag?
The firebug console says:
this._form is null
which I don't understand.
Any help well appreciated.
UICommand components ought to be placed inside an UIForm component. So, your guess
Should I put this in a h:form tag?
is entirely correct :) This because they fire a POST request and the only (normal) way for that is using a HTML <form> element whose method attribute is set to "post". Firebug also says that a parent form element is been expected, but it resolved to null and thus no actions can be taken place.
Only "plain vanilla" links like h:outputLink and consorts doesn't need a form, because they just fires a GET request.
Yes, wrap it in a form. I'm sure BalusC will post a detailed explanation while I'm typing my answer. (yup, there it is)
I have to ask why you didn't just try a form first, before posting here.
Look at your code:
<a4j:commandButton id="stopBtn" type="button" reRender="lastOp" action="#{MyBacking.stop}" value="Stop" />
You finished <a4j:commandButton with />, why need that orphan </a4j:commandButton> ?
If for some reason you don't want to place the button inside a form, you can do something like this:
<a4j:commandButton onclick="fireAjax()"/>
<h:form>
<a4j:jsFunction name="fireAjax" action=".."/>
</h:form>

Categories

Resources