Changing Browser URL from backing bean
I would like to be able to change the text in the url bar to reflect the cur开发者_StackOverflow社区rent state of my app (like GMail does).
Mainly i'm referring to changing the url parameters to reflect the values in my form. (Once a user enters a parameter I would like to change the url parameter that corresponds)
Any idea on how to do that?
Thanks!
To the point, you want fire a HTTP GET request instead of a HTTP POST request. Changing the view side is trivial, use
<form action="targetpage.jsf">
instead of
<h:form>
In the managed bean which is associated with targetpage.jsf
you however need to do a bit more changes. JSF 1.2 doesn't offer facilities to set GET request parameters for you by the view declaration, nor does it convert/validate the parameters (JSF 2.0 has <f:viewParam>
for this).
You need to gather/convert/validate all request parameters yourself in the constructor and/or @PostConstruct
of the backing bean and invoke the action over there as well. There are basically two ways to gather the parameters:
Define the parameter as
<managed-property>
of the<managed-bean>
infaces.config.xml
so that JSF will set it for you.E.g.
<h:inputText id="input" />
(which will generate
<input type="text" id="input" name="input" />
in HTML, it's thename
attribute which is been used as request parameter name; rightclick page in browser and view source if you're unsure)with
<managed-property> <property-name>input</property-name> <value>#{param.input}</value> </managed-property>
and
private String input; // +setter
EL supports automatic conversion to primitive types and their wrappers as well, so you could for numbers also use
private Long input;
instead. The caveat is however that this would throw an ugly and unhandleable exception when the value is not parseable as a number.Or, gather them yourself by
ExternalContext#getRequestParameterMap()
.public class Bean { private String input; public Bean() { Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap(); input = params.get("input"); // ... } }
This allows for more fine-grained conversion/validation/errorhandling.
精彩评论