passing parameters to method
[nobr]I know I know, its been discussed heaps, but I try to implement the solutions ppl have given and I've gotten no where, as with everyone else im just trying to call a method as the result of a button press and I want that method to have access to a variable:
<h:commandButton id="login" action="#{signinhandler.login}" value="Login">
<f:param id="return_url" value="#{param.return_url}" />
</h:commandButton><br/>
So Ive got a request bean calledsigninhandler and its got a methodlogin and I want to set the paramreturn_url to that from the request argument.
In signinhandler.login I do the following as described by others:
FacesContext context = FacesContext.getCurrentInstance();
HttpServletRequest myRequest = HttpServletRequest)context.getExternalContext().getRequest();
System.out.println(myRequest.getParameter("return_url"));
and the result is alwaysnull
So what am I doing wrong?
I've also tried making the parameter managed but I always got null also.... and yes its definatly set in the request URL.
Any help at all, I hope to god its head slapper because I've been on this for over an hour now![/nobr]
[nobr]I realise that the life of the request may have terminated prior to the action therefore the param should be null by all rights but even if I try to declaire:
<h:commandButton id="login" action="#{signinhandler.login}" value="Login">
<f:param id="return_url" value="wasssup" />
</h:commandButton><br/>
I still get null, in-fact if I itterate through the parametersValues of the context I cant find anything by the name of return_url at all, this leads me to belive that I have muffed the use of the <f:param /> tag[/nobr]
How aboutString return_url = (String) FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("return_url");?Also see http://balusc.xs4all.nl/srv/dev-j2p-com.html
[nobr]Unfortunatly no, the return was still null.
Just mucking around I noticed that even if I specify a completly invalid bean name and property there are no errors given out, example:
<h:commandButton id="login" action="#{signinhandler.login}" value="Login">
<f:param id="returnUrl" value="#{this_is_not_a_real_bean_name.bad_bean_bad}" />
</h:commandButton><br/>
Should it be complaining if these values are incorrect? Does that point to a problem that might be making this appear broken? (just an idea)[/nobr]
[nobr]I've read [url]http://balusc.xs4all.nl/srv/dev-j2p-com.html[/url about a billion times now trying to get their example to work.
I just then tried using these 3 scraps to get 'something' to work:
In the jsp
<h:commandButton id="login" action="#{signinhandler.login}" value="Login">
<f:param name="paramname1" value="paramvalue1" />
<f:param name="paramname2" value="paramvalue2" />
</h:commandButton><br/>
in the bean
public String login(){
String paramvalue1 = getFacesParamValue("paramname1");
String paramvalue2 = getFacesParamValue("paramname2");
System.out.println(paramvalue1);
System.out.println(paramvalue2);
System.out.println("");
...
}
public static String getFacesParamValue(String name)
{
return (String) FacesContext
.getCurrentInstance()
.getExternalContext()
.getRequestParameterMap()
.get(name);
}
and the result:
null
null
*throws paper in the air* im all outta ideas, its as though its not even parsing my <f:param> tag. I dont know if it is important at all but the bean signinhandler is defined in the config.xml like this:
<managed-bean>
<managed-bean-name>signinhandler</managed-bean-name>
<managed-bean-class>jsfserver.handler.SignInHandler</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
[/nobr]
I think I've already seen the issue (but can't explain how I fixed it, though). What JSF runtime do you use?Just to not leave you with no working solution, give the binding attribute a try. It ought to work.Jacek
Are these tags nested by a h:form tag? This is required to get h:commandLink and commandButton to work.
I think that your bean should have managed-properties :
<managed-bean>
<managed-bean-name>signinhandler</managed-bean-name>
<managed-bean-class>jsfserver.handler.SignInHandler</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
<managed-property>
<property-name>paramname1</property-name>
<value>#{param.paramname1}</value>
</managed-property>
<managed-property>
<property-name>paramname2</property-name>
<value>#{param.paramname2}</value>
</managed-property>
</managed-bean>
Add propeties to your bean and the correct getter and setter.
So you can just get the values of parameters in the login method.
public String login() {
System.out.println(this.paramname1);
System.out.println(this.paramname2);
}
H.
Thanks guys I'll get stuck into applying all the different solutions offered, great responses so far. It appears as tho im using face 1.1.3 and im running it under java 1.4 on tomcat 5.0.
[nobr]herbin,
I had tried this approach in the past with no sucees but not being one to look a gift horse in the mouth I gave it another go using the code snippets you have supplied.
Now it all seems to work, well not really but i'll try to explain, Im using eclipse to develope this so I out a breakpoint in the setters for paramname1 and paramname2, now the DO get called but the value passed in is always null.
So using your approach there it looks like its trying to do the right things but the values defined below are not getting assigned an put in the parameterMap.
<h:commandButton id="login" action="#{signinhandler.login}" value="Login">
<f:param name="paramname1" value="paramvalue1" />
<f:param name="paramname2" value="paramvalue2" />
</h:commandButton><br/>
[/nobr]
[nobr]BalusC, they sure are inside the <h:form> tag, I'll post the complete jsp here, you may spot something I've missed (time and time again.....)
<%@ page contentType="text/html; charset=Cp1252" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Cp1252" />
<title></title>
</head>
<body>
<f:view>
<f:loadBundle basename="jsfserver.bundle.messages" var="msg"/>
<h:form id="loginform">
<h2><h:outputText value="#{msg.loginTitle}" /></h2>
<h:outputText value="#{msg.loginOption}" /><br/>
<br/>
<h:outputText value="#{msg.loginAlreadyReg}" /><br/>
<h:outputText value="#{msg.loginEmail}" />
<h:inputText id="email" value="#{customer.email}" validator="#{customer.validateEmail}" required="true" size="50"/>
<h:message for="email" showDetail="false" showSummary="true" style="color: red" /><br/>
<br/>
<h:commandButton id="login" action="#{signinhandler.login}" value="Login">
<f:param name="paramname1" value="paramvalue1" />
<f:param name="paramname2" value="paramvalue2" />
</h:commandButton><br/>
</h:form>
<h:outputText value="#{msg.loginOR}" /><br/>
<br/>
<h:commandLink id="register" action="register" value="#{msg.loginRegister}" />
<br/>
<br/>
<h:outputText value="<strong>debug</strong>" escape="false" /><br/>
12345678912345<br/>
</f:view>
</body>
</html>
[/nobr]
I believe that commandButton doesn't support <f:param/> at least in JSF 1.1.
99 percent sure that it supported in version 1.2.
Just try to use commandLink instead of commandButton and it should work.
If you will still need the button that support parameter(version 1.1) take a look:
http://jsftutorials.net/articles/actionLink.html
Vladimir Perlov
YES! Your right, after days of this the answer was a head slapper, just as I predicted!
WOW, I cant belive that was so difficult to resolve...... and the answer was that easy... wow.... it works!
*crazy scientist laugh*
Now for the intersting question does 1.2 properly work under java 1.4?
JSF 1.2 requires Java 1.5 (5.0). See http://java.sun.com/javaee/javaserverfaces/download.htmlI ever have tried to install JSF 1.2 at Java 1.4 and I couldn't get it work 100% correctly. JSF 1.2 implements a heap of EL stuff (Expression Language) which requires some Java 1.5 libs.