In my table, I've a row with checkbox which tells the controller whether that particular row of the table has to be included or no. The check box does not have relation with other rows. I tried adding it in the following way :
<form:form id="fee" method="post" modelAttribute="clientForm" commandName = "clientForm"
action="<%= request.getContextPath().toString()%>/addFee.do">
<TABLE>
<tr>
<c:forEach var="type" items="${clientInfo}" varStatus="status">
<td><form:checkbox class="editable${ifeeCount}" path="includeFeeValue" value="false"/> </td>
<td>feeType<c:out value = "${status.index}"/></td>
<td>Source Fee<c:out value = "${status.index}"/></td>
<td><form:input class="editable${ifeeCount}" disabled="true" path="overriddenFee" /></td>
<td><form:errors path="overriddenFee" cssClass="error" /></td>
</c:forEach>
</tr>
</TABLE>
And in my form, I've a list private ArrayList<String> includeFeeValue;
And i'm trying to retrieve this in the spring controller class as follows :
#RequestMapping(value="/addFee.do",method = RequestMethod.POST)
protected #ResponseBody ModelAndView selectValues(#ModelAttribute("clientForm") PaswFeeMaintenanceForm MyMaintForm ) throws Exception {
for(int i=0;i<MyMaintForm.getIncludeFeeValue().size();i++){
System.out.println("Checkbox : "+MyMaintForm.getIncludeFeeValue().get(i)+ " of "+i);
}
}
Once I submit my form, it throws null pointer exception in here : MyMaintForm.getIncludeFeeValue().size() .
Could you tell me what's missing here?
Remove disabled='true' and it will work. I faced the same problem with my textfield with property disabled as true.
And also use private String[] includeFeeValue instead of List.
As your checkbox is disabled, ans disabled elements values never flow to controller or Servlet. I advise you to remove a disabled attribute. I hope so it will work.
Related
I m encountering a problem with the '\n' or in thymleaf the problem is i need to enter to a new line. I have iteretate inside the inside of cell. I m getting the content but just in one line and i want one tweet under the other inside the cell. and i saw as well some similiar topic but it is not working in my case.
This is what i m getting:
How should i manage it to go in the next line inside one cell.
The Thymleaf code:
<tr>
<td th:text="${user.id}">1</td>
<td th:text="${user.getUsername()}">Hamdo</td>
<span th:each="tweet : ${tweets}">
<td th:text="${tweet.content} " ><br/>
<br/>
</td>
</span>
The controller :
#GetMapping({"", "/", "/index", "/index.html"})
public String followers(Principal principal, Model model) {
User user=userService.getUser(principal.getName());
model.addAttribute("tweets",
tweetService.tweetsFromUser(principal.getName()));
model.addAttribute("user",user);
return "index";
}
The service class:
private List<TweetDTO> tweetsFromUser(User user) {
return tweetRepository.findAllByAuthor(user).stream().map(TweetDTO::new).collect(toList());
}
You don't need the extra span... just loop inside of the <td />, like this:
<tr>
<td th:text="${user.id}">1</td>
<td th:text="${user.username}">Hamdo</td>
<td>
<p th:each="tweet : ${tweets}" th:text="${tweet.content}" />
</td>
</tr>
The goal: adding multiple table rows dynamically based on user inputs and catch the data through controller.
What I have so far(all simplified):
POJO:
public class Item(){
String price;
String weight;
getters and setters...
}
public class ItemForm(){
List<Item> items;
getter and setter...
}
JSP:
<form:form action="/create" method="POST" modelAttribute="itemForm">
<table>
<tr>
<td><input type='text' name='price'/></td>
<td><input type='text' name='weight'/></td>
</tr>
</table>
<c:forEach items="${itemForm.items}" var="item" varStatus="status">
<tr>
<td align="center">${status.count}</td>
<td><input name="items[${status.index}].price" value="${item.price}" /></td>
<td><input name="items[${status.index}].weight" value="${item.weight}" /></td>
</tr>
</c:forEach>
</form:form>
Controller:
private List<Item> items = new ArrayList<>();
#RequestMapping(value = "/create", method = RequestMethod.POST)
public String saveMultipleRows(#ModelAttribute("itemForm") ItemForm itemForm) {
items = itemForm.getItems();
if(items != null && items.size() > 0){
System.out.println("The list is not null!");
}
System.out.println("didn't get into the if statement");
return null;
}
I skipped the Javascript on adding table rows, if you think that have anything to do with this question, I will update my post and put the Javascript code.
The idea is to create a ItemForm class that contains a list of Item object, and in the JSP using JSTL c:foreach to save all the data from users to the list. And in my controller, if the list is not empty, I simply want to print out a message so that I know the list is not empty. But now if I run the program, it prints out "didn't get into the if statement".
So the problem I am currently having is the list is empty, that means I am not able to save the user input data to the list. Can anyone help me and let me know where I did wrong?
Below is the corrected code
since you have item in scope you can directly access the props of Item,You dont need to explicitly declare the index
<input name="${item.price}" value="${item.price}" />
<input name ="${item.weight}" value="${item.weight}" />
I have a jsp in which I am showing a table like
<tr class="even">
<td><bean:write name="itemDetails" property="recived"/></td>
<td><bean:write name="itemDetails" property="actioned"/></td>
<td> <bean:write name="itemDetails" property="internalUser"/></td>
<td> <bean:write name="itemDetails" property="action"/> </td>
</tr> </logic:iterate>
here property actioned is Date in format 08/05/2014 14:34.
Now I want to show property internalUser for row if actioned property is not more than 6 months old else hide it, Can anyone help how to proceed?
Create another property in the itemDetails, for example
private boolean showInternalUser;
public boolean getShowInternalUser() {
// compare difference between `actioned time` and `new Date()` here (6 months)
// and return true if you need to display the property `internalUser`
}
public void setShowInternalUser(boolean showInternalUser) {
this.showInternalUser = showInternalUser;
}
Then in jsp change:
<td> <bean:write name="itemDetails" property="internalUser"/></td>
to
<td>
<c:if test="${itemDetails.showInternalUser}">
<c:out value="${itemDetails.internalUser}"/>
</c:if>
</td>
I have written a javascript function.
function wellChecked(state) {
if (state)
{
wellDropDown.style.visibility = 'visible';
}
else
{
wellDropDown.style.visibility = 'hidden';
}
}
I have a checkbox after the Well Modification <td> as given below,
<tr>
<td>On Call</td>
<td><html:checkbox property="onCall"/></td>
<td>Well Modification</td>
<td><input type="checkbox" onclick="wellChecked(this.checked)" /></td>
</tr>
When that checkbox is clicked I want the drop down list given under the div id=wellDropDown to be displayed. Defaultly, if the check box is not clicked, the drop down should not be displayed.
<tr>
<td>Active</td>
<td><html:checkbox property="active"/></td>
<div id="wellDropDown" style="visibility:hidden;">
<td>
<html:select property="wellFormatId">
<html:option value="">(Select)</html:option>
<bean:define id="wellFormatColl" name="wellFormats" scope="request"/>
<logic:iterate id="wellFormats" name="wellFormatColl" indexId="index" type="com.astrazeneca.compis.data.WellFormatVO">
<% Long wellId = wellFormats.getWellFormatId();%>
<% String wellIdNo = wellId.toString(); %>
<html:option value="<%=wellIdNo%>">
<bean:write name="wellFormats" property="wellFormatName"/>
</html:option>
</logic:iterate>
</html:select>
</td>
</div>
</tr>
When I tried executing this code, I could see the drop down list getting displayed irrespective of the checkbox checked or not.
Where I have went wrong in this scenario? Please give ur suggestions or ways to implement my requirement.
Your HTML is invalid. You may not have a div enclose a td like this. Either make the td itself visible or invisible, or put the div inside the td, instead of putting it around the td.
Also, unless wellDropDown is a global JS variable, the code should be
document.getElementById("wellDropDown").style.visibility = 'visible';
with jquery you could do this :
<tr>
<td>On Call</td>
<td><html:checkbox property="onCall"/></td>
<td>Well Modification</td>
<td><input type="checkbox" id="myCheckBox" /></td>
</tr>
...
<script>
$('#myDropDown').click(
function () {
$("#wellDropDown").toggle();
});
);
</script>
I have de following Form:
<form action="/AppStore/publish" method="post" accept-charset="ISO-8859-1">
<fieldset>
<legend>Do you have an Account already?</legend>
<input type="radio" name="registred" value="yes"> Yes
<input type="radio" name="registred" value="no"> No
</fieldset>
<fieldset>
<legend>About your App</legend>
<table>
<tr>
<td><label for="AppDesc">Describe it:</label></td>
<td><input type="text" name="AppDesc" /></td>
</tr>
<tr>
<td><label for="AppName">Name:</label></td>
<td><input type="text" name="AppName" /></td>
</tr>
</table>
</fieldset>
<input type="submit" value="Submit" />
</form>
I pass this data to a Java Servlet, but every time I get a Nullpointer Exception at getParameter("AppDesc"), instead getParameter("AppName") works fine, what do I wrong?
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context = getServletContext();
RequestDispatcher dispetcher = context.getRequestDispatcher("/publishForm.jsp");
List<String> errorMessages = new ArrayList<String>();
//Validating form input...
if(request.getParameter("AppName").toString().isEmpty())
{
errorMessages.add("Please type a valid Name for your App.");
}
if(request.getParameter("AppDesc").toString().isEmpty())
{
errorMessages.add("The Description of your App should contain at least 160 Characters.");
}
You're calling request.getParameter("...").toString().
request.getParameter() already returns a string reference, so you don't actually need to call toString() to get the value as a string, but it will return a null reference if the parameter is missing - in which case calling toString() will throw an exception. You need to check whether the value is null or empty. For example:
String description = request.getParameter("AppDesc");
if (description == null || description.isEmpty())
...
Of course, there are libraries around to check for "null or empty" - for example, in Guava you could use:
if (Strings.isNullOrEmpty(description))
If request.getParameter("AppDesc") is null, then
request.getParameter("AppDesc").toString().isEmpty() will throw a NullPointerException.
Why not change the condition to:
if(request.getParameter("AppDesc") == null ||
request.getParameter("AppDesc").toString().isEmpty()))
{
<td><input type="text" name="AppDescr" /></td>
You've named the actual field AppDescr (notice the trailing "r"), but you're calling getParameter for AppDesc (sans "r").
EDIT: Or not... you edited your post and fixed it. Was this not the problem?
It must be the case that request.getParameter("AppDesc") returns a null value, causing the chained toString() to generate a NullPointerException.
This parameter was never set; the name specified in the html was "AppDesr" (note the trailing 'r').
Your question title says doGet, your code says doPost. The difference between those two might explain your problem. :-)