Reputation: 1304
I have a JSP page named "home.jsp". And i have a method named "getRunHomeDisplay".
Initially i have a button in home.jsp such that when i click on the button it will display all the item in the house.
This is the button call:
try {
log("Account selected: ");
(new action.ItemShowAction()).execute(getgetRunHomeDisplay());
} catch (Exception e) {
return "error";
}
return "success";
This is my "home.jsp" source:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%-- jsf:pagecode language="java" location="/src/pagecode/Itemtest2.java" --%> <%-- /jsf:pagecode --%><%@page
language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@taglib uri="http://www.ibm.com/jsf/html_extended" prefix="hx"%>
<%@taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<html>
<head>
<title>itemtest2</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet" type="text/css" title="Style"
href="theme/stylesheet.css">
<script type="text/javascript">
</script></head>
<script type="text/javascript">
</script>
<f:view>
<body onload="#{pc_ItemClick.doButton1Action}">
<hx:scriptCollector id="scriptCollector1">
<h:form styleClass="form" id="form1">
<hx:commandExButton type="submit" value="Submit"
styleClass="commandExButton" id="button1"
action="#{pc_ItemClick.doButton1Action}"></hx:commandExButton>
<hx:dataTableEx id="item1" value="#{itemtest.item}" var="varitem"
styleClass="dataTableEx" headerClass="headerClass"
footerClass="footerClass" rowClasses="rowClass1, rowClass2"
columnClasses="columnClass1" border="0" cellpadding="2"
cellspacing="0">
<hx:columnEx id="item2column">
<f:facet name="header">
<h:outputText styleClass="outputText" value="Item" id="item2text"></h:outputText>
</f:facet>
<h:outputText styleClass="outputText" id="item2" value="#{varitem}">
</h:outputText>
</hx:columnEx>
</hx:dataTableEx>```1
</h:form>
</hx:scriptCollector>
</body>
</f:view>
</html>
How should i call to run this method on page load instead of clicking on a button?
Upvotes: 1
Views: 10343
Reputation: 1109810
You seem to be using JSF 1.x with IBM Faces Client Framework. If it's JSF 1.0 or 1.1, just do the job in the bean's constructor. There are not really other clean ways.
public class ItemClick {
public ItemClick() {
// Do your job here.
}
// ...
}
Or if you're using JSF 1.2 on Java EE 5, then you can also annotate a random method with the @PostConstruct
annotation. It will get invoked directly after bean's construction, which may be more useful if you're doing the job depending on managed properties or injected EJBs.
public class ItemClick {
@PostConstruct
public void init() {
// Do your job here.
}
// ...
}
Note that both ways require that the bean is request scoped in order to run on every page request. A session scoped bean won't be reconstructed on every page request, but only once on first page request during the session.
A hacky way would be to create an "invisible" form and submit that by JavaScript.
<body onload="document.getElementById('formId:buttonId').click()">
<h:form id="formId" style="display: none;">
<h:commandButton id="buttonId" value="submit" action="#{pc_ItemClick.doAction}" />
</h:form>
...
</body>
The disadvantage is that this will cause a page refresh which may end up in an infinite loop if you navigate back to the same page. You might want to navigate to a different page or to add some JS condition which skips the onload function when the action is already been invoked. Something like:
<body onload="<h:outputText value="document.getElementById('formId:buttonId').click()" rendered="#{!pc_ItemClick.didAction}" />">
Upvotes: 2
Reputation: 9266
I believe that you have a ManagedBean
tied to that JSP page. If it's true, I think you can take advantage of the @PostConstruct
annotation. It would be something like this:
@ManagedBean
@RequestScoped
public class MrBean{
@PostConstruct
public void prepareView() {
// do what you need to do
}
}
This prepareView
function will always be called when the page is requested.
Upvotes: 0