更新时间:2022-09-16 23:02:45
JSP行为(JSP Actions)是一组JSP内置的标签,只书写少量的标记代码就能够使用JSP提供丰富的功能,JSP行为是对常用的JSP功能的抽象和封装。
为什么我不把它直接称为JSP标签呢?我把这些JSP内置的标签称之为JSP行为,能够和JSTL标签区分开来。当然了,你也可以把它称之为JSP标签,你不要搞混就行了。我个人喜欢把这些JSP内置标签称之为JSP行为。
<jsp:include page=""/>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>包含页头和页尾进来</title> </head> <body> <jsp:include page="head.jsp"/> <jsp:include page="foot.jsp"/> </body> </html>
访问1.jsp页面看一下效果:
使用jsp行为来包含文件,jsp源文件是这样子的:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>页头</title> </head> <body> <% String s = "zhongfucheng"; %> 我是页头呀 <br> <br> <br> </body> </html>
我的页尾也有个名为s的字符串变量
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>页尾</title> </head> <body> <% String s = "zhongfucheng"; %> 我是页尾呀 </body> </html>
现在我使用静态包含看看会发生什么,出现异常了。
出现异常的原因很简单,就是同一个文件中有两个相同的变量s
使用动态包含就可以避免这种情况
<jsp:forward page=""/>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>访问1.jsp就跳转到head.jsp</title> </head> <body> <jsp:forward page="head.jsp"/> </body> </html>
看一下效果
<jsp:forward page="head.jsp"> <jsp:param name="username" value="zhongfucheng"/> </jsp:forward>
在head.jsp页面中获取到传递过来的参数
<% String ss = request.getParameter("username"); %> 获取到的参数是: <%=ss%>
效果如下图所示
<jsp:directive.include file="head.jsp"></jsp:directive.include> <jsp:directive.include file="foot.jsp"></jsp:directive.include>
看下效果,正常可以包含页面:
<jsp:scriptlet>
替代<%%>是同样一个道理<jsp:useBean id=""/>
<jsp:setProperty name="" property=""/>
<jsp:getProperty name="" property=""/>
如果文章有错的地方欢迎指正,大家互相交流。