I'm making a Spring MVC project, and I have this form in my .jsp
JSP
<form:form action="/admin/assign/add" modelAttribute="cafeTable">
<table>
<tr>
<td>
<form:label path="tableNumber">
<spring:message text="Table's Number"/>
</form:label>
</td>
<td>
<form:select path="tableNumber" action = "select">
<form:options items="${tableNumbers}"></form:options>
</form:select>
</td>
</tr>
<tr>
<td>
<form:label path="${user.fullName}">
<spring:message text="Waiter's name"/>
</form:label>
</td>
<td>
<form:select path="${user.fullName}" action = "select">
<c:forEach items="${listUser}" var="user">
<option value="${user.fullName}">${user.fullName}</option>
</c:forEach>
</form:select>
</td>
</tr>
</table>
<br>
<input type="submit" name="assign"
value="<spring:message text="Assign"/>"/>
</form:form>
So I have User and CafeTable models, but my model attribute is CafeTable.
With the submit button I need to get both selected values in my controller:
#RequestMapping(value = "/admin/assign/add", params = "assign", method = RequestMethod.POST)
public String assign(#ModelAttribute("cafeTable") CafeTable cafeTable) {
//set cafeTable's UserID field matching selected fullName value
tableService.addTable(cafeTable);
return "admin";
}
How can I do this?
<form:form action="/admin/assign/add" modelAttribute="cafeTable">
<table>
<tr>
<td>
<form:label path="tableNumber">
<spring:message text="Table's Number"/>
</form:label>
</td>
<td>
<input type="hidden" value="${user.fullName}" name="fullname">
<form:select path="tableNumber" action = "select">
<form:options items="${tableNumbers}"></form:options>
</form:select>
</td>
</tr>
<tr>
<td>
<form:label path="${user.fullName}">
<spring:message text="Waiter's name"/>
</form:label>
</td>
<td>
<form:select path="${user.fullName}" action = "select">
<c:forEach items="${listUser}" var="user">
<option value="${user.fullName}">${user.fullName}</option>
</c:forEach>
</form:select>
</td>
</tr>
</table>
<br>
<input type="submit" name="assign"
value="<spring:message text="Assign"/>"/>
</form:form>
If you don't have fullname property in CafeTable try to add a hidden field inside form:form like
<input type="hidden" value="${user.fullName}" name="fullname">
and add a optional request parameter into your controller like below
#RequestMapping(value = "/admin/assign/add", method = RequestMethod.POST)
public String save(#RequestParam("fullname") String fullname,#Valid #ModelAttribute("cafeTable") CafeTable cafeTable) {
//now you can access full name from fullname variable here
....
}
I haven't tested this code.
I am using this code to get form data used inside jsp Form.
String product=request.getParameter("product");
String qty=request.getParameter("quantity");
String price=request.getParameter("price");
writer.println(product +" "+qty+" "+price);
How Ever i am using dynamic form.How do i can get all datas in controller class.
Here is the code of view class i.e jsp Pages.
<script>
$(document).ready(function(){
$("button").on("click", function(){
var row = '<tr><td><input type="text" name="product" value="product..">'+
'</input></td><td><input type="number" name="quantity" value="quanty..">' +
' </input></td><td><input type="number" name="price" value="price.."> </input></td></tr>';
$("#customers").append(row);
});
});
</script>
</head>
<body>
<button type="button">Add More Products</button>
<form action="XmlServlet" method="get">
<table id="customers">
<tr>
<th>Product</th>
<th>Quantity</th>
<th>Price</th>
</tr>
<tr>
<td><input type="text" name="product" value="product.."></input> </td>
<td><input type="number" name="quantity" value="quanty.."></input></td>
<td><input type="number" name="price" value="price.."></input></td>
</tr>
<tr>
<td><input type="submit"></td>
</tr>
</table>
</form>
How do i can get all data's saved in text field after add more button action's.
String[] products = request.getParameterValues("product");
This question already has answers here:
NullPointerException, when reading HTML input [duplicate]
(3 answers)
Closed 6 years ago.
home.jsp
<form method="POST" action="Initiater.do">
<table>
<tr>
<td>
Internal Diameter from FlowAss:
</td>
<td>
<input type="text" id="Id" />
</td>
<td>
Depth:
</td>
<td>
<input type="text" id="Depth" />
</td>
<td>
Units:
</td>
<td>
<select>
<option value="ft">feet</option>
<option value="mts">meters</option>
</select>
</td>
</tr>
<tr>
<td>
<input type="submit" />
</td>
</tr>
</table>
</form>
</body>
</html>
Home.java
public class Home extends HttpServlet {
public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException
{
System.out.println("in Post");
String id = (String) request.getParameter("Id");
String depth = request.getParameter("Depth");
/*double id =Double.parseDouble(request.getParameter("Id"));
double depth =Double.parseDouble(request.getParameter("Depth"));*/
System.out.println("Id"+id);
System.out.println("Depth"+depth);
}
the servlet Home.java's doPost method is called, the values id and depth are returning nulls when I debug can anyone help me out with this.
Add name attribute to the input tag.
Change
<input type="text" id="Depth" />
<input type="text" id="Id" />
To
<input type="text" id="Depth" name="Depth"/>
<input type="text" id="Id" name="Id"/>
Request.getParameter gets data by name attribute and not id
I am trying to get the database values saved from form into Textbox in same form. The values are loaded whenever the form is loaded. I have written a example code what I am doing below. I have 5 rows of textboxes. I have written the below code that get the values from database. But my knowledge is limited about getting those five rows of values to the five rows of textboxes either using JSP or JAVAScript. Tried searching online but didn't fine related answer. Any help will be appreciated. Thank you.
<body>
<%
String processVal = "SELECT * FROM NETWORK_ACCESS WHERE PRD_ID=?";
PreparedStatement pst = conn.prepareStatement(processVal);
pst.setString(1, ProcID);
ResultSet rsProcess=pst.executeQuery();
while(rsProcess.next())
{
SIP=rs.getString(2);
DIP=rs.getString(3);
SP=rs.getString(4);
SD=rs.getString(5);
ED=rs.getString(6);
}
%>
<table>
<tr>
<th>Sl.No</th>
<th>SIP</th>
<th>DIP</th>
<th>SP</th>
<th>Start Date</th>
<th>END Date</th>
</tr>
<tr>
<td style="align:center"> 1 </td>
<td><input type="text" name="name" id="id" size="20"> </td>
<td><input type="text" name="name1" id="id1" size="20"> </td>
<td><input type="text" name="name2" id="id2" size="20"> </td>
<td><input type="text" name="name3" id="id3" size="15"> <img src="../Image/cal.gif" id="" style="cursor: pointer;" onclick="javascript:NewCssCal('name3','MMddyyyy','dropdown',false,'12')" /></td>
<td><input type="text" name="name4" id="id4" size="15"><img src="../Image/cal.gif" id="" style="cursor: pointer;" onclick="javascript:NewCssCal('name4','MMddyyyy','dropdown',false,'12')" /> </td>
</tr>
<tr>
<td style="align:center"> <label> 2 </label> </td>
<td><input type="text" name="name" id="id5" size="20"> </td>
<td><input type="text" name="name1" id="id6" size="20"> </td>
<td><input type="text" name="name2" id="id7" size="20"> </td>
<td><input type="text" name="name3" id="id8" size="15"> <img src="../Image/cal.gif" id="" style="cursor: pointer;" onclick="javascript:NewCssCal('name3','MMddyyyy','dropdown',false,'12')" /></td>
<td><input type="text" name="name4" id="id9" size="15"><img src="../Image/cal.gif" id="" style="cursor: pointer;" onclick="javascript:NewCssCal('name4','MMddyyyy','dropdown',false,'12')" /> </td>
</tr>
<tr>
<td> <label> 3 </label> </td>
<td><input type="text" name="name" id="id10" size="20"> </td>
<td><input type="text" name="name1" id="id11" size="20"> </td>
<td><input type="text" name="name2" id="id12" size="20"> </td>
<td><input type="text" name="name3" id="id13" size="15"> <img src="../Image/cal.gif" id="" style="cursor: pointer;" onclick="javascript:NewCssCal('name3','MMddyyyy','dropdown',false,'12')" /></td>
<td><input type="text" name="name4" id="id14" size="15"><img src="../Image/cal.gif" id="" style="cursor: pointer;" onclick="javascript:NewCssCal('name4','MMddyyyy','dropdown',false,'12')" /> </td>
</tr>
<tr>
<td> <label>4 </label> </td>
<td><input type="text" name="name" id="id15" size="20"> </td>
<td><input type="text" name="name1" id="id15" size="20"> </td>
<td><input type="text" name="name2" id="id16" size="20"> </td>
<td><input type="text" name="name3" id="id17" size="15"> <img src="../Image/cal.gif" id="" style="cursor: pointer;" onclick="javascript:NewCssCal('name3','MMddyyyy','dropdown',false,'12')" /></td>
<td><input type="text" name="name4" id="id18" size="15"><img src="../Image/cal.gif" id="" style="cursor: pointer;" onclick="javascript:NewCssCal('name4','MMddyyyy','dropdown',false,'12')" /> </td>
</tr>
<tr>
<td> <label>5 </label> </td>
<td><input type="text" name="name" id="id19" size="20"> </td>
<td><input type="text" name="name1" id="id20" size="20"> </td>
<td><input type="text" name="name2" id="id21" size="20"> </td>
<td><input type="text" name="name3" id="id22" size="15"> <img src="../Image/cal.gif" id="" style="cursor: pointer;" onclick="javascript:NewCssCal('name3','MMddyyyy','dropdown',false,'12')" /></td>
<td><input type="text" name="name4" id="id23" size="15"><img src="../Image/cal.gif" id="" style="cursor: pointer;" onclick="javascript:NewCssCal('name4','MMddyyyy','dropdown',false,'12')" /> </td>
</tr>
</table>
//write table header code here
i = 0;
while(rsProcess.next())
{
SIP=rs.getString(2);
DIP=rs.getString(3);
SP=rs.getString(4);
SD=rs.getString(5);
ED=rs.getString(6);
i++;
%>
//write code for each row of db
<tr>
<td style="align:center"> <c:out value=${i}> </td>
<td><input type="text" name="name" id="id" size="20" value="<c:out value=${SIP}>">
..........
</tr>
<%
}
%>
You should assign classes to tags. Later, you should do like these with jQuery :
var abc = $("thclass1").text();
$("input[name=input1]).val(abc);
var abcd = $("thclass2").text();
$("input[name=input2).val(abcd);
...
you should make these each inputs and th tags.
I have the following class that I'm using in my Java with JSP applicaton.
//
public class QuestionBO implements Serializable{
private int questionId;
private int testID;
private String question;
private TutorBO infoAboutTutor;
private SubjectBO infoAboutSubject;
private TestBO infoAboutTest;
private List<AnswerBO> answers;
public QuestionBO() {
}
public QuestionBO(String question) {
this.question = question;
}
getter & setter....
The JSP page has a form where each Question (its String representation) has a checkbox next to it. A user marks some of the questions and submits the form to the server for processing by a servlet.
What is the conventional way of binding the Question objects with the checkboxes so that I could find out what Questions have been selected?
Currently I'm using the following approach for constructing the form:
//
<c:if test="${not empty questionsForSubject}">
<form action="/TutorWebApp/controller" method="POST" name="addQuestionForm">
<input type="hidden" name="command" value="add_question_list" />
<input type="hidden" name="testName" value="${testName}"/>
<table border ="1">
<tbody>
<c:forEach items="${questionsForSubject}" var="question">
<tr>
<td>
<input type="checkbox" name ="choosen_question"
value="${question.getQuestion()}">
${question.getQuestion()}
<br />
</td>
</tr>
</c:forEach>
</tbody>
</table>
<input type="submit" value="Add questions "/>
</form>
And I shouldn't use frameworks.
Thanks
And I have last question
<c:if test="${not empty questionsForSubject}">
<form action="/TutorWebApp/controller" method="POST" name="addQuestionForm">
<input type="hidden" name="command" value="add_question_list" />
<input type="hidden" name="testName" value="${testName}"/>
<input type="hidden" name="questionsForSubject" value="${questionsForSubject}"/>
<table border ="1">
<tbody>
<c:forEach items="${questionsForSubject.keySet()}" var="questionID">
<tr>
<td>
<input type="checkbox" name ="choosen_question" value="${questionID}">
${questionsForSubject.get(questionID).getQuestion()}
<br />
</td>
</tr>
</c:forEach>
</tbody>
</table>
<input type="submit" value="Добавить вопросы"/>
</form>
How I can get map from this page on servlet?
Give each checkbox an unique value. For example, the unique question identifier:
<c:forEach items="${questionsForSubject}" var="question">
<tr>
<td>
<input type="checkbox" name="chosen_question" value="${question.questionId}" />
${question.question}
<br />
</td>
</tr>
</c:forEach>
This way you'll be able to grab all checked values by just the following call in the servlet:
String[] chosenQuestions = request.getParameterValues("chosen_question");
Generate an unique name for each checkbox as follows:
<input type="checkbox" name="${question.questionId}" />
or:
<input type="checkbox" name="choosen_question_${question.questionId}" />
After that, you're already able to identify each checkbox in your servlet