Clarification in Struts 2 - java

There is a page in which there are multiple rows with a 'Change Status' button[a 'submit' button] at the end of each row. There are more than ten status present. When I click on the 'Change Status' button, the control goes the action class and the page reloads with the changed status.
I have id's associated with each row. how do I identify in which row I have clicked the 'Change Status' button when the page reloads ? Should I pass the id through java-script to the action class ? Is there a simpler way than this ?

Well you can take advantage of Struts2 multiple submit button functionality.Like you can set submit button value based on your ID and collect that in your action to use it in your way something like
<button type="submit" value="Submit" name="buttonName">
<button type="submit" value="Clear" name="buttonName">
class MyAction extends ActionSupport {
private String buttonName;
public void setButtonName(String buttonName) {
this.buttonName = buttonName;
}
public String execute() {
if ("Submit".equals(buttonName)) {
doSubmit();
return "submitResult";
}
if ("Clear".equals(buttonName)) {
doClear();
return "clearResult";
}
return super.execute();
}
}
So in short value can be what is the uniqque id and you can use it in action as per your choice
for details
multiple-submit-buttons

You can add the id to be part of your form and that way you will receive the id as a parameter when you submit. No need for javascript.

You can do a different form for each row.
Every form can have a
<s:hidden />
with the id of the row.
In this way you always know which is the submit clicked.

Related

Invoke Java Method When Button Is Clicked

I have an old JSP application for which I cannot use Jquery or Ajax.
I have the following code snippet
function func(val){
if(val=="true"){
<%
myBean.myMethod("ABC","DET",0);
%>
myfrm.submit();
}
}
and I am calling this from a button's onClick event.
What I would like to do is invoke my Java method only when button is clicked. If page is refreshed java method should not be invoked.
How can I achieve this?
This is not possible. The java code is only executed when you refresh/load the page. Once the page is rendered, only client-side code can be executed.
You could have the button post to x.jsp and then do a redirect y.jsp
Looks like you have called your method within the jsp lifecycle.
As the jsp is parsed your method will be executed.
The functionallity you want you will most likely have to create an event.
<input type="hidden" value="" id="executeThis" onclick="document.forms[0].submit();"/>
<button id="doWork" type="button" onclick="execTheClick('executeThis', 'methodName');"/>
function execTheClick(linkId, methodName)
{
var fireOnThis = document.getElementById(linkId);
fireOnThis.value = methodName;
if (document.createEvent)
{
var evObj = document.createEvent('MouseEvents');
evObj.initEvent( 'click', true, false );
fireOnThis.dispatchEvent(evObj);
}
else if (document.createEventObject)
{
var evObj = document.createEventObject();
fireOnThis.fireEvent('onclick',evObj);
}
}
Hope that helps

Play Framework 2.x: How to make button submit for each news just once?

I'm Trying to code the button-submit for each news_id just once, i.e the user presses the button and the button does not see more
My Application.java
public static boolean flag = false;
public static Result exampleButton(Long id){
News news = News.find.byId(id);
if(!flag){
//do something
flag = true;
}
return ok();
}
My view.scala
#form(controllers.routes.Application.exampleButton(news.id)){
<button class="btn btn-default btn-xs active" id="thumbsUp" type="submit"><span class="glyphicon glyphicon-thumbs-up"></span></button>
}
But it does not work properly. If the user pressed the button for one news, this button is not available for all the news. What's wrong?
Hm, just add new boolean field to your News model ie:
static boolean alreadyClicked;
So in view you can use simple condition check:
#if(!news.alreadyClicked){
<button>Label</button>
}

Wicket - refresh component on evry Request (implementing session timeout countdown)

i have a Wicket 6.10.0 Web Application. I want to implement a timer display the remaining time in the current user session. Something link this:
http://jsfiddle.net/justinobney/uZMGD/
I have a Wicket Label in the footer of my Page. This label uses JavaScript to countdown the time from for example 5 minutes down to 0 min 0 sek. This countdown (label) should be refreshed on every request.
Now the question is how to do this?
In the init() method i added and AbstractRequestCycleListener which notifies me of every end request like this:
getRequestCycleListeners().add(new AbstractRequestCycleListener() {
#Override
public void onEndRequest(RequestCycle cycle) {
super.onEndRequest(cycle);
System.out.println("End Request - here i would like to refresh somehow the label");
}
});
but i dont know how to send this message to the Label to refresh itself. Any ideas?
* E D I T : *
I have a SPA:
<body>
lots of AJAX components here
<div wicket:id="footer">Footer always stays here. <br />
Session timeout in: <span wicket:id="sessionTimeoutLabel">countdown here</span>
</div>
</body>
The countdown works a little bit like this: http://jsfiddle.net/justinobney/uZMGD/
Now on every AJAX Request i need the sessionTimeoutLabel label to rerender to start counting from lets say 600 seconds down to 0.
Don't focus too much on the "end of request". This is about 10µs after to the render phase. Also note that the session timeout is reset after each request, so there is no need to update the label with a new value, other than to reset it to the session timeout parameter.
You should just have a parameter somewhere that dictates the session timeout you configured, and use that to show the label, if it is application specific you can create a getter in your Application class:
public MyApplication extends WebApplication {
public static final int SESSION_TIMEOUT = 30; // minutes
public int getSessionTimeout() {
return SESSION_TIMEOUT;
}
}
And in your footer you can add a label:
add(new Label("timeout", PropertyModel.of(this, "application.sessionTimeout")));
Or if it is a property of your session, you can add the getter to your custom session:
public MySession extends WebSession {
public static final int SESSION_TIMEOUT = 30; // minutes
public int getSessionTimeout() {
return SESSION_TIMEOUT;
}
}
And retrieve it in your label:
add(new Label("timeout", PropertyModel.of(this, "session.sessionTimeout")));
The expression for the property models make use of the fact that Component has a getter for the Application and a getter for the Session.

