I would like to implement a search box in my web-app. A JSF <h:form> should redirect me to products.xhtml?search=somestring. It should work the same as the following piece of HTML:
<form action="products.xhtml" method="GET">
<input type="text" name="search" />
<input type="submit" value="Submit" />
</form>
All the solutions of similar problems I came across does the redirection to another page without querystring
<h:form>
<h:inputText value="#{product.searchString}"/>
<h:commandButton value="Submit" action="products?faces-redirect=true&includeViewParams=true"/>
</h:form>
.
<h:form>
<h:inputText valueChangeListener="#{product.searchStringChanged}" />
<h:link value="Submit" outcome="products">
<f:param name="search" value="#{product.searchString}" />
</h:link>
</h:form>
OR builds the querystring but redirects to the same page.
The reason of not using pure HTML is to keep the benefits of the JSF session management.
EDIT
With the help of BalusC's post and https://blogs.oracle.com/enterprisetechtips/entry/post_redirect_get_and_jsf, I came up with following solution:
<f:metadata>
<f:viewParam name="search" value="#{product.search}" />
</f:metadata>
<h:form>
<h:inputText value="${product.search}"/>
<h:commandLink action="products?faces-redirect=true&includeViewParams=true" value="Search"/>
</h:form>
Related
I would like to implement form with file upload. I tried this.
<f:websocket channel="uploadProgress" scope="view" onmessage="updateProgressBar" />
<h:form id="form" enctype="multipart/form-data">
<h:panelGrid columns="2" styleClass="new_table">
..................
</h:panelGrid>
<div class="row">
<h:messages id="uploadMsgId" globalOnly="true" showDetail="false" showSummary="true"/>
<h:inputFile id="fileToUpload" required="true" requiredMessage="No file selected ..." value="#{newProcedure.file}"/>
<h:message showDetail="false" showSummary="true" for="fileToUpload"/>
<h:commandButton value="Upload" action="#{newProcedure.upload()}">
<f:ajax execute="#form" onevent="progressBar" render="#form"/>
</h:commandButton>
<div>
<div id="progressBarId" width="250px;" height="23"/>
</div>
</div>
<h:commandButton styleClass="button" value="Create Procedure" action="#{newProcedure.addNewProcedure(1)}">
<f:ajax render="#form" execute="#form"></f:ajax>
</h:commandButton>
</h:form>
I can upload file when I press the internal h:commandButton but when I press the second button to submit the form with the attached file nothing happens.
Any idea why it;s not working? Also if there is no selected file how I can submit the form without the attached file? I need to implement some check?
I want to create JSF form with attached file. But also to give a option form the users to submit the for without attach file. I don't want this to be mandatory.
<h:form id="form" enctype="multipart/form-data">
<div class="string">
<label class="name">
<h:inputText id="name" value="#{contacts.name}" pt:placeholder="Name*:"/>
</label>
</div>
<label class="message">
<h:inputTextarea value="#{contacts.comment}" pt:placeholder="Comment*:"/>
</label>
<h:inputFile id="fileToUpload" value="#{contacts.file}" required="true" requiredMessage="No file selected ..."/>
<h:commandButton value="Upload" action="#{contacts.upload()}">
<f:ajax execute="fileToUpload" />
</h:commandButton>
<h:message showDetail="false" showSummary="true" for="fileToUpload" style="color:red"/>
<div class="btns">
<h:commandLink id="submitlink" class="link" value="submit" action="#{contacts.sendEmail}" >
<f:ajax render="#form" execute="#form" onevent="handleDisableButton" resetValues="true"/>
</h:commandLink>
</div>
<h:outputText id="output" value="#{contacts.result}" />
</h:form>
Is there a way to implement this because now if I try to submit the form I always get message No file selected ...?
Also when I attach a file using the button upload button no message appears for successful file upload.
You have your inputFile as required. That is why you have to upload first always.
Also, if you are not seeing any message it's because you are not rendering again your h:message. Try using this in your managed bean:
FacesContext.getCurrentInstance().getPartialViewContext().getRenderIds()
.add("idMessage");
change
<h:inputFile id="fileToUpload" value="#{contacts.file}" required="true" requiredMessage="No file selected ..."/>
to
<h:inputFile id="fileToUpload" value="#{contacts.file}" />
In JSF 1.1, is it possible to use value of inputText as f:parm of an outputLink?
For example:
<h:inputText value="enter Input Value" />
<h:outputLink value="someLink.jsp" styleClass="searchButton">
<f:param name="userName" value="whatever the input value is" />
</h:outputLink>
Not with standard JSF. You'd need to bring in some JavaScript code.
E.g.
<h:form id="formId">
<h:inputText id="inputId" />
<h:outputLink value="someLink.jsp?userName=" onclick="href+=encodeURIComponent(document.getElementById('formId:inputId').value)" />
</h:form>
An alternative is to just use a plain vanilla HTML.
<f:verbatim>
<form action="someLink.jsp">
<input type="text" name="userName" />
<input type="submit" />
</form>
</f:verbatim>
You could if necessary add some CSS to make the button to look like a link.
I have below code:
<h:commandLink action="#{clController.action()}"
value="#{item.code}" >
<input type="hidden" name="address" value="#{item.address}" />
<input type="hidden" name="address" value="#{item.name}" />
<input type="hidden" name="address" value="#{item.taxDept}" />
</h:commandLink>
Page lists more than 12 links like above. What i want to do sending all these hiddens to another jsf whichever user clicks.
When I click commandLink it goes other page. But How can I show these values?
You can't use <input /> directly in JSF.
Your inputs have all the same name.
In JSF, values posted are the one inside the same <h:form /> as the action (if not specified).
You can use something simple as a parameter :
<h:commandLink action="start" actionListener="#{clController.actionListener}">
<f:attribute name="item" value="#{item}" />
</h:commandLink>
public void actionListener(ActionEvent event)
{
ClDataModel item = (ClDataModel)event.getComponent().getAttributes().get("item");
System.out.print(item.getTaxDept());
System.out.print(item.getAddress());
System.out.print(item.getName());
}
I want to pass user input to another page as a parameter. Here is my code:
<h:form>
<h:inputText value="#{indexBean.word}"/>
<h:commandLink value="Ara" action="word.xhtml">
<f:param value="#{indexBean.word}" name="word"/>
</h:commandLink>
</h:form>
Well, this is not working. I can read the inputtext value in my backing bean but I can't send it to word.xhtml.
Here is another approach I tried:
<h:form>
<h:inputText binding="#{indexBean.textInput}"/>
<h:commandLink value="Ara" action="word.xhtml">
<f:param value="#{indexBean.textInput.value}" name="word"/>
</h:commandLink>
</h:form>
This is also not working.
So, what am I doing wrong?
Your concrete problem is caused because the <f:param> is evaluated when the page with the form is requested, not when the form is submitted. So it remains the same value as on the initial request.
The concrete functional requirement is not exactly clear, but the particular functional requirement can be solved in basically two ways:
Use plain HTML.
<form action="word.xhtml">
<input type="text" name="word" />
<input type="submit" value="Ara" />
</form>
Send a redirect in action method.
<h:form>
<h:inputText value="#{bean.word}" />
<h:commandButton value="Ara" action="#{bean.ara}" />
</h:form>
with
public String ara() {
return "word.xhtml?faces-redirect=true&word=" + URLEncoder.encode(word, "UTF-8");
}