Ok. I'm making a java web app with a database backend to do some CRUD on some data. When the edit button is clicked next to an item, it navigates to a form with the current data for editing. One of the fields is boolean and I would like to display it as a checkbox so that True makes it checked and False leaves it unchecked.
I have tried many different variations none seem to work. Here are some examples where <%= action.get("stable")%> returns a string with either True or False
<input TYPE=checkbox name="stable" value=<%= action.get("stable") %>
<input TYPE=checkbox name="stable" value=<%= action.get("stable")?"True":"False" %><%= action.get("stable")?"checked":"" %>
<input TYPE=checkbox name="stable" checked=<%= action.get("stable")%>/>
So how do you set a check box to checked/unchecked depending on the string returned with action.get("stable")
Thank you for any help sorry if the question is a bit trivial.
I used this, and it worked perfectly.
<input type="checkbox" <c:if test="${item.estado==2}">checked=checked</c:if> class="switch-input" >
The correct markup for a checked checkbox is checked="checked". If it's not checked, the checked attribute must not be present at all.
You should generate it using the JSTL and the JSP EL, because scriptlets are something from the past which should not be used in JSPs for years. See How to avoid Java code in JSP files?.
This would of course need some refactoring so that the action bean has a regular isStable() method returning a boolean, which would be much cleaner. But anyway, here's how it would work using your existing code :
<input type="checkbox" name="stable" <%
if ("True".equals(action.get("stable"))) {
out.print("checked=\"checked\"");
} %>/>
Note that all attributes should also be surrounded by quotes.
You need to set checked attribute of <input type="checkbox"/>
Edit:
<input type="checkbox" <%=action.get("stable") ? "checked='checked'" : "" %> />
Related
I'm using Freemarker template (java) and I need to show checked checkboxes. I expect to see a checkbox marked with a regular checkmark (unicode "U+2714"), but instead of I see that my checkbox marked with question mark (U+003F). I need a regular checkmark, or even "X" is good, but not question mark "?". My code now is:
<#list labels as checklabel>
<div>
<input type="checkbox" id="checklabel" name="checklabel" checked="true">
<label for="checklabel">${checklabel}</label>
</div>
</#list>
Maybe somebody knows what I do not do right and can help me?
Write like this:-
<input type="radio" id="checklabel" name="checklabel" checked>
you have to write only checked and the type to radio.
Im trying to get the values of some td elements where the data consist of data from MySQL table. It displays the data fine in my browser (e.g. if i change type from "hidden" to "submit"), but when I try to get the value i only get null.
Here are my jsp and it displays the correct results in the browser.
<td>
<form action="history.jsp" method="get">
<input type="hidden" name="res" value="<%=his.getRes()%>"/>
</form>
</td>
When i try to print the values however, I only get "null" at of evey :
<%
String res = request.getParameter("res");
System.out.print(res);
%>
I'm still very new, so it's proberly a straight forward answer. Thank you in advance for the help.
I suggest that you change the name of your variable :
String newname = request.getParameter("res");
System.out.println(newname)
One can submit (=send) only one <form>. So one must assume there is just one single td with one <form>. Forms also may not be nested in an outer form.
It need some way to submit the form.
So experiment first with:
<td>
<form action="history.jsp" method="get">
<input type="text" name="res" value="<%=his.getRes()%>"/>
<input type="submit" value="Send"/>
</form>
</td>
This will show whether his.getRes() yielded something. And allows a manual submit in the browser.
Does anyone know how to add a default setting of checked in a Thymeleaf, SpringBoot, Java project, to an input field for a Single checkbox?
I have to use th:field because I am later submitting this form via email and if I use th:name with the checked box default, the default works but email doesn't.
Any suggestions?
<label>
<input type="checkbox" id="serviceLevel" th:field="*{serviceLevel}" th:checked="${serviceLevel}"/>
Affiliate Serviced</label>
Email html code to pull value on email:
<tr class="emailRow">
<h3>Direct: </h3>
<td class="bodycopy" th:text="${directBind.directBox}">
</td>
</tr>
I have this variable set as private Boolean directBox in the model.
I've tried everything: th:checked=checked, value=true, value=checked, set the value and checked ="${directBox}" none of this works.
Is there a solution out there?
Figured out a way to use th:field and have it default to checked. Have to set it in my controller, like Gustavo mentioned. Code example below.
...
directBind.setDirectBox(true);
directBind.setServiceLevel(true);
model.addAttribute("directBind", directBind);
return "directBind";
(Oups.. Sorry for my english :) )
In my web application, Struts2 is used as the main Servlet dispatcher and filter. But For some reasons, i have a custom filter and a custom servlet used for a specific url "/book".
But I have some commons jsp... i had some issues when the custom Servlet should display my request attributes in the JSP because of the struts tags (implemented before). So i changed these tag by the jstl taglibs and it works now.
But... In one JSP, the main (lol)... I have a search form.. This JSP is included in several JSPs and could be called by Struts and the custom Servlet..
With only Struts the tag was "< s:form>.." and when the form was submitted, all sended values was kept in the input... But now, because of the custom Servlet i use a simple html form which is calling the struts action "search.do".
As source code is below:
<form method="post" action="<c:out value="${contextPath}"/>/search.do" name="search" id="search">
<input type="text" id="search_searchWord" value="" maxlength="200" size="100" name="searchWord">
<div align="right">
<input type="submit" value="Ok" name="searchButton" id="search">
</div>
<select id="search_searchCrit" name="searchCrit">
<option value="0">Crit1</option>
<option value="1">Crit2</option>
<option value="2">Crit3</option>
</select>
</form>
My problem is the search word and the selected option are refreshed after the submit. I need to keep them !
Is there a way to use the struts taglibs with a Standard Servlet ?
Or Do you have another solution to keep the submitted information ?
Thanks all !
take each field value from the input field and write js function to fill each field in jsp source code of your page.
function selectedValue(){
var value =<%=request.getParameter("searchCrit")%>;
if(value !=null)
{
document.getElementById('search_searchCrit').innerHTML=value;
}
}
I found a solution with the help of #java_seeker.
In my Struts action, i got the request through this way :
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("searchWord", this.getSearchWord());
There is two different way to do this, see: http://www.mkyong.com/struts2/how-to-get-the-httpservletrequest-in-struts-2/
The attribute is setted in each method (in the action) that could refresh the page.
Then, i just recovered and set the attribute from the request as a variable with a jstl tag and display it as the value of my html input:
<c:set var="searchWord" value='<%=request.getAttribute("searchWord") %>' />
<input type="text" id="search_searchWor" value='<c:out value="${searchWord}" />' name="searchWord">
For the , i just used an <c:choose><c:when test=""></c:when><c:otherwise><c:otherwise></c:choose> to set the selected choice.
Now all value are always displayed. Maybe it's not the very good way to display share the same JSP between a standard servlet and a Struts action, but it works. I'm open to try a better solution if you have one! Thanks all!
I am trying to update the existing code of other developer. I am facing the problem of confusing actions.
Existing :
<s:form name="f2" action="delFood.action">
<input type="submit" value="Delete" class="button" onClick="javascript:get_check_value()"/>
My Code to Update:
<input type="file" class="button" id="foodItemFile" name="foodItemFile" value="Browse ..."/>
<input type="submit" class="button" value="AddFood" onClick="callAddFood();"/>
My Javascript:
In my script, I try to submit my action by following code.
document.f2.action = "AddFoodAction.action";
document.f2.submit();
It seems like when I click [AddFood] button, it always call the [delFood.action].
For adding food, I need to check something with javascripts before calling the [AddFoodAction.action] action.
Due to limitations, I can't change the existing code. I can only add new codes to the existing one.
So, Any way to call [AddFoodAction.action] from javascripts without confusing with other actions of the same form ?
Thanks ahead.
Looks like a javascript problem here. Make sure the code
document.f2.action = "AddFoodAction.action";
document.f2.submit();
is executing. Maybe document.f2 is not resolving correctly (maybe more than one form with this name?).
This fiddle shows that it should work. It changes the action of a form inside a onclick handler on a <input type="submit">.
And just a reccomendation, don't do document.f2.submit();. It's an <input type="submit"> so it will submit the form automatically when onclick ends.
Finally, I resolve it by doing like that.
1) There will no direct action in form tag.
<s:form name="f2" method="post" enctype="multipart/form-data">
2) for delete part,
<input type="submit" value="Delete" class="button" onClick="javascript:get_check_value()"/>
call the delete action from the javascript, not directly from form.
document.f2.action = "delFood.action";
3) for add part, like delete part. check necessary things in java scripts and call the add action. It works well.
Another Solution:
There maybe common action directly called from Form. For this approach, just name your button and give value and map those value from action class. And differentiate multiple methods by using those value from one action. I read this article at coderanch and javaSample. Thanks.