I made a web project from jsp and hibernate, on my from have a checkbox... I got no problem when insert the data but when update the data I got this [Ljava.lang.String in every first checkbox on my form and the value of my first checkbox doesn't store to database but the other checkbox work well
one more thing how to check the checkbox value from database??
this part of my jsp
<td><spring:message code="authorityRight" text="default text" /></td>
<td>:</td>
<td><fieldset id="menu_e">
<input type="checkbox" name="menu" value="User" class="ceksmenu">User<br />
<fieldset id="sub_menu_user_e">
<input type="checkbox" name="sub_menu_user" value="User1" class="ceksmuser">User1
<input type="checkbox" name="sub_menu_user" value="User2" class="ceksmuser">User2
<input type="checkbox" name="sub_menu_user" value="User3" class="ceksmuser">User3<br />
</fieldset>
<input type="checkbox" name="menu" value="Monitoring" class="ceksmenu">Monitoring<br />
<fieldset id="sub_menu_monitoring_e">
<input type="checkbox" name="sub_menu_monitoring" value="Monitoring1" class="ceksmmonit">Monitoring1
<input type="checkbox" name="sub_menu_monitoring" value="Monitoring2" class="ceksmmonit">Monitoring2
<input type="checkbox" name="sub_menu_monitoring" value="Monitoring3" class="ceksmmonit">Monitoring3<br />
</fieldset>
<input type="checkbox" name="menu" value="Parameter" class="ceksmenu">Parameter<br />
<fieldset id="sub_menu_parameter_e">
<input type="checkbox" name="sub_menu_parameter" value="Parameter1" class="ceksmparam">Parameter1
<input type="checkbox" name="sub_menu_parameter" value="Parameter2" class="ceksmparam">Parameter2
<input type="checkbox" name="sub_menu_parameter" value="Parameter3" class="ceksmparam">Parameter3</fieldset>
</fieldset>
</td>
this is my javascript
<script type="text/javascript">
$(document).ready(function() {
$('.update').click(function() {
var id_update = $(this).attr("id_update");
var nama_authority = $(this).attr("nama_authority");
var menu = $('input:checkbox[name=menu]').is(':checked');
var sub_menu_user = $('input:checkbox[name=sub_menu_user]').is(':checked');
var sub_menu_monitoring = $('input:checkbox[name=sub_menu_monitoring]').is(':checked');
var sub_menu_parameter = $('input:checkbox[name=sub_menu_parameter]').is(':checked');
$('#id_authority_e').val(id_update);
$('#id_authority_l').val(id_update);
$('#nama_authority_e').val(nama_authority);
$('#menu_e').val(menu);
$('#sub_menu_user_e').val(sub_menu_user);
$('#sub_menu_monitoring_e').val(sub_menu_monitoring);
$('#sub_menu_parameter_e').val(sub_menu_parameter);
});
$('.delete').click(function() {
var id_delete = $(this).attr("id_delete");
$('#id_authority_d').val(id_delete);
});
$('#example').dataTable({
});
});
</script>
and this is my controller
public class Operator {
#RequestMapping(value = "/admin/updateAuthority.html", method = RequestMethod.POST)
public ModelAndView updateAuthorityUser(ModelMap model, HttpServletRequest request,
HttpServletResponse response) throws Exception {
String id_Authority = request.getParameter("id_authority");
AuthorityUser authorityuser = authorityuserService.get(id_Authority);
String nama = request.getParameter("nama_authority");
String[] menu = request.getParameterValues("menu");
String[] sb_user = request.getParameterValues("sub_menu_user");
String[] sb_monitor = request.getParameterValues("sub_menu_monitoring");
String[] sb_parameter = request.getParameterValues("sub_menu_parameter");
authorityuser.setId_authority(id_Authority);
authorityuser.setNama_authority(nama);
authorityuser.setMenu(menu);
authorityuser.setSub_menu_user(sb_user);
authorityuser.setSub_menu_monitoring(sb_monitor);
authorityuser.setSub_menu_parameter(sb_parameter);
authorityuserService.update(authorityuser);
model.addAttribute("successUpdate", "true");
return listAuthorityUser(model);
}
}
any help will be pleasure :)
Related
Code to post PayPal form.
<body>
<form accept-charset="utf-8"
action="https://www.sandbox.paypal.com/au/cgi-bin/webscr" id="frm"
method="post">
<input type="hidden" name="business"
value="aaaa-facilitator#gmail.com"> <input
type="hidden" name="cancel_return"
value="http://localhost:8080/abc-jaxrs/web/cancel_redirection"> <input type="hidden"
name="cmd" value="_xclick"> <input type="hidden"
name="charset" value="utf-8"> <input type="hidden"
name="currency_code" value="USD"> <input type="hidden"
name="notify_url" value="http://localhost:8080/abc-jaxrs/web/success_redirection"> <input
type="hidden" name="return" value="http://localhost:8080/abc-jaxrs/web/success_redirection"> <input
type="hidden" name="item_name" value="123"> <input
type="hidden" name="amount" value="0.1">
</form>
<p style="text-align: center">
<h4>Redirecting To PayPal...</h4>
<script>
$(this.document).ready(function() {
$('.loaderparent').show();
var frm = $("form");
frm.submit();
});
</script>
Code to retrieve data from data from Java controller.
#RequestMapping(value = "/success_redirection", method = RequestMethod.GET)
public ModelAndView successRedirection(HttpServletRequest request, HttpServletResponse response) {
String itemName = (String) request.getAttribute("item_name"); // or
String itemNname = request.getParameter("item_name");
}
But getting itemName value as null in both cases.
So how to retrieve success/failure data from PayPal in JAVA.
Ensure enabling the Instant Payment Notification (IPN) from paypal account setting along with the success URL.
My Profile > My Selling Tools> Instant payment notifications >update then like below.
Im trying to create 2 instances of an object and then parse them to the controller via an input type="submit".
Here's what I have:
#RequestMapping (value = "/webdata")
public String vorsorgestammdaten(Model model) {
model.addAttribute("Data", new DataOp());
model.addAttribute("Data2", new DataOp());
return "/webdata";
}
// Here I want to parse both objects
#RequestMapping(value = "/formdata", method = RequestMethod.POST)
public String formdata(HttpServletRequest req, #AuthenticationPrincipal User currentUser,
DataOp Data, DataOp Data2, Model model) {
// Do stuff for example
Data.getName();
Data2.getName();
}
Here is part of my forms:
<form method="post" th:action="#{/formdata}" th:object="${regData}">
<div class="col-sm-4">
<input th:field="${Data2.gender}" type="radio" value="MALE" name="gender" disabled="disabled" /><span>Mr</span>
<input th:field="${Data2.gender}" type="radio" value="FEMALE" name="gender" disabled="disabled"/><span>Frau</span>
<p>Firstname:</p>
<input required="required" type="text" th:field="${Data2.firstname}" placeholder="Max" disabled="disabled" />
</div>
<div class="col-sm-4">
<input th:field="*{Data.gender}" type="radio" value="MALE" name="gender" disabled="disabled" /><span>Mr</span>
<input th:field="*{Data.gender}" type="radio" value="FEMALE" name="gender" disabled="disabled"/><span>Frau</span>
<p>Firstname:</p>
<input required="required" type="text" th:field="*{Data.firstname}" placeholder="Max" disabled="disabled" />
<button type="submit">Speichern</button>
</div>
</form>
Try removing this th:object="${regData}".
Then you can use #ModelAttribute annotation.
For example,
public String formdata(HttpServletRequest req,#AuthenticationPrincipal User currentUser, #ModelAttribute DataOp Data,#ModelAttribute DataOp Data2, Model model) {
//Some code
}
You might as well switch *{Data.gender} to ${Data.gender}.
add RequestAttribute annotation to each of those params, adding the "name" attribute to differentiate them.
here you have some info.
So my Spring MVC project home page has two input fields within a form.
The user can enter the roll number or name.
Jsp
<form action="search" method="GET" >
<div style="text-align: center;">
<input type="text" id="regNo" name="regNo" size="30" maxLength="50" "></input> or
<input id="studentName" type="text" size="30" maxLength="50" "></input>
</form>
In my controller, I have two methods mapped to two input fields.
#RequestMapping(value ="/search", method = RequestMethod.GET)
public String getStudent(String regNo, ModelMap model){
return "A";
}
#RequestMapping(value="/search", method = RequestMethod.GET)
public String searchStudentByName(String studentName, ModelMap model){
return "B";
}
Since both the input fields are String, I don't know how can I map them to two different methods in controller?
Did you want something like this :
#RequestMapping(method= RequestMethod.GET,value = "/search/r/regNo={regNo}")
public String getStudent(#PathVariable String regNo){
}
for studentName :
#RequestMapping(method= RequestMethod.GET,value = "/search/s/studentName={studentName}")
public String getStudent(#PathVariable String studentName){
}
then you need to add both request in different form tag, & in action tag provide :
if regNo send :
/search/r/
for studentName :
/search/s/
Jsp :
<form action="search/r" method="GET" >
<div style="text-align: center;">
<input type="text" id="regNo" name="regNo" size="30" maxLength="50" "></input>
<input type="submit" name="approve" value="RegNo" />
</form>
<form action="search/s" method="GET" >
<div style="text-align: center;">
<input id="studentName" type="text" size="30" maxLength="50" "></input>
<input type="submit" name="approve" value="StudentName" />
</form>
OR Second way to do so :
<form action="search" method="GET" >
<input type="text" id="regNo" name="regNo" size="30" maxLength="50" "></input>
<input id="studentName" type="text" size="30" maxLength="50" "></input>
<input type="submit" name="regno" value="regno" />
<input type="submit" name="studentName" value="studentName" />
</form>
controller :
#RequestMapping(value = "/search", method = RequestMethod.GET, params = { "regno" })
public String getRegno(#RequestParam String regno) {
}
#RequestMapping(value = "/search", method = RequestMethod.GET, params = { "studentName" })
public String getStudent(#RequestParam String studentName) {
}
Post me.
I have a jsp file contains a form and I need to validate the form. The jsp page will be opened as pop-up page and hence I choose not to pop-up the validation message again. Here is my code.
<script type="text/javascript">
function validateForm() {
var x=document.forms["myForm"]["name1"].value;
if (x.trim().length<=4){
return false;
}
}
</script>
<form name="myForm" method="POST" action="write.jsp" onsubmit="return validateForm();">
Name: <input type="text" name="name1" value="<%=name%>" size="20"/> <br>
Price: <input type="text" name="price" value="<%=price%>" size="20" /><br>
Due time: <input type="text" name="DueTime" value="<%=dueday%>" size="20" /><br>
Location: <input type="text" name="Location" value="<%=address%>" size="20" /><br>
Photo Url: <input type="text" name="url" value="<%=imagePath%>" size="20" /><br>
Description: <input type="text" name="desc" value="<%=desc%>" size="20" /><br>
<input type="submit" name="submit" onclick="window.close()"/>
<% if(validateForm = false) { %> <a>error message</a> <% } %>
</form>
I am validating for first input "name1" only and the input length is less than 4.
I'd like to display a error message below the submit button. The codes is not working, can anyone help me to fix this?
document.getElementsByName('name1')[0].value;
in place of
document.forms["myForm"]["name1"].value;
use a span below the submit button
html
<span id="error"></span>
script
function validateForm()
{
var x=document.getElementsByName('name1')[0].value;
if (x.trim().length<=4)
{
document.getElementById("error").innerHTML="atleast 4 letters required in name";
return false;
}
}
Use document.getElementsByName("name1") instead of document.forms["myForm"]["name1"].value;
Use as
function validateForm()
{
var x=document.getElementsByName("name1").value;
if (x.trim().length<=4)
{
alert("Message");
return false;
}
}
I am using Spring MVC controller.
Below is my JSP code in which it will show three radio button. One is Insert, second radio button is Update and third radio button is Delete.
Once I click Insert radio button, it show two text box next to Insert radio button, and same thing with other two radio button.
<script type="text/javascript">
function doDisplay(radio) {
switch (radio.value) {
case "Insert":
document.getElementById("inserts").style.display = "inline";
document.getElementById("updates").style.display = "none";
document.getElementById("delete").style.display = "none";
break;
case "Update":
document.getElementById("inserts").style.display = "none";
document.getElementById("updates").style.display = "inline";
document.getElementById("delete").style.display = "none";
break;
case "Delete":
document.getElementById("inserts").style.display = "none";
document.getElementById("updates").style.display = "none";
document.getElementById("delete").style.display = "inline";
break;
}
}
</script>
<body>
<form method="post" action="testOperation">
<input type="radio" name="tt" value="Insert"
onclick="doDisplay(this);" /> Insert <input type="hidden" name="name" value="insert"><span id="inserts"
style="display: none"> <label for="Node"> Node </label> <input
type="text" name="node" size="20" /> <label for="Data"> Data </label>
<input type="text" name="data" size="100" />
</span> <br /> <input type="radio" name="tt" value="Update"
onclick="doDisplay(this);" /> Update <input type="hidden" name="name" value="update"> <span id="updates"
style="display: none"> <label for="Node"> Node </label> <input
type="text" name="node" size="20" /> <label for="Data"> Data </label>
<input type="text" name="data" size="100"/>
</span> <br /> <input type="radio" name="tt" value="Delete"
onclick="doDisplay(this);" /> Delete <input type="hidden" name="name" value="delete"><span id="delete"
style="display: none"> <label for="Node"> Node </label> <input
type="text" name="node" size="20" /> <label for="Data"> Data </label>
<input type="text" name="data" size="100"/>
</span> <br />
<input type="submit">
</form>
Now what I am trying to do is as soon as I click on Insert radio button, it will show me two textbox next to Insert radio button. And after putting some values inside those two text box, I press submit button on the form. And then it goes to testOperations method in which name field has these many things -
insert,update,delete
How can I force it to have only one field which I am clicking only? Meaing if I am clickign insert radio button, then name variable should have insert value? Below is my method -
#RequestMapping(value = "testOperation", method = RequestMethod.GET)
public Map<String, String> testOperation() {
final Map<String, String> model = new LinkedHashMap<String, String>();
return model;
}
#RequestMapping(value = "testOperation", method = RequestMethod.POST)
public Map<String, String> testOperations(#RequestParam String name,
#RequestParam String node,
#RequestParam String data) {
final Map<String, String> model = new LinkedHashMap<String, String>();
System.out.println(name);
System.out.println(node);
System.out.println(data);
return model;
}
Meaning, Suppose I click Insert radio button and type Hello in the first text and World in the second text box, then I will hit the submit button, and after that I should see insert value in the name variable and hello value in the node variable and world value in the data variable.
And same thing with update and delete.
Is this possible to do?
You have done correct. All you need to do is while click the radio button set the value on name. One hidden variable is enough. Your html part look like this,
<form method="post" action="testOperation">
<!-- I used only one hidden box to store value -->
<input type="hidden" name="name" id="dynamicName">
<input type="radio" name="tt" value="Insert"
onclick="doDisplay(this);" /> Insert <span id="inserts"
style="display: none"> <label for="Node"> Node </label> <input
type="text" name="node" size="20" /> <label for="Data"> Data </label>
<input type="text" name="data" size="100" />
</span> <br /> <input type="radio" name="tt" value="Update"
onclick="doDisplay(this);" /> Update <span id="updates"
style="display: none"> <label for="Node"> Node </label> <input
type="text" name="node" size="20" /> <label for="Data"> Data </label>
<input type="text" name="data" size="100"/>
</span> <br /> <input type="radio" name="tt" value="Delete"
onclick="doDisplay(this);" /> Delete <span id="delete"
style="display: none"> <label for="Node"> Node </label> <input
type="text" name="node" size="20" /> <label for="Data"> Data </label>
<input type="text" name="data" size="100"/>
</span> <br />
<input type="submit">
</form>
And in script,
<script type="text/javascript">
function doDisplay(radio) {
switch (radio.value) {
case "Insert":
document.getElementById("inserts").style.display = "inline";
document.getElementById("updates").style.display = "none";
document.getElementById("delete").style.display = "none";
document.getElementById("dynamicName").value = "insert";
break;
case "Update":
document.getElementById("inserts").style.display = "none";
document.getElementById("updates").style.display = "inline";
document.getElementById("delete").style.display = "none";
document.getElementById("dynamicName").value = "update";
break;
case "Delete":
document.getElementById("inserts").style.display = "none";
document.getElementById("updates").style.display = "none";
document.getElementById("delete").style.display = "inline";
document.getElementById("dynamicName").value = "delete";
break;
}
}
</script>
Hope this helps.
You have inputs with same names, you don't need other hidden. A single node and data inputs are enough
This HTML is enough:
<input type="radio" name="tt" value="Insert" onclick="doDisplay(this);" /> Insert<br/>
<input type="radio" name="tt" value="Update" onclick="doDisplay(this);" /> Update<br/>
<input type="radio" name="tt" value="Delete" onclick="doDisplay(this);" /> Delete<br/>
<label for="Node"> Node </label> <input type="text" name="node" size="20" /><br/>
<label for="Data"> Data </label> <input type="text" name="data" size="100"/><br/>
<input type="submit">
Here is the controller function:
public Map<String, String> testOperations(
#RequestParam(value="tt", required=false) String name,
#RequestParam(value="node", required=false) String node,
#RequestParam(value="data", required=false) String data) {
//...
}