如何在java中从ajax url获取值到action类,并在与popup相同的页面上重定向
我是ajax新手,我尝试使用下面的示例从复选框中获取bookid并将其传递给javascript。如果用户已经订购了该bookid,则弹出一条消息,说明您已经订购了该bookid。我正试图用ajax和javascript实现这一点。但是我的程序没有运行。请帮助我 Booking.jsp页面:其中包含所有可用书籍的表格如何在java中从ajax url获取值到action类,并在与popup相同的页面上重定向,java,javascript,ajax,jsp,struts2,Java,Javascript,Ajax,Jsp,Struts2,我是ajax新手,我尝试使用下面的示例从复选框中获取bookid并将其传递给javascript。如果用户已经订购了该bookid,则弹出一条消息,说明您已经订购了该bookid。我正试图用ajax和javascript实现这一点。但是我的程序没有运行。请帮助我 Booking.jsp页面:其中包含所有可用书籍的表格 <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=I
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript">
var xmlHttp;
function sendRequest(bookid)
{
var url = "ajaxcall.action?bookID="+bookid;
if (window.XMLHttpRequest)
{
xmlHttp=new XMLHttpRequest()
}
else if(window.ActiveXObject)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlHttp.onreadystatechange = getResponse;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}
function getResponse()
{
if(xmlHttp.readyState==4)
{
if(xmlHttp.status == 200)
{
alert(xmlhttp.responseText);
}
}
}
</script>
</head>
<body>
<s:form action="booking" method="post">
<table border="2" align="center" >
<tr>
<th></th>
<th > bookID </th>
<th > bookName </th>
<th > Description </th>
<th > AuthorName </th>
<th > bookStatus </th>
</tr>
<s:iterator value="bookList">
<tr>
<td><s:checkbox name="bookListID" fieldValue="%{bookID}" theme="simple" onclick= "sendRequest(this.value)"/></td>
<td><s:property value="%{bookID}" /></td>
<td><s:property value="%{bookName}" /></td>
<td><s:property value="%{description}" /></td>
<td><s:property value="%{authorName}" /></td>
<td><s:property value="%{bookStatus}" /></td>
</tr>
</s:iterator>
</table>
</br>
<s:submit id="submitbook" value="booking" theme="simple"></s:submit>
</s:form>
</body>
</html>
1.首先包括javascript文件。内部标题标签 2.在docuemtn ready$document.readyfunction{var xmlHttp;……您的代码……}之后,将您的函数放在里面
3.调用ajax函数时,请使用bookID而不是bookID,并且不需要太多解释。String result=user.bookStatusbookID//使用bookID我建议不要在浏览器中使用自己的AJAX版本。jQuery和类似的框架将为您提供一个更好的包装器来处理浏览器中的所有bug/缺陷;使用bookID而不是bookID,并且不需要更多的解释。String result=user.bookStatusbookID//使用bookID
<action name="ajaxcall" class="com.wipro.tns.action.BookingAction" method="ajaxcall">
<result name="success">/Booking.jsp</result>
<result name="failure">/Failure.jsp</result>
</action>
public String ajaxcall() {
System.out.println("in ajax call");
User user = new User();
String result = user.bookStatus(bookid);
// this bookid is the value of checkbox bookid and bookStatus() use to check the status like already booked or not
if(result.equals("alreadybooked")
{
//then it show popup in Booking.jsp page that you have already order this book
}
else
{
// user can book the bookid
}
return "failure"
}