How to solve "Conversion Error setting value '2013-10-26' for 'null Converter'" in h:inputText with Date value? - date

when I press the insert button, I get the error indicated on the title
Conversion Error setting value '2013-10-26' for 'null Converter'
<h:form id="formulario">
<h:outputLabel for="date">Plazo</h:outputLabel>
<h:inputText id="date" required="true" requiredMessage="Campo Obligatorio" value="#{aaaNewDetalles.criterioAaa.plazo}"/>
<h:message for="date" style="color: red;"/>
<h:commandButton actionListener="#{aaaNewDetalles.add()}" value="Ingresar"/>
</h:form>
the form is managed by this class:
#ManagedBean(name = "aaaNewDetalles")
#ViewScoped
public class aaaNewDetallesBean {
private CriterioAaaController controller;
private CriterioAaa criterioAaa;
#PostConstruct
public void init(){
controller= new CriterioAaaController();
criterioAaa= new CriterioAaa();
}
public void add(){
controller.save(criterioAaa);
}
public CriterioAaa getCriterioAaa() {
return criterioAaa;
}
public void setCriterioAaa(CriterioAaa criterioAaa) {
this.criterioAaa = criterioAaa;
}
}
The object CriterioAaa:
import java.sql.Date;
#Table(name = "criterio_aaa", schema = "", catalog = "ciclos_calidad")
#Entity
public class CriterioAaa extends Entidad implements Serializable {
private Date plazo;
public Date getPlazo() {
return plazo;
}
public void setPlazo(Date plazo) {
this.plazo = plazo;
}
}

There are two problems in your current approach:
You should use java.util.Date instead java.sql.Date. JSF and other frameworks work with this type. Also, java.sql.Date extends java.util.Date but its purpose is basically for JDBC usage. More info about this: Date vs TimeStamp vs calendar?
<h:inputText> expects a String as value, and when sending the data to the managed bean, it also expects the class field is from String type as well. In cases like this, you need to use a converter to tell JSF that this String in fact represents a Date. For this, you may use <f:convertDateTime> tag component.
<h:inputText id="date" required="true" requiredMessage="Campo Obligatorio"
value="#{aaaNewDetalles.criterioAaa.plazo}">
<f:convertDateTime pattern="yyyy-MM-dd" />
</h:inputText>
As a recommendation, you may use a calendar component from third party libraries like PrimeFaces or RichFaces whose provide <p:calendar> and <rich:calendar> component respectively.

You should import the appropriate Date package :
import java.util.Date;

In Managed Bean You should use java.util.Date. and specify the converter.
e.g.
<h:inputText id="date" required="true" requiredMessage="Hire Date"
value="#{empBean.empDetail.hireDate}">
<f:convertDateTime pattern="yyyy-MM-dd" />
</h:inputText>
you can specify the pattern for DateTimeConverter also you can use dateStyle,timeStyle, type.

Related

ZK MVVM : Passing parameter to another viewmodel using <include>

