Dynamic allowed types in PrimeFaces fileUpload - jsf

My allowedTypes in PrimeFaces p:fileUpload are not static.They depend on some parameter which is passed as request parameter to the page. So how to change allowedTypes dynamically according to that request parameter.

I just tried it with defining the allowedTypes via a bean property and that is working for me.
So define a allowedTypes property in you bean
public String getAllowedTypes() {
return "/(gif|png)$/";
}
and call it in your page
<p:fileUpload fileUploadListener="#{fileUploadView.handleFileUpload}" mode="advanced" dragDropSupport="false"
update="rocomessages" sizeLimit="100000" fileLimit="3" allowTypes="#{fileUploadView.allowedTypes}" />

I have another idea. Create a String function with parameter(s), this can be used when the types must be dynamics.
This could be the String function in your Bean:
public String getAllowedTypes(String someData) {
if (value.equals("My special types")) {
return "/(\\.|\\/)(json)$/";
}
return "/(\\.|\\/)(ZIP|zip)$/";
}
and, like this, you call ir in the page:
<p:fileUpload allowTypes="#{MyBean.allowedTypes(MyBean.anotherString)}" />
Note: you can use Switch instead of IF in getAllowedTypes function, this must be understand for another uses.

Related

Preserving query-string parameters using request scoped beans [duplicate]

This question already has answers here:
Retaining GET request query string parameters on JSF form submit
(2 answers)
Closed 6 years ago.
Whenever I make an ajax call, my URL param expires. The workaround I have done is to pass a param request inside every button like in:
<p:commandLink value="Submit" actionListener="#{mybean.dosomething}">
<f:param name="foo" value="#{mybean.bar}"/>
</p:commandLink>
However, in some scenarios I can't do the above workaround. For example when I'm using primefaces rowEditEvent:
<p:ajax event="rowEditCancel" listener="#{mybean.onCancel}"/>
The param expires when I make this ajax call and results in error before invoking #{mybean.onCance} as I'm reading datatable's data from the param in URL.
So how can I maintain the param value when I make such an ajax call?
PS: mybean is ViewScoped
Problem Extension:
The <o:form> has solved part of the problem, but now I can't send params inside the form for dynamic image streaming inside the table. See the following:
<p:dataTable value="#{mybean.data}" var="var">
<p:column headerText="Thumbnail">
<p:graphicImage value="#{streamer.thumbnail}">
<f:param name="id" value="#{var.id}"/>
</p:graphicImage>
</p:column>
</p:dataTable>
Streamer Bean (RequestScoped):
public StreamedContent getThumbnail() throws IOException {
FacesContext context = FacesContext.getCurrentInstance();
if (context.getRenderResponse()) {
return new DefaultStreamedContent();
}
else {
String string = context.getExternalContext().getRequestParameterMap().get("id");
Long id = Long.parseLong(idString);
MyImage img = (MyImage) service.find(MyImage.class, id);//get resource
StreamedContent sc = new DefaultStreamedContent(img.getInputStream(), "image/jpg", img.getName());
return sc;
}
}
Here string is always null and parameter is not passed resulting in error
Register it as a <f:viewParam>
<f:viewParam name="foo" value="#{mybean.bar}" />
and instead of <h:form>, use OmniFaces <o:form> with includeViewParams="true" (which uses under the covers a custom ViewHandler implementation which automatically collects all view parameters and appends them to the outcome of the getActionURL() method which is used as form action URL):
<o:form includeViewParams="true">
...
</o:form>
This way all view params are included in the form's action URL and will therefore end up as request parameters the usual way without the need to fiddle with <f:param> and being clueless inside <p:ajax> (and <f:ajax>).

How can I maintain param on ajax call? [duplicate]

This question already has answers here:
Retaining GET request query string parameters on JSF form submit
(2 answers)
Closed 6 years ago.
Whenever I make an ajax call, my URL param expires. The workaround I have done is to pass a param request inside every button like in:
<p:commandLink value="Submit" actionListener="#{mybean.dosomething}">
<f:param name="foo" value="#{mybean.bar}"/>
</p:commandLink>
However, in some scenarios I can't do the above workaround. For example when I'm using primefaces rowEditEvent:
<p:ajax event="rowEditCancel" listener="#{mybean.onCancel}"/>
The param expires when I make this ajax call and results in error before invoking #{mybean.onCance} as I'm reading datatable's data from the param in URL.
So how can I maintain the param value when I make such an ajax call?
PS: mybean is ViewScoped
Problem Extension:
The <o:form> has solved part of the problem, but now I can't send params inside the form for dynamic image streaming inside the table. See the following:
<p:dataTable value="#{mybean.data}" var="var">
<p:column headerText="Thumbnail">
<p:graphicImage value="#{streamer.thumbnail}">
<f:param name="id" value="#{var.id}"/>
</p:graphicImage>
</p:column>
</p:dataTable>
Streamer Bean (RequestScoped):
public StreamedContent getThumbnail() throws IOException {
FacesContext context = FacesContext.getCurrentInstance();
if (context.getRenderResponse()) {
return new DefaultStreamedContent();
}
else {
String string = context.getExternalContext().getRequestParameterMap().get("id");
Long id = Long.parseLong(idString);
MyImage img = (MyImage) service.find(MyImage.class, id);//get resource
StreamedContent sc = new DefaultStreamedContent(img.getInputStream(), "image/jpg", img.getName());
return sc;
}
}
Here string is always null and parameter is not passed resulting in error
Register it as a <f:viewParam>
<f:viewParam name="foo" value="#{mybean.bar}" />
and instead of <h:form>, use OmniFaces <o:form> with includeViewParams="true" (which uses under the covers a custom ViewHandler implementation which automatically collects all view parameters and appends them to the outcome of the getActionURL() method which is used as form action URL):
<o:form includeViewParams="true">
...
</o:form>
This way all view params are included in the form's action URL and will therefore end up as request parameters the usual way without the need to fiddle with <f:param> and being clueless inside <p:ajax> (and <f:ajax>).

