tags:

views:

128

answers:

2

I need to have this link:

http://myserver:/myproject/innerpage/clip.jsf&id=9099

to extract the id from a code like this:

HttpServletRequest request = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
String clipId = request.getParameter("id");

When I run it on tomcat I get:

message /OnAir/innerpage/clip.jsf&id=9099

description The requested resource (/OnAir/innerpage/clip.jsf&id=9099) is not available.

When I run it without &id=9099 it runs all right.

How can I make it run?

A: 

You first have to show us how you are sending the parameter in your JSF, is it a commandButton/Link? An outputLink? A ? Also are you using redirect=true?

Chances are you are losing the id somewhere during the request.

Zenzen
A: 

The separator character between path and query string in URL is ?, not &. The & is separator character for multiple parameters in query string, e.g. name1=value1&name2=value2&name3=value3. If you omit the ?, then the query string will be seen as part of path in URL, which will lead to a HTTP 404 page/resource not found error as you encountered.

So, this link should work http://myserver:port/myproject/innerpage/clip.jsf?id=9099


That said, there's a much better way to access the request parameter. Set it as a managed property with a value of #{param.id}.

public class Bean {

    @ManagedProperty(value="#{param.id}")
    private Long id;

    @PostConstruct
    public void init() {
        System.out.println(id); // 9099 as in your example.
    }

    // ...
}

The EL #{param.id} returns you the value of request.getParameter("id").

A tip: whenever you need to haul the "raw" Servlet API from under the JSF hoods inside a managed bean, always ask yourself (or here at SO): "Isn't there a JSF-ish way?". Big chance you're unnecessarily overcomplicating things ;)

BalusC
thanks. I was idiot
Odelya
You're welcome. No, you just have to start somewhere. I did the same mistake over 10 years ago. Learn from your mistakes. Remember them for whenever you encounter the same problem symptoms again. The lessons learnt are valuable for the future.
BalusC
Actually, it wouldn't work for me. My bean scope is view and I get the nasty exception:The scope of the object referenced by expression #{param.id}, request, is shorter than the referring managed beans (clipBean) scope of view
Odelya
If you want to process **request** parameters on **every request**, your bean should logically be **request** scoped. A view scoped bean isn't constructed on every request. Maybe you'll stick to your "hack" or create a separate request scoped bean and inject the view scoped one in the new request scoped bean.
BalusC