JSP 7个动作指令

JSP 7个动作指令:

  • jsp:forward
  • jsp:include
  • jsp:useBean
  • jsp:setProperty
  • jsp:getProperty
  • jsp:param
  • jsp:plugin

一、forward 指令

将页面转发到另一页面。

<jsp:forward page="relativeURL">
    <jsp:param name="a" value="b" />
</jsp:forward>

通过request获取:

request.getParameter("a");

可以看到,用的方法与用户传递过来的参数获取方式一样。使用这种方法会将被请求页面的request转递给被转发的页面。

二、include 指令

<jsp:include page="relativeURL" flush="false">
    <jsp:param name="a" value="b" />
</jsp:include>

动态导入,flush用于指定输出缓存是否转移到被导入的文件中。

三、useBean、setProperty、getProperty指令

例子:

public class Person {
    private String name;
    private int age;
    
    public Person() {}
    
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public String getName() {
        return this.name;
    }
    
    public void setAge(int age) {
        this.age = age;
    }

    public int getAge() {
        return this.age;
    }
}
<jsp:useBean id="p" class="Person" scope="page" />
<jsp:setProperty name="p" property="name" value="Matro Leox" />
<jsp:setProperty name="p" property="age" value="18" />
<jsp:getProperty name="p" property="name" />
<jsp:getProperty name="p" property="age" />

其中scope表示作用域,它有如下值:

  • application 整个 Web 应用
  • session 本次会话
  • request 本次请求
  • page 本页面

其代码同:

<%
Person p = new Person();
pageContext.setAttribute("p", p);
p.setName("Matro Leox");
p.setAge(18);
%>
<%=p.getName()%>
<%=p.getAge()%>

四个作用域:

<%
application.setAttribute("p", p);
session.setAttribute("p", p);
request.setAttribute("p", p);
pageContext.setAttribute("p", p);
%>

四、plugin 指令

用少使用

五、param 指令

配合 forward 和 include 指令使用。

posted @ 2022-03-17 20:05  MatroLeox  阅读(108)  评论(0编辑  收藏  举报