I've have two forms in two jsp files. Where in I, submit the first form and save it in a the database using servlet. And then, I've go to other form to fill up the details. As in the second form I have few fields the same as first form. As I, enter the dataid in the second form automatically the first name and last name to be field matching the dataid.
how can I do this in a servlet?
<form>
Data id:<input type="text" name="dataid"><br>
First name: <input type="text" name="firstname"><br>
Last name: <input type="text" name="lastname">
<input type="submit value="submit"/>
</form>
<form>
Some id:<inut type="text" name="someid"><br>
Age:<input type="text" name="age"> <br>
Data id:<inut type="text" name="dataid"><br>
First name: <input type="text" name="firstname"><br>
Last name: <input type="text" name="lastname">
<input type="submit" type="transmit">
</form>
If you still have the parameters in the request you can simply write them in the valueof the ìnput` tag. Something like:
<form>
Data id:<input type="text" name="dataid" value="<%=request.getParameter("dataid")%>"><br>
First name: <input type="text" name="firstname" value="<%=request.getParameter("firstname")%>"><br>
Last name: <input type="text" name="lastname" value="<%=request.getParameter("lastname")%>">
<input type="submit value="submit"/>
</form>
Take a look at The Java EE 5 Tutorial
Related
I have a form for sending "question" to server.
A "question" has fields: title, description and options (from 1 to 4 represented by Map<String, Boolean>). String - for option, Boolean - true/false.
So, how can I create a Map of every option and send it to server?
<div id="TYPE_TEST" class="opclass">
<p><label for="option1"/>Option 1:
<input id="option1" name="key" type="text" placeholder=""/>
<input type="radio" name="value"/>
</p>
<p><label for="option2"/>Option 2:
<input id="option2" name="key" type="text" placeholder=""/>
<input type="radio" name="value"/>
</p>
<p><label for="option3"/>Option 3:
<input id="option3" name="key" type="text" placeholder=""/>
<input type="radio" name="value"/>
</p>
<p><label for="option4"/>Option 4:
<input id="option4" name="key" type="text" placeholder=""/>
<input type="radio" name="value"/>
</p>
</div>
Thank you,
Pavel
You need to represent like this
represent questions and options like this
//Map<question,options>
//where option also map
Map<String,Map<String,Boolean>> questions=new HashMap<String,Map<String,Boolean>>();
Map<String,Boolean> question1_options=new HashMap<String,Boolean>();
question1_options.put("option1",true);
question1_options.put("option2",false);
question1_options.put("option3",false);
question1_options.put("option4",false);
Map<String,Boolean> question2_options=new HashMap<String,Boolean>();
question2_options.put("option1",true);
question2_options.put("option2",false);
question2_options.put("option3",false);
question2_options.put("option4",false);
questions.put("question_1",question1_options);
questions.put("question_2",question2_options);
System.out.println(questions);
Following MaDHaN's code: inside JSP, use it with ${mapName["option1"]}.
If it works, I think MaDHaN deserves the accept.
You can do it in several ways. Some of them as follows:
<% Map<String,Boolean> map =new HashMap<String,Boolean>();
map.put("key1",true);
map.put("key2",false);
request.setAttribute("map", map);
%>
<%for(Map.Entry<String,Boolean> mapEntry: map.entrySet()){%>
<%= mapEntry.getKey() %>
<%= mapEntry.getValue() %>
<%}%>
Or
<c:forEach items="${map}" var="entry">
<c:out value ="${entry.key}" />
<c:out value ="${entry.value}"/>
</c:forEach>
I have a problem with binding my object in ftl form.
Here is my controller method:
#RequestMapping(method = RequestMethod.POST)
public String saveConfigProperties(#ModelAttribute("configCmdList") ConfigCmdList configCmdList, BindingResult bindingResult) {
configurationDao.setConfigValues(configCmdList.getConfigurations());
return "config";
}
and here is part of my ftl:
<form action="" method="POST">
<#spring.bind "configCmdList" />
<#list configCmdList.configurations as config>
${config.name}
</#list>
<input type="submit" value="submit"/>
</form>
I have an access to my list of objects which I sent previous using GET method in my ftl, but my object list is null after sending object without modifications back to controller.
I tried to bind my configCmdList.configurations and also bind separately each element of that list in loop but without success.
What I'm missing?
VairalPatel web page is down and I remember that he wrote good example about freemarker form and spring mvc.
Thanks in advance for your help.
Ok, I resolved an issue. I had to bind each list element and it parameters separately in loop using ${spring.status.expression} and ${spring.status.value}.
Here is my code:
<form action="" method="POST">
<#list configCmdList.configurations as config>
<#spring.bind path="configCmdList.configurations[${config_index}].id"/>
<input type="hidden" name="${spring.status.expression}" value="${spring.status.value}" />
<#spring.bind path="configCmdList.configurations[${config_index}].name"/>
<input type="text" name="${spring.status.expression}" value="${spring.status.value}" />
<#spring.bind path="configCmdList.configurations[${config_index}].value"/>
<input type="text" name="${spring.status.expression}" value="${spring.status.value}" />
</#list>
<input type="submit" value="submit"/>
</form>
Thank you for your help :)
This is another way to write Caro's solution:
<form action="" method="POST">
<#list configCmdList.configurations as config>
<input type="hidden" name="configurations[${config_index}].id" value="${config.id}"/>
<input type="text" name="configurations[${config_index}].name" value="${config.name}"/>
<input type="text" name="configurations[${config_index}].value" value="${config.value}" />
</#list>
<input type="submit" value="submit"/>
</form>
<form action="Operation">
First number::<input type="text" name="firstno"></input></br>
Second number::<input type="text" name="Secondno"></input></br>
<select name="operation">
<option>Add</option>
<option>Substract</option>
<option>Multiply</option>
<option>Divide</option>
</select> </br>
<input type="submit" value="submit">
</form>
Here instead of a drop down menu I want to use a radio button. How can I implement this? Also I want to perform an operation in servlet page.
for radio buttons try this way
<form action="Operation">
First number::<input type="text" name="firstno"></input></br>
Second number::<input type="text" name="Secondno"></input></br>
<input type="radio" name="operation" value="add">add<br>
<input type="radio" name="operation" value="subtract">Substract<br>
<input type="radio" name="operation" value="Multiply">Multiply<br>
<input type="radio" name="operation" value="Divide">Divide<br>
<input type="submit" value="submit">
</form>
on the servlet try
String radio=request.getParameter("operation");
change your input type="radio"
The following is the HTML code:
<div id="TITLE" class="text">
<label for="widget_polarisCommunityInput_113_title">Title</label>
<input type="text" name="field(TITLE)" id="widget_polarisCommunityInput_113_title">
<div class="error">This field must not be empty</div>
</div>
I tried to get the text "This field must not be empty" as follows by using WebDriver with Java:
driver.findElement(By.xpath("//div[#id='TITLE']/div")).getText();
driver.findElement(By.xpath("//div[#class='error']")).getText();
But, no text was retrieved. How can I do it? What's the wrong with my code?
Yes, there is a form. HTMl code including form as belows:
<form action="http://community.sandbox.no/content/save.do;jsessionid=F2BF733599D8D9F812B89ACBC20D37C5" method="post">
<div id="widget_polarisCommunityInput_113_leftcolumn">
<input type="hidden" value="article" name="articleType">
<input type="hidden" value="published" name="state">
<input type="hidden" value=" " name="successUrl">
<input type="hidden" value="" name="articleId">
<input type="hidden" value="http://sandbox.no/community/article/" name="redirect_url">
<input type="hidden" value="113" name="widget_id">
<div id="TITLE" class="text">
<label for="widget_polarisCommunityInput_113_title">Title</label>
<input type="text" name="field(TITLE)" id="widget_polarisCommunityInput_113_title">
<div class="error">This field must not be empty</div>
</div>
</form>
You can try as:
driver.findElement(By.xpath("//form/div[#id='widget_polarisCommunityInput_113_leftcolumn']/div[#id='TITLE']/div")).getText();
I'm building a java servlet to respond to some HTML form. Here is the simple test form:
<FORM action="http://somesite.com/prog/adduser" method="post">
<P>
<LABEL for="firstname">First name: </LABEL>
<INPUT type="text" id="firstname"><BR>
<LABEL for="lastname">Last name: </LABEL>
<INPUT type="text" id="lastname"><BR>
<LABEL for="email">email: </LABEL>
<INPUT type="text" id="email"><BR>
<INPUT type="radio" name="sex" value="Male"> Male<BR>
<INPUT type="radio" name="sex" value="Female"> Female<BR>
<INPUT type="submit" value="Send"> <INPUT type="reset">
</P>
</FORM>
On the server side I get the HttpRequest alright. But when I get the parameters like this:
#Override
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String firstName = req.getParameter("firstname");
String lastName = req.getParameter("lastname");
String sex = req.getParameter("sex");
String email = req.getParameter("email");
}
Only the "sex" is ok. I've been at this for hours without understanding why "sex" is different from the rest. All other parameters are null. Ok it's the only "radio" type but is there a special way to get the parameters for the others?
Thank you!
In an HTML Form it is important to give your Input values the Name attribute, the ID attribute only helps Javascript in the page find your Input elements better.
I noticed that you missed Name attributes for your fist few Input elements.
You need to add the name attribute with the rest of the input tags, like you did with the sex input tag:
<FORM action="http://somesite.com/prog/adduser" method="post">
<P>
<LABEL for="firstname">First name: </LABEL>
<INPUT type="text" id="firstname" name="firstname"><BR>
<LABEL for="lastname">Last name: </LABEL>
<INPUT type="text" id="lastname" name="lastname"><BR>
<LABEL for="email">email: </LABEL>
<INPUT type="text" id="email" name="email"><BR>
<INPUT type="radio" name="sex" value="Male"> Male<BR>
<INPUT type="radio" name="sex" value="Female"> Female<BR>
<INPUT type="submit" value="Send"> <INPUT type="reset">
</P>