• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

passing parameter

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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*
 
Author
Posts: 134
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please explain what you're trying to do. It sounds like you could use the f:param though.
 
ryo gin
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Author
Posts: 134
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Author
Posts: 134
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Author
Posts: 134
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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*
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
If you look closely at this tiny ad, you will see five bicycles and a naked woman:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic