在打開的模態窗口中,必須在head中加入:<base target='_self'>,否則請求完後,會跳轉到新窗口。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!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=UTF-8">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<title>111111</title>
<script type="text/javascript" src="js/jquery-1.8.0.js"></script>
<script type="text/javascript" src="./js/option.js"></script>
</head>
<body>
<a href="javascript:openwin();">Open Window</a><br>
你選擇了:<div id="div"></div>
</body>
</html>
打開的模態窗口:
<%@page import="java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<!-- 下面這一句是核心,否則在此頁面發送的請求,都會跳轉到新窗口(ajax除外,ajax請求完後,不會回到本頁面) -->
<base target='_self'>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<title>test</title>
<script type="text/javascript" src="js/jquery-1.8.0.js"></script>
<script type="text/javascript" src="./js/option.js"></script>
</head>
<body>
<a href="javascript:submits();">檢索</a>
<input type="button" οnclick="submits()" value="檢索"/>
<br>
<c:forEach items="${students}" var="entry">
<input type="text" class="sel" value="${entry.id}+${entry.name}"/><br>
</c:forEach>
</body>
<form action="SelectServlet">
<input type="hidden" name="select" value="select"/>
</form>
</html>
javascript腳本:
function openwin(){
var rtn=window.showModalDialog('SelectServlet','','scroll:0;status:0;help:0;resizable:1;dialogWidth:600px;dialogHeight:400px');
document.getElementById("div").innerHTML=rtn;
}
$(function(){
var lnks = document.getElementsByTagName("input");
for (var i=0; i<lnks.length; i++) {
if (lnks[i].getAttribute("class") == "sel") {
lnks[i].onclick = function() {
returnValue=this.value;
window.close();
// return false;
}
}
}
});
function submits(){
document.forms[0].submit();
}
java後臺模擬:
package com.test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SelectServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public SelectServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String select=request.getParameter("select");
Student s=new Student(12,"xiaoming");
Student s2=new Student(22,"hong");
Student s3=new Student(33,"ming");
List<Student> students=new ArrayList<Student>();
students.add(s);
students.add(s2);
students.add(s3);
if (select!=null) {
request.setAttribute("students", students);
}
request.getRequestDispatcher("/modalwindow2.jsp").forward(request, response);
}
}