JSP tag - how to limit input to only numbers - jsp

<form:form method="post" commandName="registerForm" action="${action}">
<formElement:formSelectBox idKey="register.age"
labelKey="register.age" selectCSSClass="form-control"
path="titleCode" mandatory="true" skipBlank="false"
skipBlankMessageKey="form.select.empty" items="${titles}" />
<formElement:formInputBox idKey="register.firstName"
labelKey="register.firstName"
path="firstName" inputCSS="form-control"
mandatory="true" />
/>
I want age to only contain numbers and otherwise it should give an error.
But JSP does not have an onkeypressed event or type.
So, what can I do?

Related

Allowing only digits in aui:form, with Liferay portlet-model-hints.xml

My portlet-model-hints.xml below stipulates that quantity is required, that works fine.
Now I also want to stipulate that quantity must be made of digits:
<model-hints>
<model name="com.example.model.MyEntity">
[...]
<field name="order" type="long">
<validator name="required" />
<validator name="digits" /> <----- Does not work
</field>
[...]
</model>
</model-hints>
PROBLEM: Adding <validator name="digits" /> makes the text field disappear.
Is there a problem in my syntax? Should I do the validation in the JSP instead? By the way here is the JSP form to add/edit my entity:
<aui:form action="<%= editMyEntityURL %>" method="POST" name="fm">
<aui:fieldset>
[...]
<aui:input name="quantity" />
[...]
</aui:fieldset>
[....]
</aui:form>
[Workaround, I am still looking for a better solution]
Not elegant at all, but moving digits validation to the JSP works:
<aui:form action="<%= editMyEntityURL %>" method="POST" name="fm">
<aui:fieldset>
[...]
<aui:input name="quantity">
<aui:validator name="digits"/>
</aui:input>
[...]
</aui:fieldset>
[....]
</aui:form>
It must be done in all JSP forms that use the entity.

I can format a number value of a textfiled but can not update it

I am trying to update few number values on a form. Once the values are shown without formation, I can easily change them to any number and correct number will be sent to the back-end; however, if I enter a big number such as 6000000, it will be changed to 6.0E7 on browser, but correct number will be sent to the back-end. I added fmt library to format the number but when I submit the form it sends 0 to the back-end.
<%# taglib prefix="s" uri="/struts-tags"%>
<%# taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<s:textfield id="price" name="price" value="%{price}" theme="simple"/> //6.0E7
<input name="price" id="price" value='<fmt:formatNumber value='${price}'/>'/> //0
You don't need to use JSTL's fmt, Struts2 has inbuilt Formatting Utilities:
MUST READ: Using Struts2 Tags to Formatting Numbers
Then instead of
<s:textfield name="price" value="%{price}" />
<!-- output: 6.0E7 -->
use for example
<s:textfield name="price" value="%{getText('{0,number,#,##0.00}', {price})}" />
<!-- output: 6000000.00 -->
Also read about Type Conversion, and keep in mind that 6.0E7 is Scientific Notation (read more here).
How using pattern property,
<input name="price" id="price" value=
'<fmt:formatNumber value='${price}' pattern="##,####,###"/>'
/>
or type and groupingUsed like
<input name="price" id="price" value=
'<fmt:formatNumber value='${price}' type="number" groupingUsed="false"/>'
/>

How is fmt:formatNumber tag used?

I am trying to format a percentage to 2 decimal places and the code (below) I am using is not working:
<form:input
class="calculator-percentage-input"
value="<fmt:formatNumber value='${percentage}' maxFractionDigits='2' />"
path="discount" cssErrorClass="error"
/>
The value displayed in my input box is (percentage is empty):
<form:input
class="calculator-percentage-input"
value=""
path="discount" cssErrorClass="error"
/>
Can anyone tell me how to just display the value of percentage?
Solution:
<fmt:formatNumber var='myDiscountPercentage' value='${discountPercentage}' maxFractionDigits='2'/>
<form:input
class="calculator-percentage-input"
value="${myDiscountPercentage}"
path="discount" cssErrorClass="error"
/>

