I have a form where depending on the select state ([checkboxOn/checkboxOff]) a check box should appear or not. When the check box is displayed it should be checked by default.
How to handle that situation, taking into account that
when select is in 'checkboxOff'
state, I would have
MyFormObject.isCheckboxOn == false;
when select is in 'checkboxOn' state,
the value should be as in request?
All of this should work also on Validation errors postback, and when new form is shown and on valid form case.
Also, I would like to avoid using JavaScript on client side.
Here is some code, which needed to be extended:
class MyFormObject {
private String selectValue;
private boolean isCheckboxOn;
...
}
and two Spring controller's method:
#RequestMapping(method = RequestMethod.GET)
public ModelAndView showForm() {
return new ModelAndView('/form.jsp', 'command', new MyFormObject());
}
#RequestMapping(method = RequestMethod.POST)
public ModelAndView processSubmit(BindingResult result, MyFormObject command) {
if (result.hasErrors()) {
return new ModelAndView('/form', 'command', command);
}
...
return new ModelAndView('redirect:/success.jsp');
}
If you want to exclude the checkbox field from your view when isCheckboxOn is set to false, you can surround the checkbox field with a c:if in your JSP:
<c:if test="isCheckboxOn">
(your checkbox <input> tag goes here)
</c:if>
To check the box by default, you can use Spring's form tag library to bind directly to the checkbox field in the model, or you can use another c:if to add a checked to your <input> tag.
Related
I have a selectbox and want to load the value and text into the template, similar to an HTML dropdown box. I am using ZK framework with Java on the back end.
<selectbox id="buListbox" model="${$composer.buModel}" >
<template name="model">
<label value="${each}" />
</template>
</selectbox>
When using ZK, you don't need the value to identify the selected object like in HTML.
When using the MVC pattern, binding a model via model attribute, the selected item is also stored in that model and can be retrieved in java via model.getSelection().
Furthermore, a model is not restricted to lists of String, but it can hold any object type you want. In the template section, you can display any property of that object. Then the properties' toString() method is used to get the value which is displayed. This also applies to ${each} itself.
Example:
Assuming your model is a ListModelList of type ValueType:
public class ValueType {
private String value;
private String text;
public ValueType(String value, String text) {
this.value=value;
this.text=text;
}
public String getText() {
return this.text;
}
public String getValue() {
return this.value;
}
}
private ListModelList<ValueType> typesModel;
public ListModelList<ValueType> getTypesModel() {
return typesModel;
}
You than can use the selectbox's model/template to display it's text property:
<selectbox id="typesSelectbox" model="${$composer.typesModel}">
<template name="model">
${each.text}
</template>
</selectbox>
In java, you then get the selected item via typeModel.getSelection() .
Here you can find a working ZKFiddle example.
I want to create input forms which validate user input and prevent the model from being saved with invalid data. I have been using databinding which works up to a point but my implementation is not as intuitive as I would like.
Imagine an input which contains '123' and the value must not be empty. The user deletes the characters one by one until empty. The databinding validator shows an error decoration.
However, if the user saves the form and reloads it, then a '1' is displayed in the field - i.e. the last valid input. The databinding does not transmit the invalid value into the model.
I have a ChangeListener but this is called before the databinding so at that point the invalid state has not been detected.
I would like the error to be displayed in the UI but the model remains valid (this is already so). Also, for as long as the UI contains errors, it should not be possible to save the model.
/**
* Bind a text control to a property in the view model
**/
protected Binding bindText(DataBindingContext ctx, Control control,
Object viewModel, String property, IValidator validator)
{
IObservableValue value = WidgetProperties.text(SWT.Modify).observe(
control);
IObservableValue modelValue = BeanProperties.value(
viewModel.getClass(), property).observe(viewModel);
Binding binding = ctx.bindValue(value, modelValue, getStrategy(validator), null);
binding.getTarget().addChangeListener(listener);
ControlDecorationSupport.create(binding, SWT.TOP | SWT.LEFT);
return binding;
}
private UpdateValueStrategy getStrategy(IValidator validator)
{
if (validator == null)
return null;
UpdateValueStrategy strategy = new UpdateValueStrategy();
strategy.setBeforeSetValidator(validator);
return strategy;
}
private IChangeListener listener = new IChangeListener()
{
#Override
public void handleChange(ChangeEvent event)
{
// notify all form listeners that something has changed
}
};
/**
* Called by form owner to check if the form contains valid data e.g. before saving
**/
public boolean isValid()
{
System.out.println("isValid");
for (Object o : getDataContext().getValidationStatusProviders())
{
ValidationStatusProvider vsp = (ValidationStatusProvider) o;
IStatus status = (IStatus)vsp.getValidationStatus()
.getValue();
if (status.matches(IStatus.ERROR))
return false;
}
return true;
}
Your best bet is to steer clear of ChangeListeners - as you've discovered, their order of execution is either undefined or just not helpful in this case.
Instead, you want to stick with the 'observable' as opposed to 'listener' model for as long as possible. As already mentioned, create an AggregateValidationStatus to listen to the overall state of the DataBindingContext, which has a similar effect to your existing code.
Then you can either listen directly to that (as below) to affect the save ability, or you could even bind it to another bean.
IObservableValue statusValue = new AggregateValidationStatus(dbc, AggregateValidationStatus. MAX_SEVERITY);
statusValue.addListener(new IValueChangeListener() {
handleValueChange(ValueChangeEvent event) {
// change ability to save here...
}
});
You can use AggregateValidationStatus to observe the aggregate validation status:
IObservableValue value = new AggregateValidationStatus(bindContext.getBindings(),
AggregateValidationStatus.MAX_SEVERITY);
You can bind this to something which accepts an IStatus parameter and it will be called each time the validation status changes.
I have a Note element in my zend framework 1 form used for registration. It is defined in the format:
$captcha_reload = new Zend_Form_Element_Note('captcha_reload',
array('value'=>"<div id='captcha_reload_div'>
<a href='javascript:return false;'
id='change-image'>Change text.</a></div>"));
$this->addElement($captcha_reload);
This element displays a hyperlink and displays perfectly during registration page call.
The problem is during form submission. This note element doesn't displays anything (ie missing the hyperlink) if there is form validation error.
I have checked and tried the code below:
$this->setDefaults(array('captcha_reload'=>"<div id='captcha_reload_div'>
<a href='javascript:return false;'
id='change-image'>Change text.</a></div>"));
But still there is no value if there is form validation error.
For Note element, I have included the following in the Zend Registration Form page:
class Zend_Form_Element_Note extends Zend_Form_Element_Xhtml
{
public $helper = 'formNote';
}
When the form is submitted it is over-riding the value property of your element. As there is nothing being submitted, when the form is echoed again to show form errors, the value of the element is nothing as well.
Perhaps adding an isValid function to the element?
// pseudo-code
public function isValid($value, $context = null) {
$this->_setValue("<div id='captcha_reload_div'><a href='javascript:return false;' id='change-image'>Change text.</a></div>");
return true;
}
This will reset the value to your custom text, and return true without doing any checks (as you know the value is what you want it to be). Subsequently, when the form echos again it will show the value as set in isValid
class Zend_Form_Element_Note extends Zend_Form_Element_Xhtml
{
public $helper = 'formNote';
public function isValid($value, $context = null)
{
return true;
}
}
I have added that isValid() into Note class and it works fine. It doesn't need to use _setValue() inside Note class.
I am trying to generate an export file out of some items, which should conform to some "criteria" in order to be able to be exported. The thing is: the user should select some items (by using checkboxes) and then click on the ICEFaces' OutputResource in order to export (hopefully all) the selected items.
The parts involved in this process are the following:
The OutputResource in the XHTML:
<ice:outputResource rendered="#{not myBackingBean.emptySelection}" resource="#{myBackingBean.excelResource}" label="export to Excel" shared="false" target="_self" />
The backing bean holding the resource:
#ManagedBean(name = "myBackingBean")
#ViewScoped
#WindowDisposed
public class MyBackingBean implements Serializable
{
...
private ExcelResource resource;
...
}
And, finally, the actual resource:
...
import com.icesoft.faces.context.Resource;
...
public class ExcelResource implements Resource
{
...
#Override
public InputStream open() throws IOException
{
//do some selection here. If there is no valid ticket to export then
//this method will return null, otherwise it will return an InputStream
//and everything will work properly
if (everythingOk)
{
return new ByteArrayInputStream(...);
}
//hopefully, it won't get to this point
return null;
}
As you can see, I'm implementing the com.icesoft.faces.context.Resource interface and overriding the open() method to create the Excel export "on the fly".
Now, once againg, what I want to do is to filter some of the originally selected items and, in case no item is left, navigate to some error page. If this was an h:commandButton or an ice:commandLink then I would use the action property to do it, but I cannot do this here because this is an ice:outputResource. Is there some workaround for this?. Please notice it is not enough to use the rendered property to do this because the user can select something (which will immediately render the ice:outputResource) but the selection should be filtered before exporting it.
Last but not least: I'm using Websphere 8 and ICEFaces 3 to do this.
Thanks in advance!
I've managed to do what I was looking for, this time using some buttons with real actions to redirect to pages.
What I did was the following:
MyBackingBean has now a method to determine whether or not the selection is empty;
It also has a method to determine if the selection is valid (this is: not empty and without any invalid item in it);
There is an <ice:commandLink> rendered when the selection is invalid. This commandLink has the actual redirection to the error page.
The <ice:outputResource> will be rendered when the <ice:commandLink> is not rendered (i.e.: when the selection is completely valid).
And now, the code:
First, the commandLink:
<ice:commandLink rendered="#{not myBackingBean.validSelection}"
disabled="#{myBackingBean.emptySelection}"
label="download excel report}"
action="redirect_to_error_page" />
Remember:
MyBackingBean.isEmptySelection() returns true if there is no item selected.
MyBackingBean.isValidSelection() returns true if there is at least one item selected and also each and every selected item is a valid item (this is: an item that is valid for export)
Now, the outputResource:
<ice:outputResource
rendered="#{myBackingBean.validSelection}"
resource="#{myBackingBean.excelResource}"
label="download excel report}" shared="false" target="_self" />
Last but not least, you may have figured out the fact that the <ice:outputResource> will now handle only valid selection (the actual redirection to the error page is being done by the <ice:commandLink>). This means there has to be a way to filter the items before passing them to the resource for the actual export. Well, in my case I decided to create a filter(...) method in the backing bean.
#ManagedBean(name = "myBackingBean")
#ViewScoped
#WindowDisposed
public class MyBackingBean implements Serializable
{
...
private ExcelResource resource;
...
public List<MyItems> getFilteredList(List<MyItems> allSelectedItems)
{
...
//do some selection here and return a list containing only valid items
return validItemsList;
}
public ExcelResource getExcelResource()
{
return new ExcelResource(getFilteredList(allSelectedItems));
}
public boolean isEmptySelection()
{
//return true if the selection is EMPTY, false otherwise.
}
public boolean isValidSelection()
{
//return true if the selection is NOT EMPTY and it has
//only VALID items in it, false otherwise.
}
}
This way you can generate the ExcelResource "on the fly" with nothing but valid items in it. By the time the <ice:outputResource> is being rendered in the XHTML, it will contain only valid and exportable items!.
I hope someone will find this useful :)
If there's a form, and has a textbox and a button, how do you erase the content of the textbox after you submit the form?
<h:inputText id="name" value="#{bean.name}" />
<h:commandButton id="submit" value="Add Name" action="#{bean.submit}" />
After I enter a value in the textbox and submit, the value still appears in the textbox. I need to clear the content of the textbox once its been submitted. How can I achieve this?
Introduction
There are several ways to achieve this. The naive way is to simply null out the fields in backing bean. The insane way is to grab JS/jQuery for the job which does that after submit or even during page load. Those ways only introduces unnecessary code and indicates a thinking/design problem. All you want is just starting with a fresh request/page/view/bean. Like as you would get with a GET request.
POST-Redirect-GET
The best way is thus to just send a redirect after submit. You probably already ever heard of it: POST-Redirect-GET. It gives you a fresh new GET request after a POST request (a form submit), exactly as you intended. This has the additional benefit that the previously submitted data isn't re-submitted when the enduser ignorantly presses F5 afterwards and ignores the browser warning.
There are several ways to perform PRG in JSF.
Just return to same view with faces-redirect=true query string. Assuming a /page.xhtml, you could do so in action method:
public String submit() {
// ...
return "/page.xhtml?faces-redirect=true";
}
If you're still fiddling around with navigation cases the JSF 1.x way, then it's a matter of adding <redirect/> to the navigation case in question. See also How to make redirect using navigation-rule.
To make it more reusable, you can obtain the view ID programmatically:
public String submit() {
// ...
UIViewRoot view = FacesContext.getCurrentInstance().getViewRoot();
return view.getViewId() + "?faces-redirect=true";
}
Either way, if you've view parameters which needs to be retained in the request URL as well, then append &includeViewParams=true to the outcome. See also Retaining GET request query string parameters on JSF form submit.
If you're making use of some URL rewriting solution which runs outside JSF context, then you'd best grab the current request URL (with query string) and use ExternalContext#redirect() to redirect to exactly that.
public void submit() throws IOException {
// ...
ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
StringBuffer requestURL = ((HttpServletRequest) ec.getRequest()).getRequestURL();
String queryString = ((HttpServletRequest) ec.getRequest()).getQueryString();
ec.redirect((queryString == null) ? requestURL.toString() : requestURL.append('?').append(queryString).toString());
}
It's only a mess which should really be refactored to some utility class.
Request/View scoped bean
Note that this all works only nicely in combination with request or view scoped beans. If you've a session scoped bean tied to the form, then the bean wouldn't be recreated from scratch. You've then another problem which needs to be solved as well. Split it into a smaller session scoped one for the session scoped data and a view scoped one for the view scoped data. See also How to choose the right bean scope?
Faces Messages
If you've a faces message to be shown as result of successful action, then just make it a flash message. See also How to show faces message in the redirected page.
public String submit() {
// ...
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(clientId, message);
context.getExternalContext().getFlash().setKeepMessages(true);
return "/page.xhtml?faces-redirect=true";
}
Ajax
Only if you happen to have an ajax-only page on which a F5 would always trigger a fresh new GET request, then simply nulling out the model field(s) in action method shouldn't harm that much.
See also:
How to navigate in JSF? How to make URL reflect current page (and not previous one)
Pure Java/JSF implementation for double submit prevention
You can blank out the property of the managed bean that should not be repainted when you render the response. This can be done done using code similar to the snippet posted below:
private String name;
public String getName(){return name;}
public void setName(String name){this.name=name};
public String submit()
{
//do some processing
...
// blank out the value of the name property
name = null;
// send the user back to the same page.
return null;
}
The reason for the current behavior can be found in how the JSF runtime processes requests. All JSF requests to a view are processed in accordance with the JSF standard request-response lifecyle. In accordance with the lifecyle, the managed bean contents are updated with the value from request (i.e. the value of DataForm.Name is set) before the application event (DataForm.submit) is executed. When the page is rendered in the Render Response phase, the current value of the bean is used to render the view back to the user. Unless the value is changed in an application event, the value will always be one that is applied from the request.
You can clear the form from the Bean method that gets called when the form is submitted;`
private String name;
private String description;
private BigDecimal price;
/*----------Properties ------------*/
/*-----Getter and Setter Methods---*/
public void save()throws SQLException{
String sql = "INSERT INTO tableName(name,description,price) VALUES (?,?,?)";
Connection conn = ds.getConnection();
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, getName());
pstmt.setString(2, getDescription());
pstmt.setBigDecimal(3, getPrice());
pstmt.executeUpdate();
} catch (SQLException e) {
e.getMessage();
e.toString();
}finally{
conn.close();
clear();
}
}//End Save Method
public void clear(){
setName(null);
setDescription(null);
setPrice(null);
}//end clear`
Notice that the clear() method is called from the save method after all the operations of the save method is complete. As an option you could perform the clearing only if the methods operation was successful...The method below is placed in the ProductController Class...
public String saveProduct(){
try {
product.save();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
The method call from the view/jsp would look like the Following:
<h:commandButton value="Save" action="#{productController.saveProduct}"/>
You can do it with jQuery.
I had the similar problem. I needed to clear popup window form.
<rich:popupPanel id="newProjectDialog" autosized="true"
header="Create new project">
<h:form id="newProjectForm">
<h:panelGrid columns="2">
<h:outputText value="Project name:" />
<h:inputText id="newProjectDialogProjectName"
value="#{userMain.newProject.projectName}" required="true" />
<h:outputText value="Project description:" />
<h:inputText id="newProjectDialogProjectDescription"
value="#{userMain.newProject.projectDescription}" required="true" />
</h:panelGrid>
<a4j:commandButton id="newProjectDialogSubmit" value="Submit"
oncomplete="#{rich:component('newProjectDialog')}.hide(); return false;"
render="projects" action="#{userMain.addNewProject}" />
<a4j:commandButton id="newProjectDialogCancel" value="Cancel"
onclick="#{rich:component('newProjectDialog')}.hide(); return false;" />
</h:form>
</rich:popupPanel>
jQuery code:
$('#newProjectForm').children('input').on('click', function(){$('#newProjectForm').find('table').find('input').val('');});
I added a code snippet how to reset all values for the current ViewRoot recursively for JSF 2 here:
Reset all fields in form
This works for submitted forms showing validation errors as well as for newly entered values in a form.