GeeCON Prague 2014*
The moose likes JSF and the fly likes passing parameter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » JSF
Bookmark "passing parameter" Watch "passing parameter" New topic
Author

passing parameter

ryo gin
Greenhorn

Joined: Jan 28, 2010
Posts: 7
hi,

I am a beginner of jsf, I am studying jsf for more than two week.

My very big problem is passing parameter from jsf file to bean.
I search from the internet but still i did not find any solution that works properly.

I used several code like:
1. String param = (String)FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("FormID:paramID");
2. ExternalContext econ = FacesContext.getCurrentInstance().getExternalContext();
HttpServletRequest request = (HttpServletRequest) econ.getRequest();
System.out.println("Requested parameter: "+ request.getParameter("paramID"));

but this codes does not works.What shall i do...???

Please help me .


Thanks in advance,
*kato_tyo*
Jason Porter
Ranch Hand

Joined: Apr 26, 2007
Posts: 120
Please explain what you're trying to do. It sounds like you could use the f:param though.
ryo gin
Greenhorn

Joined: Jan 28, 2010
Posts: 7
this is my jsf file:

<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD><TITLE>Welcome to JSF</TITLE>
</HEAD>
<BODY>
<f:view>
<h:form id="AddForm">
<f:param id="par" name="param" value="JACK" />
<h:commandButton action="#{MyBean.display}" value="SAVE" />
</fieldset>
</h:form>
</f:view>

</BODY></HTML>


this is my bean: MyBean

package pack;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

import sun.reflect.ReflectionFactory.GetReflectionFactoryAction;



public class MyBean {

//MyBean bean=null;
private String fname;
private String lname;
private String mname;

public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getLname() {
return lname;
}
public void setLname(String lname) {
this.lname = lname;
}
public String getMname() {
return mname;
}
public void setMname(String mname) {
this.mname = mname;
}

public void display(){

String p = (String)FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("AddForm:par");
System.out.println("parameter: "+p);

/*AddForm is the id of the form and par is the id of the f:param*/

ExternalContext econ = FacesContext.getCurrentInstance().getExternalContext();
HttpServletRequest request = (HttpServletRequest) econ.getRequest();
System.out.println("Requested parameter: "+ request.getParameter("param"));

/*param is the name of the f:param*/

HttpServletRequest request1 = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
String value = request1.getParameter("param");
System.out.println("Third try: "+value);

/*param is the name of the f:param*/

}

}

I have found this code in the internet ...???
Whats wrong...???

Please help me

*kato_ryo*
Jason Porter
Ranch Hand

Joined: Apr 26, 2007
Posts: 120
That's some really nasty code. Switch to facelets, it'll make some things easier. The f:param needs to be a child of the commandButton / commandLink component.

This is really not a great way to be doing it either. Typically you'll have a backing bean (managed bean) that you'll set values on using h:input or h:inputHidden then pull the values off of the backing bean.
ryo gin
Greenhorn

Joined: Jan 28, 2010
Posts: 7

sorry if my codes is nasty for I am only a beginner and Im sorry also if my english(grammar) is bad because Im poor in english

can you please give any sample.

Thanks in advance Mr. Jason Porter.

*kato_ryo*

Jason Porter
Ranch Hand

Joined: Apr 26, 2007
Posts: 120
I'd have to cook one up, but googling for any jsf tutorial should get you started in the right direction. If you're just starting JSF, skip the 1.x stuff and go straight to JSF 2. You can find lots of info at javaserverfaces.org
Jason Porter
Ranch Hand

Joined: Apr 26, 2007
Posts: 120
Also sorry about the "nasty code" bit, you'd said you found it from the internet. I didn't mean it as an insult, just that the code is difficult to understand, and seems to be doing things to work around what JSF has done.
ryo gin
Greenhorn

Joined: Jan 28, 2010
Posts: 7

Its ok! I understand...

by the way, thanks for the information and thank you also for the link,
Its a very good tutorial for jsf beginners

Regards,
*kato_ryo*
Lincoln Baxter
Greenhorn

Joined: Aug 25, 2008
Posts: 24
For more information on passing request-parameters between pages, I recommend two things:

1. http://java.dzone.com/articles/bookmarkability-jsf-2?page=0,1 for JSF2's new view parameters/bookmarks.

This will give a step-by-step tutorial on how to do View Parameters and Bookmarks with JSF2. Start here.

2. As an additional step, for pretty/clean URLs, try PrettyFaces: http://ocpsoft.com/prettyfaces/
3. I second Jason's link: http://www.javaserverfaces.org is a good place to start.


Lincoln Baxter, III | [url]http://ocpsoft.com/[/url] | Creator of: PrettyFaces - Bookmarks, Navigation, and Dynamic Parameters for JSF
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: passing parameter