Filling a HTML form with properties from a Java Bean and JSP?

I have webpage with a form like this:
<html>
<head>
...
</head>
<body>
<form id="form4" name="form4" method="post" action="Receive.jsp">
<input name="inputField1" type="text"/>
...
<input type="submit" value="Send"/>
</form>
</body>
</html>
Then in my Receive.jsp I use:
<jsp:useBean id="form4" class="control.FormBean4" scope="session"/>
<jsp:setProperty name="form4" property="*"/>
To get all the input data from the form into a Bean, then I do some stuff with it. Now, what I want to do is to redirect the data from the Bean into another JSP with another form in it and fill out the input fields of that form with the properties from my Bean. My question is if there is some way to automatically fill the form like <jsp:setProperty name="form4" property="*"/> but backwards?
I already tried <jsp:getProperty name="form4" property="*"/>, but it obviously didn't work (I read somewhere in the JSP reference that this is not valid), so I'm wondering if you know some way to do this, because I have more than just one form each one with a bunch of fields and I'd like to save all the work of setting the values one by one.
I'm still a newbie in JSP and don't really know much about the JSTL or any frameworks like Struts, any help would be greatly appreciated.
My question is if there is some way to automatically fill the form like <jsp:setProperty name="form4" property="*"/> but backwards?
No. You'd need to either do it yourself, or to head to a JSP/Servlet based MVC framework like JSF, Struts, Spring-MVC, etc.
To do it yourself:
<input type="text" name="textName" value="${fn:escapeXml(bean.textName)}" />
<select name="dropdownName">
<c:forEach items="${bean.dropdownOptions}" var="dropdownOption">
<option value="${dropdownOption.key}" ${bean.dropdownName == dropdownOption.key ? 'selected' : ''}>${dropdownOption.value}</option>
</c:forEach>
</select>
<c:forEach items="${bean.checkboxOptions}" var="checkboxOption">
<c:forEach items="${bean.checkboxName}" var="checkboxName">
<c:if test="${checkboxName == checkboxOption.key}">
<c:set var="checked" value="true" />
</c:if>
</c:forEach>
<input type="checkbox" name="checkboxName" value="${checkboxOption.key}" ${checked ? 'checked' : ''}>${checkboxoption.value}<br/>
</c:forEach>
To do it with for example JSF:
<h:inputText value="#{bean.textName}" />
<h:selectOneMenu value="#{bean.dropdownName}">
<f:selectItems value="#{bean.dropdownOptions}" />
</h:selectOneMenu>
<h:selectManyCheckbox value="#{bean.checkboxName}">
<f:selectItems value="#{bean.checkboxOptions}" />
</h:selectManyCheckbox>
(which in turn also immediately minimizes the <jsp:useBean> and all Servlet code boilerplate)

Use XML-Content in <jsp:setProperty

im trying to set some Content in my JavaBean. I'm doing the following:
<x:set var="asdf123" select="$record/uuid"/>
<c:out value="${asdf123}"/> <---- this displays []
<x:out select="$record/uuid" /> <---- this displays the content from my XML
<x:out select="$asdf123" /> <---- this displays the content from my XML
<jsp:useBean id="matchBean" class="my.project.beans.MatchBean" />
<jsp:setProperty name="matchBean" property="uuid" value="${asdf123}"/> <--- this isn't working
I already put the content in a hidden input field in my form. This works just with a
<input type="radio" name="uuid" value="<x:out select="$record/uuid" />" checked="checked" />
this one works. I tried similar things for my Bean, but without any result so far.
Thank you
Markus
This one did the trick:
<x:set var="asdf123" select="string($record/uuid/text())"/>
References
jstl xml variables for select attribute
Reading XML files using JSTL
java.lang.ClassNotFoundException: org.apache.xpath.VariableStack when use jstl xml

Resources