validation - Validate f:viewParam value to be one of a select set of allowable values -
i learning jsf 2.2 , having little trouble finding answer can understand how have more validation on parameter.
i have source.xhtml file has link this:
<h:link value="alter" outcome="/main/showsqltemplates.xhtml"> <f:param name="type" value="alter" /> </h:link>
and in destination.xhtml have code looks this:
<f:metadata> <f:viewparam id="type" name="type" value="#{showsqltemplatemanagedbean.type}" required="true" requiredmessage="invalid page access. please use link menu."/> </f:metadata> <h:message for="type" class="bold"></h:message> <br/>type : #{showsqltemplatemanagedbean.type}
and bean class looks this:
@managedbean @requestscoped public class showsqltemplatemanagedbean { string type = ""; public string gettype() { return type; } public void settype(string type) { this.type = type; } public showsqltemplatemanagedbean() { } }
as can see have used "required" attribute make sure type parameter @ least there , working fine far goes. more validation.
specifically want make sure value of string type alter,insert,update or delete.
is custom validator comes in? custom validator seems on kill such simple validation check. sure missing something. perhaps put in postconstruct init() method?
just make enum
.
@managedbean @requestscoped public class showsqltemplatemanagedbean { public enum type { alter, insert, update, delete; } private type type; public type gettype() { return type; } public void settype(type type) { this.type = type; } }
jsf has builtin converter enumns kick in transparently. invalid value cause conversion error can customize via convertermessage
attribute of <f:viewparam>
.
enums have more benefits elsewhere in code, too.
unrelated concrete problem, explicitly initializing properties empty string (or null
) poor practice. don't anymore. moreover, initializing managed bean properties non-empty-string or non-null should happen in @postconstruct
annotated method.
Comments
Post a Comment