Primefaces TabMenu active tab remembered on logout

As the title explains by itself, I have an issue with managing the currently active tab in the tab menu. I'm using JSF 2.1 w/ PF 3.4. Here is the code fragment with the tab menu:
<h:form>
<p:tabMenu activeIndex="#{navigationMB.activeIndex}" >
<p:menuitem value="Početna" action="#{navigationMB.navigateStudent('home')}" icon="ui-icon-home" />
<p:menuitem value="Konsultacije" action="#{navigationMB.navigateStudent('konsultacije')}" icon="ui-icon-search" />
<p:menuitem value="Zakazivanje" action="#{navigationMB.navigateStudent('zakazivanje')}" icon="ui-icon-document"/>
<p:menuitem value="Profesori" action="#{navigationMB.navigateStudent('profesori')}"/>
<p:menuitem value="Moj profil" action="#{navigationMB.navigateStudent('profil')}" icon="ui-icon-person" />
</p:tabMenu>
</h:form>
Here is the code of the backing bean which serves for the sole purpose of navigating that tab menu:
#Named(value = "navigationMB")
#RequestScoped
public class NavigationMB {
private int activeIndex = 0;
public NavigationMB() {
}
public String navigateStudent(String page) {
System.out.println("go to page " + page);
if ("home".equals(page)) {
activeIndex = 0;
return "/student/home?faces-redirect=true";
}
if ("konsultacije".equals(page)) {
activeIndex = 1;
return "/student/allConsults?faces-redirect=true";
}
if ("zakazivanje".equals(page)) {
activeIndex = 2;
return "/student/newConsult?faces-redirect=true";
}
if ("profesori".equals(page)) {
activeIndex = 3;
return "/student/allProfessors?faces-redirect=true";
}
if ("profil".equals(page)) {
activeIndex = 4;
return "/student/profile?faces-redirect=true";
}
return "";
}
This runs fine when just browsing, but when I logout (invalidate the session) and later return with same or different user, the activeIndex is remembered. Am I not understanding something here? I suppose that the request scoped bean would be created every time there's a navigation action, and even if the user doesn't navigate anywhere, the integer I set to 0 would always point to "home" but it doesn't.
Any help would be awesome.
edit:
It seems that even without logging out, when two users (two tabs in browser) navigate around, if user 1 clicks on, for instance, tab menu item 2, and user 2 refreshes his page, user 2 will see tab menu item 2 selected as well, and vice versa.
edit2: I made a mistake with the previous edit, please forget about this above (I didn't notice that refresh on user 2 side actually loads user 1 with his view).
As discussed among the comments of the question, the bean is not recognized as being request scoped. It is created during application startup and lives as long as the application is running.
As Spring is used, using Spring annotations will resolve this issue:
#Scope("request")
public class NavigationMB {
}
For a request scoped bean, or:
#Scope("session")
public class NavigationMB {
}
to make it session scoped.

Wicket: Changing the text of an AjaxButton on submit

I'm a noob to Wicket and trying to change the text of a AjaxButton on submit. So the idea is that the for the first time the page loads, the user sees an AjaxButton labeled e.g. "1", after clicking the button, the label of the button changes to "2" and after the next click to "3" and so on...This can't be hard, but as I said, I'm a noobie when it comes to wicket. All help appreciated!
form.add(new AjaxButton("ajax-button", form)
{
#Override
protected void onSubmit(AjaxRequestTarget target, Form<?> form)
{ //how to change Button label here?
}
}
The answer is simple: use a model.
//counter field declared in page class
private int counter;
...
form.add(new AjaxButton("ajax-button", new PropertyModel<String>(this,
"counter", form)) {
#Override
protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
counter++;
target.addComponent(this);
}
});
This is probably the most important rule of Wicket: when you need something changing, use a model. This takes some time getting used to, especially if you have experience with more "traditional" frameworks, and haven't used Swing either.
N.b.: keeping the counter in your page class may not be a good idea, but the general idea is the same.
Additionally to biziclop's answer, here is a solution for text with changing parameter.
In your java code:
AjaxButton yourButton = new AjaxButton("btnId"){
//your button's implementation goes here
};
int yourVariable = 42;
Label yourLabel = new Label("labelId", new Model<String>() {
public String getObject() {
String text = MessageFormat.format(new Localizer().getString("IdForLocalizerInYourLocalizerFile", null), yourVariable);
return text;
}
})
yourButton.add(yourLabel);
In your html:
<a type="submit" wicket:id="btnId">
<span wicket:id="labelId">[This text will never be seen, will be replaced by "The var..."]</span>
</a>
Finally your localization file will contain a line like:
IdForLocalizerInYourLocalizerFile= The variable's value is {0}. It will be replaced whenever it changes and button component is added to target. Text will remain.

Categories

Resources