这是我的jsp页面
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script>
function getChoice(){
var k=confirm("Do you want me to overwrite the existing file ?");
return k;
}
</script>
</head>
<body style="background-color:powderblue;color:Tomato">
<center><form method="POST" action="saveagain">
<h3>${message} </h3>
<br> <br>
<input type="hidden" name="fname" value="${filename}">
<input type="hidden" name="fcontent" value="${filecontent}"> <br><br>
<input type="button" value="Overwrite ?" onclick="choice=getChoice()"/>
<input type="submit" value="Submit ?"></form></center>
<br>
<br>
<center><b>${m}</b></center>
<br><br>
<center><a href="toupload">Click me to upload a file</a> <br><br>
</center>
</body>
</html>
在上面的代码中,我想将确认弹出的结果输入变量&#34; choice&#34;。怎么做 ?
修改后的代码:
bean class:
package com.fss;
public class Choice {
private String choice;
private String fname;
private String fcontent;
public String getChoice() {
return choice;
}
public void setChoice(String choice) {
this.choice = choice;
}
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getFcontent() {
return fcontent;
}
public void setFcontent(String fcontent) {
this.fcontent = fcontent;
}
}
这是我的jsp页面:
<html>
<head>
<script>
function getUserChoice(){
var k=confirm("Do you want me to overwrite the existing file ?");
$('#choice').val(k);
return k;
}
</script>
</head>
<body>
<center><h3>${message} </h3></center>
<br>
<center><form method="POST" action="saveagain">
<input type="hidden" name="fname" value="${filename}">
<input type="hidden" name="fcontent" value="${filecontent}">
<input type="hidden" name="choice" id="choice"> <br><br>
<input type="submit" value="Overwrite ?" onclick="getUserChoice()"/></form></center>
<center><a href="toupload">Click me to upload a file</a> </center>
</body>
</html>
这是我的Controller类:
@RequestMapping(value="/saveagain",method = RequestMethod.POST)
public ModelAndView fileUploading2(@ModelAttribute("ch") Choice ch) throws IOException{
String ch1;
String message="";
ch1=ch.getChoice();
System.out.println("popup result "+ch1);
return new ModelAndView("fileUpload","message",message);
}
fname和fcontent值反映在控制器中。为什么选择价值没有反映在&#34; ch1&#34; ?
答案 0 :(得分:0)
使用您的方法,您可以在getChoice()函数中使用javascript将返回值(true / false)作为隐藏元素值并在服务器端检索
function getChoice() {
var k=confirm("Do you want me to overwrite the existing file ?");
$('#hidden_choice').val(k);
}
<!-- HTML element -->
<input type="hidden" name="choice" id="hidden_choice" />
<input id="button_id" type="button" value="Overwrite ?" onclick="getChoice()"/>
在服务器端,您可以使用@RequestParam来检索值。以下是示例代码:
@RequestMapping(value="/saveagain",method = RequestMethod.POST)
public ModelAndView fileUploading2(@RequestParam("choice") Boolean ch) throws IOException {
// do your thing with choice variable here
System.out.println("Choice is: " + ch);
}
如果布尔值不起作用,请尝试使用String并手动将其转换为布尔值。
答案 1 :(得分:0)
您可以在表单中创建一个变量,例如cofirmstatus
。
<input type="hidden" name="cofirmstatus" id="cofirmstatus">
并将值设置为
function getChoice(){
var k=confirm("Do you want me to overwrite the existing file ?");
$('#cofirmstatus').val(k);
return k;
}
因此,提交表单后,该值将在服务器端提供
修改强>
我按如下方式创建了JSP。服务器端无需更改。
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript">
function submitForm(){
var k=confirm("Do you want me to overwrite the existing file ?");
document.myform.choice.value=k;
document.myform.submit();
}
</script>
</head>
<body style="background-color:powderblue;color:Tomato">
<center><form method="POST" action="saveagain" name="myform">
<h3>${message} </h3>
<br> <br>
<input type="hidden" name="fname" value="${filename}">
<input type="hidden" name="fcontent" value="${filecontent}"> <br><br>
<input type="hidden" name="choice" id="choice"> <br><br>
<input type="button" value="Overwrite ?" onclick="javascript:submitForm()"/>
<input type="submit" value="Submit ?"></form></center>
<br>
<br>
<center><b>${m}</b></center>
<br><br>
<center><a href="toupload">Click me to upload a file</a> <br><br>
</center>
</body>
</html>
服务器上的两项操作输出:
popup result true
popup result false