JSP Action
1、定义
标准动作
1、定义
<jsp:actionName attribute="attrValue" > body </jsp:actionName>
2、常见的标准动作
<jsp:forward>:把到达的请求转发另一个页面进行处理
<jsp:useBean>:定义jsp页面使用一个JavaBean实例
<jsp:setProperty>:设置一个JavaBean中的属性值
<jsp:getProperty>:从JavaBean中获取一个属性值
<jsp:include>
1、include动作和include的指令的区别
include动作的包含
org.apache.jasper.runtime.JspRuntimeLibrary.include(request, response, "/action/inner.jsp", out, false);
include指令的包含
out.write("<!DOCTYPE html>\n");
out.write("<html>\n");
out.write("<head>\n");
out.write("<meta charset=\"UTF-8\">\n");
out.write("<title>inner</title>\n");
out.write("</head>\n");
out.write("<body>\n");
out.write("\n");
out.write("\t");
for( int i = 0 ; i < 10 ; i++ ) {
out.println( i ) ; // 这个 out 是 JSP 内置对象
}
out.write("\n");
out.write("\n");
out.write("</body>\n");
out.write("</html>");
2、属性
例如:绝对路径/action/inner.jsp,相对路径 inner.jsp(被包含的资源与当前页面在同一层次)
3、测试案例
include.jsp
<%@ page language = "java" pageEncoding = "UTF-8" %>
<%@ page contentType = "text/html; charset= UTF-8"%>
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>标准动作 : include</title> <style type="text/css"> div { border: 1px solid blue ; margin: 10px 10px ; padding: 10px 10px ; } </style> </head> <body> <div> <jsp:include page="/action/inner.jsp"></jsp:include> </div> <div> <jsp:include page="/action/inner.html"></jsp:include> </div> <div> <jsp:include page="/action/inner.txt"></jsp:include> </div> <div> <jsp:include page="/inner.do"></jsp:include> </div> </body> </html>
inner.jsp
<%@ page language = "java" pageEncoding = "UTF-8" %>
<%@ page contentType = "text/html; charset= UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>inner</title>
</head>
<body>
<%
for( int i = 0 ; i < 10 ; i++ ) {
out.println( i ) ; // 这个 out 是 JSP 内置对象
}
%>
</body>
</html>
inner.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <h1>我是内部的HTML页面</h1> </body> </html>
inner.txt
你好,我是文本文件
InnerServlet.java
package ecut.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.malajava.util.StringHelper; @WebServlet( "/inner.do" ) public class InnerServlet extends HttpServlet { private static final long serialVersionUID = 8241829948239427285L; @Override protected void service( HttpServletRequest request , HttpServletResponse response ) throws ServletException, IOException { PrintWriter w = response.getWriter(); w.println( "我是内部的Servlet" ); String username = request.getParameter( "username" ); if( StringHelper.notEmpty( username ) ) { w.println( "<p>" + username +"</p>" ) ; } } }
<jsp:param>
1、作用
2、测试案例
<%@ page language = "java" pageEncoding = "UTF-8" %>
<%@ page contentType = "text/html; charset= UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>标准动作 : include & param </title> <style type="text/css"> div { border: 1px solid blue ; margin: 10px 10px ; padding: 10px 10px ; } </style> </head> <body> <div> <jsp:include page="inner.jsp"> <jsp:param name="username" value="张三丰"/> </jsp:include> </div> <div> <jsp:include page="inner.html"></jsp:include> </div> <div> <jsp:include page="inner.txt"></jsp:include> </div> <div> <jsp:include page="../inner.do"> <jsp:param name="username" value="张三丰"/> </jsp:include> </div> </body> </html>
inner.jsp
<%@ page language = "java" pageEncoding = "UTF-8" %>
<%@ page contentType = "text/html; charset= UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>inner</title> </head> <body> <% for( int i = 0 ; i < 10 ; i++ ) { out.println( i ) ; // 这个 out 是 JSP 内置对象 } %>
<div>${ param.username }</div>
</body> </html>
<jsp:forward>
1、属性
2、元素
3、测试案例
<%@ page language = "java" pageEncoding = "UTF-8" %>
<%@ page contentType = "text/html; charset= UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>标准动作 : forward </title> </head> <body> <jsp:forward page="/WEB-INF/main.jsp" > <jsp:param name="username" value="Amy" /> </jsp:forward> </body> </html>
main.jsp
<%@ page language = "java" pageEncoding = "UTF-8" %>
<%@ page contentType = "text/html; charset= UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>VIP</title> </head> <body> <h1>这是付费才能看的片片哦</h1> <h3>${ param.username }</h3> </body> </html>
<jsp:useBean>
1、定义
Java Beans是一种软件组件
JavaBeans 是一种规范
JavaBeans是一个Java的类
有时称JavaBeans的实例为JavaBeans
2、Java Beans 编写要求
3、语法
<jsp:useBean id="id" scope="page|request|session|application" typeSpec/>
class = "className"
class = "className" type = "typeName"
beanName = "beanName" type = "typeName"
type = "typeName"
4、属性
用以指定Java 对象的实例名或引用对象的脚本变量的名称
通过id指定的名称来分辨不同的Bean
该名称严格区分大小写(相当于Java中的变量的名称)
该属性用以确定Bean 存在的范围以及id 变量名的有效范围,默认page
使用new 关键字和构造器构造一个实例
这个类必须是非抽象的,必须有公共的、无参构造
类名必须是包名+ 类名,严格区分大小写
使用instantiate 方法从一个class 中实例化一个Bean
instantiate方法在java.beans.Beans 中
beanName可以是package 或class ,也可以是表达式
package和class都是大小写敏感的
同时还可以指定Bean 的类型
type可以指定一个类,也可以是一个父类,或者是一个接口
如果没有使用class或beanName指定type,Bean将不会被实例化
5、测试案例
Student.java
package ecut.entity; import java.io.Serializable; import java.util.Date; public class Student implements Serializable { private static final long serialVersionUID = 3097161330831346815L; private Integer id; private String studentName; private char gender; private Date birthdate; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return studentName; } public void setName(String name) { this.studentName = name; } public char getGender() { return gender; } public void setGender(char gender) { this.gender = gender; } public Date getBirthdate() { return birthdate; } public void setBirthdate(Date birthdate) { this.birthdate = birthdate; } }
userbean.jsp
<%@ page language = "java" pageEncoding = "UTF-8" %>
<%@ page contentType = "text/html; charset= UTF-8"%> <%@ page import= "java.util.*" %>
<%@ page import= "org.malajava.util.*" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>标准动作: useBean</title> </head> <body> <%-- Student student = null ; --%> <%--student = (Student )pageContext.getAttribute("student");--%> <%-- if ( student == null ) { student =new Student();
pageContext.setAttribute("student" ,student);
} else {
//如果指定的scope中已有某个类型的对象就不再创建
}
--%> <jsp:useBean id="student" class="ecut.entity.Student" scope="page" /> <%-- student 。setId( 1001 );//studet.id =1001--%> <jsp:setProperty name="student" property="id" value="10001" /> <jsp:setProperty name="student" property="name" value="张三丰" /> <jsp:setProperty name="student" property="gender" value="男" /> <%Date birthdate = DateHelper.getDate( 1995 , 10 , 10 ) ; %> <jsp:setProperty name="student" property="birthdate" value="<%= birthdate %>"/> <p> JSP表达式:<%= student %> </p> <p> EL表达式:${ student } </p> <hr> <div> id : ${ student.id } </div> <div> name : ${ student.name } </div> <div> gender : ${ student.gender } </div> <div> birthdate : ${ student.birthdate } </div> <hr> <div> id : <jsp:getProperty name="student" property="id" /> </div> <div> name : <jsp:getProperty name="student" property="name" /> </div> </body> </html>
<jsp:getProperty>
1、作用
2、属性
<jsp:setProperty>
1、作用
2、语法
<jsp:setProperty name=”objName” property-expression />
property="*"
property="propertyName"
property="propertyName" param="parameterName"
property="propertyName" value="propertyValue"
3、测试案例
index.jsp
<%@ page language = "java" pageEncoding = "UTF-8" %>
<%@ page contentType = "text/html; charset= UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Standard Actions</title> <style type="text/css"> a { display: block ; margin: 20px 0px ; padding : 5px 5px ; border: 1px solid blue ; border-radius : 3px ; width: 60% ; text-decoration: none ; } </style> </head> <body> <h2>Standard Actions</h2> <a href="${ pageContext.request.contextPath }/action/include.jsp"><jsp:include></a> <a href="${ pageContext.request.contextPath }/action/forward.jsp"><jsp:forward></a> <a href="${ pageContext.request.contextPath }/action/param.jsp"><jsp:param></a> <a href="${ pageContext.request.contextPath }/action/useBean.jsp"><jsp:useBean> 、<jsp:setProperty>、<jsp:getProperty></a> <hr> <form action="${ pageContext.request.contextPath }/action/setProperty1.jsp" method="post"> <input type="text" name="id" placeholder="编号" > <input type="text" name="name" placeholder="姓名" > 性别: <input type="radio" name="gender" value="女" >女 <input type="radio" name="gender" value="男" >男 <input type="submit" value="提交" > </form> <hr> <form action="${ pageContext.request.contextPath }/action/setProperty2.jsp" method="post"> <input type="text" name="sid" placeholder="编号" > <input type="text" name="sname" placeholder="姓名" > 性别: <input type="radio" name="sgender" value="女" >女 <input type="radio" name="sgender" value="男" >男 <input type="submit" value="提交" > </form> </body> </html>
setProperty1.jsp
<%@ page language = "java" pageEncoding = "UTF-8" %>
<%@ page contentType = "text/html; charset= UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>标准动作 : setProperty</title> </head> <body> <jsp:useBean id="student" class="ecut.entity.Student" scope="page" /> <jsp:setProperty name="student" property="*" /> <div> <div> id : ${ student.id } </div> <div> name : ${ student.name } </div> <div> gender : ${ student.gender } </div> </div> </body> </html>
setProperty2.jsp
<%@ page language = "java" pageEncoding = "UTF-8" %>
<%@ page contentType = "text/html; charset= UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>标准动作 : setProperty</title> </head> <body> <jsp:useBean id="student" class="ecut.entity.Student" scope="page" /> <jsp:setProperty name="student" property="id" param="sid"/> <jsp:setProperty name="student" property="name" param="sname"/> <jsp:setProperty name="student" property="gender" param="sgender"/> <div> <div> id : ${ student.id } </div> <div> name : ${ student.name } </div> <div> gender : ${ student.gender } </div> </div> </body> </html>
转载请于明显处标明出处
https://www.cnblogs.com/AmyZheng/p/9164874.html
原文:https://www.cnblogs.com/AmyZheng/p/9164874.html