What I want to do is passing value from page1.zul to page2.zul through <include> and viewModel.
From page1.zul, I have
<include processId="#bind(vm.selectedProcess.id)" src="#load('page2.zul')"></include>
And then it should pass to a viewModel in page2.zul
#Init
public void init(#ExecutionArgParam("processId") String processId){
System.out.println("processInstanceId : " + processId);
}
However, I always get a null value. Any idea how to do this thing ? page2.zul looks something like this :
<div apply="org.zkoss.bind.BindComposer"
viewModel="#id('vm') #bind('com.mrye.viewModel')">
<label value="#load(vm.processId)"></label>
</div>
First of all, make distinguish names for Id's of the VM.
Then zul :
<div apply="org.zkoss.bind.BindComposer"
viewModel="#id('vm') #init('com.mrye.viewModel', processId = parentVM.selectedProcess.id )">
And VM2:
#Init
public void init (#BindingParam("processId") MyObject processID) {
Edit after the comment :
As you can see in this fiddle it works, but your parameter has to be initialized.
If you want to have "live data" passed to other zul (not other viewmodel), you can use #ref or just use parentVM.
If the live data need's to be in the viewmodel itself, you can use a non visible textbox where you load the data from parentVM and save it in the includedVM just before some action happens.
See updated fiddle here.
Here you can check
Index.zul
<?page title="URL Parameters Test" contentType="text/html;charset=UTF-8"?>
<zk>
<window title="URL Parameters Test" border="normal">
<include src="header.zul?test=5" />
</window>
</zk>
In above code you can check it pass argument with URL here test is argument name and value=5
<?page contentType="text/html;charset=UTF-8"?>
<zk>
<window border="none" width="100%" height="100%" apply="pkg$.HeaderComposer">
<label id="lblHeader" />
<div>
Load from EL [ <label value="${param.test}" />]
</div>
</window>
</zk>
In this page we used ${param.test} to get the parameter passed in index.zul , HeaderComposer.java
import org.zkoss.zk.ui.*;
import org.zkoss.zk.ui.event.*;
import org.zkoss.zk.ui.util.*;
import org.zkoss.zk.ui.ext.*;
import org.zkoss.zk.au.*;
import org.zkoss.zk.au.out.*;
import org.zkoss.zul.*;
public class HeaderComposer extends GenericForwardComposer{
Label lblHeader;
#Override
public void doAfterCompose(Component comp) throws Exception {
try {
super.doAfterCompose(comp);
}
catch (Exception e) {
e.printStackTrace();
}
/*
* retrieve url parameters
*/
String[] parameter = (String[]) param.get("test");
if (parameter != null)
lblHeader.setValue( "Congratulations! Your parameters value is " + parameter[0] );
else
lblHeader.setValue( "No parameters found. URL should be something like http://yourserver/yoursite/main.zul?parameter=param-value" );
}
}
for more way you can check http://zkframeworkhint.blogspot.in/2014/05/zk-include-how-to-pass-and-get.html
Just be sure on VM1 the value vm.selectedProcess.id is correctly initialized and it has a value, in the VM2 add the #AfterCompose lifecycle annotation then on #Init get the value, according to your code this should work on VM2:
Long processId;
#AfterCompose
public void initAfterCompose(#ContextParam(ContextType.VIEW) Component view) {
Selectors.wireComponents(view, this, false);
}
#Init
public void init() {
//get dynamic attribut
processId = (Long) Executions.getCurrent().getAttribute("processId");
}

how to use zk annotations

I am using zk 5.0.3. I want to use the following annotation binding as the title of a "center" region of a borderlayout:
<a:bind content="entrydisplay.activeEntryCaption" /> <html />
I want to do the following:
<borderlayout>
<north title="use the above binding here">
this is north
</north>
</borderlayout>
How do I achieve the functionality such that I can wrap this binding as the value of the title?
Thanks,
Sony
You are using an outdated version of ZK data binding. It is highly recommended that you make use of the latest methodology.
The following link is the databinding section of the ZK Essential guide & Developer's Reference:
Developer's Reference Databinding
ZK Essential's Databinding
Our basic databinding consists of a POJO which follows the Java bean conventions being access from an XML based interface using annotations in attributes. For example:
Person POJO:
public class Person {
private String _firstName = "";
private String _lastName = "";
private Boolean _married = true;
public Person(){
}
public Person(String firstName, String lastName, Boolean married){
_firstName = firstName;
_lastName = lastName;
_married = married;
}
// getter and setters
public void setFullName(String f) {
// do nothing
}
public String getFullName() {
return _firstName + " " + _lastName;
}
//add more here
}
The UI file:
<?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit" ?>
<window>
<zscript><![CDATA[
//prepare the person object
import bean.Person;
Person person = new Person();
person.setFirstName("Max");
person.setLastName("Planck");
]]>
</zscript>
<grid width="400px">
<rows>
<row> First Name: <textbox value="#{person.firstName}"/></row>
<row> Last Name: <textbox value="#{person.lastName}"/></row>
<row> Full Name: <label value="#{person.fullName}"/></row>
</rows>
</grid>
</window>
The theory is described here.
i think the old way is done it like this
<borderlayout>
<north>
<attribute name="label">
<a:bind value="entrydisplay.activeEntryCaption" />
</attribute>
</north>
</borderlayout>
The new doc
The doc of [http://docs.zkoss.org/wiki/Data_binding][Data Binding]
For your specific question, annotate your component like following:
<borderlayout>
<north id="mynorth" title="#{entrydisplay.activeEntryCaption}">
this is north
</north>
</borderlayout>
Data binder will read such annotation and call the getter and setter methods to set the title of the north component for you. It will do something like:
mynorth.setTitle(entrydisplay.getActiveEntryCaption());

What's the Struts 2 equivalent of ASP.NET's Request.Form (or FormCollection)?

I'm dynamically adding textboxes to a form on my jsp page using Javascript. When that form is submitted to an action, how does my action get the values of those textboxes? (I'm using Struts 2, btw.) In ASP.NET, I was able to find them in Form.Request/FormCollection. Is there a Struts 2 equivalent? Thanks a million.
In Struts2, you create beans in the form to do submit values. In order to create the input text-box, use the <s> tag. For example :
<s:textfield name="loginBean.userName" label="UserName" required="true" />
Here loginBean is the bean passed to the jsp page when.
Bean consists of variable declarations and getters-setters for the variable.
Then in the back-end Java where the form is submitted to, you can access the same bean.
Declare getter-setter in Java and then you can access the properties of the bean.
public LoginBean getLoginBean() {
return loginBean;
}
public void setLoginBean(LoginBean loginBean) {
this.loginBean = loginBean;
}
public String authenticate() {
String username = loginBean.getUserName();
I would recommend looking at source codes of open-source Struts projects.
It sounds like you're trying to populate a dynamic list. To do that, you just have to use the [n] index syntax at the end of your Action class property name:
HTML:
<input type="text" name="yourCollection[0]" value="first value" />
<input type="text" name="yourCollection[1]" value="second value" />
<input type="text" name="yourCollection[2]" value="third value" />
Action Class:
public class YourAction extends Action {
public List<String> yourCollection;
public List<String> getYourCollection(){
return yourCollection;
}
public void setYourCollection(List<String> aCollection){
this.yourCollection = aCollection;
}
}

Can't get JSF input field value on JAVA backend

I have following UI part on JSF - it's simple search form with input field and submit:
<h:form>
<h:commandButton action="#{operation.found}" value="#{msg.search}" />
<h:inputText name="searchParam"/>
</h:form>
And correspondingly, on backend, i attempt to get value of input field next way:
public List<Store> getFound() {
String name = (String) FacesContext.getCurrentInstance()
.getExternalContext().getRequestParameterMap().get(
"searchParam");
SessionFactory sessionFactory = new Configuration().configure()
.buildSessionFactory();
HibernateTemplate hbt = new HibernateTemplate();
hbt.setSessionFactory(sessionFactory);
foundStores = hbt.find(BEAN_PATH + " WHERE name = ?",
new Object[] { name });
return foundStores;
}
And null name is passed to backend.
It seems that problem in .jsf part, but from first glance looks ok...
You must point the <h:inputText> to a managed-bean property:
<h:inputText name="searchParam" value="#{searchBean.searchParam}" />
and define in your bean:
private String searchParam;
public String getSearchParam() {..}
public void setSearchParam(String searchParam) {..}
and then use the searchParam in your getFound() method;
Of course, you need to have the bean defined as managed bean, but I assume you have done it:
<managed-bean>
<managed-bean-name>searchBean</managed-bean-name>
<managed-bean-class>mypackage.SearchBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
You can check a JSF tutorial (like this, for example)

Updating Bean from JSF dataTable

I've got this h:dataTable and form:
<h:form>
<h:dataTable value='#{bean.allData.dataItems}' var='item'>
<h:column>
<h:outputText value='#{item.id}' />
</h:column>
<h:column>
<h:inputText value='#{item.name}' />
</h:column>
</h:dataTable>
<h:commandButton value="Save" action="#{bean.saveEntries}"/>
</h:form>
So the table shows the id and a textbox with the name in. That works. When a user clicks the button, I'd like it to save any changes to the name field.
DataBean has:
private Vector<ItemBean> dataItems;
// constructor here
public void setDataItems(Vector v) {
dataItems = v;
}
public Vector getDataItems() {
return dataItems;
}
and ItemBean has...
private String id;
private String name;
// setter for both
// getter for both
Bean has a variable 'allData' of type 'DataBean'.
Bean also has a method saveEntries() which is currently blank (as I'm not sure how it works). How do I reference the inputs to set these values?
JSF has already done it during UPDATE MODEL VALUES phase. Print/debug the local variables and you'll see. All you need to do in the save method is just persisting the data in some datastore.
Also see the following articles for more background information and examples:
Using datatables.
Debug JSF lifecycle.