JSF passing a bean to a page

I have created a little controller of sorts that depending on the input could send it to a number of pages each with a different backing bean. I do this because one page, which takes a case number, verifies that case number and then passes it on to any number of pages which need a valid case number.
Here is my caseSelector page:
<h:form id="form1">
<h:inputText value="#{caseSelectorBean.caseNumber}"/>
<h:inputHidden value="#{caseSelectorBean.nextPage}"/>
<h:commandButton action="#{caseSelectorBean.gotoNext}" value="submit"/>
</h:form>
my bean correctly verifies the casenumber and fills out the bean for the next page and forwards it to that page.
//CaseSelectorBean
public String gotoNext() {
logger.debug("In the caseChooser, going to xpage");
logger.debug("caseNum=" + caseNumber);
if(!validateCaseNumber(caseNumber)) {
return "caseNotFound";
}
if(nextPage.equals("page1")) {
CaseDAO caseDAO = new CaseDAO();
caseInfo = caseDAO.getCaseInfo(caseNumber);
CaseInfoBean caseInfoBean = new CaseInfoBean();
caseInfoBean.setCaseInfo(caseInfo);
caseInfoBean.setCaseNumber(caseNumber);
FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(
"caseInfoBean", caseInfoBean);
}
logger.debug("done with gotoNext(), returning " + nextPage);
return nextPage;
}
problem is when I try to access the bean from the next page, it's empty. I can see via the constructor, it created a new one instead of using the one in the session. How would I make the page use the bean I created for it?
Thanks!
The ManagedProperty with #{param} works only for request scoped beans. Use f:viewParam for broader scopes:
<f:metadata>
<f:viewParam name="caseNumber" value="#{caseSelectorBean.caseNumber}"/>
</f:metadata>
Then you need only a simple property caseNumber with getter and setter in your bean.
   

Call action methods in controller managed beans through javascript

In order to reducing the state I am trying to reduce the no of commandButtons on my webpages.(I had large no of commandButtons shown under a long list of 20 items (6*20 = 120 commandButtons)). Thus I am trying to figure out a way, through javascript by which I can pass parameters & call the action methods in the ManagedBean controller classes. Is there any way to call action methods from javascript & pass them parameters ?
Richfaces 3.2.0.GA and XHTML as mark up
You can use javascript to call an a4j:js method, which inturn calls action method from managed bean.
The param which you need to pass can be set to a hidden variable which when set will set the value to the java variable in your bean.
<script>
function onButtonClick(){
$("#yourValue").val("value");
actionListenerMethod();
}
</script>
<a4j:jsFunction name="actionListenerMethod"
actionListener="#{yourManagedBean.actionMethod}"
oncomplete="scriptOnComplete();">
</a4j:jsFunction>
<h:inputHidden id="yourValue"
value="#{yourManagedBean.yourValue}" />
Managed bean:
public void actionMethod(ActionEvent event){
if(yourValue == "something"){
/*your action goes here*/
}
}
Otherwise you can by-pass this hidden variable by use of action param
<script>
function onButtonClick(){
actionListenerMethod("value");
}
</script>
<a4j:jsFunction name="actionListenerMethod"
actionListener="#{yourManagedBean.actionMethod}"
oncomplete="scriptOnComplete();">
<a4j:actionparam name="param1"
assignTo="#{yourManagedBean.yourValue}" />
</a4j:jsFunction>
In the later case the action param might get set only after the manged bean gets completed, in this case you can use Action atribute to call your action method instead of an action listener.
This will help you to set the param and then call the action method.
<a4j:jsFunction name="actionListenerMethod"
action="#{yourManagedBean.actionMethod}"
oncomplete="scriptOnComplete();">
<a4j:actionparam name="param1"
assignTo="#{yourManagedBean.yourValue}" />
</a4j:jsFunction>

Defining and reusing an EL variable in JSF page

Is it possible to define variable and reuse the variable later in EL expressions ?
For example :
<h:inputText
value="#{myBean.data.something.very.long}"
rendered="#{myBean.data.something.very.long.showing}"
/>
What i have in mind is something like :
<!--
somehow define a variable here like :
myVar = #{myBean.data.something.very.long}
-->
<h:inputText
value="#{myVar}"
rendered="#{myVar.showing}"
/>
Any ideas ? Thank you !
You can use <c:set> for this:
<c:set var="myVar" value="#{myBean.data.something.very.long}" scope="request" />
This EL expression will then be evaluated once and stored in the request scope. Note that this works only when the value is available during view build time. If that's not the case, then you'd need to remove the scope attribtue so that it becomes a true "alias":
<c:set var="myVar" value="#{myBean.data.something.very.long}" />
Note thus that this does not cache the evaluated value in the request scope! It will be re-evaluated everytime.
Do NOT use <ui:param>. When not used in order to pass a parameter to the template as defined in <ui:composition> or <ui:decorate>, and thus in essence abusing it, then the behavior is unspecified and in fact it would be a bug in the JSF implementation being used if it were possible. This should never be relied upon. See also JSTL in JSF2 Facelets... makes sense?
Like any view in MVC, the page should be as simple as possible. If you want a shortcut, put the shortcut into the controller (the #ManagedBean or #Named bean).
Controller:
#Named
public MyBean
{
public Data getData()
{
return data;
}
public Foo getFooShortcut()
{
return data.getSomething().getVery().getLong();
]
}
View:
<h:inputText
value="#{myBean.fooShortcut}"
rendered="#{myBean.fooShortcut.showing}"
/>

Resources