-----------------------------.JSP动态标签---------------------------------
JSP动态标签
JSP自己的标签,不用导包!
JSP动态标签是JSP自己的标签,不是由第三方提供的,所以使用JSP动态标签时无需使用taglib指令“导包”。
JSP动态标签的格式为:<jsp:xxx …>
无论是自定义的JSP标签,还是JSP自己的动态标签,还有第三方的标签,最终都会对应一组方法的调用!!!
1 include动态标签
include标签是动态包含,与include指令不同,include标签与RequestDispatcher.include()方法的功能是相同的。
hel.jsp
<body> <h1>hel.jsp</h1> <jsp:include page="lo.jsp" /> </body>
扫描二维码关注公众号,回复:
2681813 查看本文章
|
lo.jsp
<% out.println("<h1>lo.jsp</h1>"); %> |
动态包含是会为两个JSP页面都生成“真身”,然后hel.jsp的“真身”中会调用lo.jsp的“真身”的_jspService()方法而已。
Hel.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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"> <title>Insert title here</title> </head> <body> <%-- 取出包含页面传送的值 --%> <% String name = request.getParameter("name"); %> <%-- 根据包含页面传送不同的值,输出的值也不同,这就是动态标签 --%> <h1><%=name %></h1> </body> </html> |
Lo.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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"> <title>Insert title here</title> </head> <body> <%-- 向被包含页面传送值 ,静态标签适合包含一个文件,如CSS文件 而动态标签适合包含一个页面上的一个版块--%> <jsp:include page="/hel.jsp?name=likunpeng"></jsp:include> </body> </html> |
2 forward动态标签
forward标签的作用是请求转发!forward标签的作用与RequestDispatcher#forward()方法相同。
hel.jsp
lo.jsp
<% out.println("<h1>lo.jsp</h1>"); %> |
注意,显示结果中没有<h1>hel.jsp</h1>,即hel.jsp中的所有输出都会被清除!
模拟登陆页面:
Login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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"> <title>Insert title here</title> </head> <body> <form action="/jsp_demo4/login.jsp" method="post"> <input type="hidden" name="isSubmit" value="1"> 用户名:<input type="text" name="username"><br> 密码 :<input type="password" name="password"><br> <input type="submit" value="提交"> </form>
<% String isSubmit = request.getParameter("isSubmit"); if("1".equals(isSubmit)){ String username = request.getParameter("username"); String password = request.getParameter("password"); if("likunpeng".equals(username)&&"123".equals(password)){ //将用户和密码添加到request域对象中 request.setAttribute("userInfor", username +"你好,欢迎登录!"); %> <jsp:forward page="/success.jsp"></jsp:forward> <% }else{ %> <jsp:forward page="/fail.jsp"></jsp:forward> <% } %> <% } %> </body> </html> |
Success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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"> <title>Insert title here</title> </head> <body> <h1>登录成功</h1> <% String userInfor = (String)request.getAttribute("userInfor"); %> <h1><%=userInfor %></h1> </body> </html> |
Fail.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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"> <title>Insert title here</title> </head> <body> <h1>用户名或者密码错误</h1> </body> </html> |
3 useBean、setProperty、getProperty动态标签
useBean
先说一下与JavaBean相关的动态标签在JSP Model2中已经很少使用了。在JSP Model1中,JSP页面直接使用JavaBean,那里使用这些标签是很有用的!但是,这里还是要介绍一下。
在开始测试JavaBean动态标签之前,我们需要先创建一个javaBean类:cn.itcast.domain.Person类。
因为useBean标签有点小复杂,所以我们把useBean标签的作用直接翻译成Java代码!
<jsp:useBean id="p1" class="cn.itcast.domain.Person" /> |
<% Person p1 = (Person)pageContext.getAttribute("p1"); if(p1 == null) { p1 = new Person(); pageContext.setAttribute("p1", p1); } %> |
<jsp:useBean id="p1" class="cn.itcast.domain.Person" scope="session" /> |
<% Person p1 = (Person)session.getAttribute("p1"); if(p1 == null) { p1 = new Person(); session.setAttribute("p1", p1); } %> |
setProperty
setProperty标签的作用是给Bean设置属性值。当然,你必须保证Bean已经存在的前提下才能设置属性值。
<jsp:useBean id="p1" class="cn.itcast.domain.Person" /> <jsp:setProperty property="sex" value="male" name="p1"/> |
<% Person p = (Person)pageContext.getAttribute("p1"); if(p == null) { throw new NullPointerException(); } p.setSex("male"); %> |
getProperty:
getProperty标签的作用是获取Bean的属性值。
<jsp:useBean id="p1" class="cn.itcast.domain.Person" /> <jsp:setProperty property="sex" value="male" name="p1"/> <jsp:getProperty property="sex" name="p1" /> |
useBean.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@page import="com.rl.model.*" %> <!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"> <title>Insert title here</title> </head> <body> <%-- 通过jsp:useBean默认给pageContext的设置一个属性 可以通过jsp:useBean的scope属性来选择四个域对象中的一个来设置属性 --%> <jsp:useBean id="p" class="com.rl.model.Person"></jsp:useBean> <jsp:useBean id="p1" class="com.rl.model.Person" scope="request"></jsp:useBean> <jsp:useBean id="p2" class="com.rl.model.Person" scope="session"></jsp:useBean> <jsp:useBean id="p3" class="com.rl.model.Person" scope="application"></jsp:useBean> <%-- 给p3对象的name设置值,name指的是对象名,property是指对象中的属性名,value指的是对象中属性的指 --%> <jsp:setProperty property="name" name="p3" value="likunpeng"/> <% //获得Person对象 Person person = (Person)pageContext.getAttribute("p"); Person person1 = (Person)request.getAttribute("p1"); Person person2 = (Person)session.getAttribute("p2"); Person person3 = (Person)application.getAttribute("p3"); %> <%-- 获取到的person对象里面的值是空的 --%> <h1>pageContext域</h1> <%=person %> <hr> <h1>request域</h1> <%=person1 %> <h1>session域</h1> <%=person2 %> <hr> <h1>application域</h1> <%=person3 %><br> <%=person3.getName() %> <hr> </body> </html> |
setProperty的自省机制:
form.jsp
<form action="bean.jsp" method="post"> 姓名:<input type="text" name="name"/><br/> 年龄:<input type="text" name="age"/><br/> 性别:<input type="text" name="sex"/><br/> <input type="submit" value="提交"/> </form> |
bean.jsp
<jsp:useBean id="p1" class="cn.itcast.domain.Person" /> <jsp:setProperty property="*" name="p1"/> <%=p1 %> |
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@page import="com.rl.model.*" %> <!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"> <title>Insert title here</title> </head> <body> <jsp:useBean id="p" class="com.rl.model.Person"></jsp:useBean> <%-- <jsp:setProperty property="name" name="p"/> <jsp:setProperty property="age" name="p"/> --%> <%--简便给属性设值 --%> <jsp:setProperty property="*" name="p"/>
<% //<jsp:setProperty property="*" name="p"/>相当于以下java代码 String name = request.getParameter("name"); String age = request.getParameter("age");
Person person1 = new Person(); person1.setName(name); person1.setAge(Integer.valueOf(age));
pageContext.setAttribute("p", person1,pageContext.PAGE_SCOPE); %>
<% Person person = (Person)pageContext.getAttribute("p"); %> <h1><%=person %></h1> <h1><%=person.getName() %></h1> <h1><%=person.getAge() %></h1> <hr>
<%-- 使用jsp:getProperty输出域对象中的属性 --%> <h1>姓名:<jsp:getProperty property="name" name="p"/></h1> <h1>年龄:<jsp:getProperty property="age" name="p"/></h1> </body> </html> |