最近开始接触到一个挺老项目,其中涉及到了一些jsp代码编写,
其中遇到一个问题比较麻烦,就是在jsp页面中iframe 的src属性动态修改的问题
页面:hello.jsp,table.jsp
页面上有三个标签:text,button,iframe
实现的功能是:点击button标签,将text标签内容作为src 的请求参数传递给服务端的jsp页面,在hello.jsp中动态显示子窗口表格内容
下面直接贴出代码:
hello.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hello</title>
</head>
<!-- <script src="/js/jquery.min.js"></script> -->
<script type="text/javascript">
function clickButton(){
var text = document.getElementById("name1").value;
var frame = document.getElementById('frame1');
frame.src="table.jsp?text="+text;
}
</script>
<body>
Hello ${name}
<br/><br/><br/><br/>
<input id="name1" type="text"></input>
<input id="buttton1" type="button" οnclick="clickButton()" value="Click me"/>
<iframe src="table.jsp" id="frame1"
frameborder="0" marginheight="0" marginwidth="0" height="700" width="100%"></iframe>
</body>
</html>
table.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%@ page import="springbootjsp.springbootjspweb.config.SpringManager"%>
<%@ page import="org.springframework.context.ApplicationContext"%>
<%@ page import="springbootjsp.springbootjspweb.service.HelloService"%>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>HELLO</title>
</head>
<body>
<%
String text = request.getParameter("text");
//HelloService helloService = SpringManager.getApplicationContext().getBean(HelloService.class);
%>
transport text from parent window is : <%=text %>
</body>
</html>
希望对大家有用吧,jsp(servlet) 从request中获取就可以了
如果希望使用post方法,要结合表单(form)的使用,注意form的target属性就ok了