JSP基本语法-Java板块造梦空间论坛-技术交流-造梦空间论坛

JSP基本语法

文章目录[隐藏]

基本语法

  1. <% 放java方法/定义变量 %>:这个里面可以定义变量,也可以写iffor这类的逻辑语句。
    <% if (day <= 5 && day >= 1) {%>
    <p>今天非周末</p>
    <%}else{%>
    <p>今天是周末</p>
    <%} %>
    <% if (day <= 5 && day >= 1) {
    String a = "今天非周末";
    }else{
    String a = "今天是周末";
    } %>
    <% if (day <= 5 && day >= 1) {%>
           <p>今天非周末</p>
        <%}else{%>
          <p>今天是周末</p>
        <%} %>
    或
    <% if (day <= 5 && day >= 1) {
       String a = "今天非周末";
        }else{
       String a = "今天是周末";
        } %>
    <% if (day <= 5 && day >= 1) {%> <p>今天非周末</p> <%}else{%> <p>今天是周末</p> <%} %> 或 <% if (day <= 5 && day >= 1) { String a = "今天非周末"; }else{ String a = "今天是周末"; } %>
  2. <%= 输出变量 %>: 这个语法用于在 JSP 页面中输出 Java 变量的值。
    <h1>欢迎, <%= name %>加入造梦论坛!</h1>
    <h1>欢迎, <%= name %>加入造梦论坛!</h1>
    <h1>欢迎, <%= name %>加入造梦论坛!</h1>
  3. <%! 放java方法/定义全局变量 %>:这个语法可以定义类和各种方法。
    public String desc(int day){
    if (day <= 5 && day >= 1) {
    return "非周末";
    }else{
    return "今天周末";
    }
    }
    public  String desc(int day){
        if (day <= 5 && day >= 1) {
            return "非周末";
        }else{
            return "今天周末";
        }
    }
    public String desc(int day){ if (day <= 5 && day >= 1) { return "非周末"; }else{ return "今天周末"; } }

案例

  1. 输出一次比一次大的文字:
    <% for (int i = 1 ; i <=5 ; i++){%>
    <p style="font-size:<%=i*10%>px">造梦论坛</p>
    <%}%>
    <% for (int i = 1 ; i <=5 ; i++){%>
        <p style="font-size:<%=i*10%>px">造梦论坛</p>
    <%}%>
    <% for (int i = 1 ; i <=5 ; i++){%> <p style="font-size:<%=i*10%>px">造梦论坛</p> <%}%>
  2. 在第一题的基础上加上随机色:
    <% for (int i = 1 ; i <=10 ; i++){%>
    <% String[] color = {"Pink","Fuchsia","Cyan","Green","Coral"};
    %>
    <p style="font-size:<%=i*10%>px;color: <%= color[i% color.length]%>">造梦论坛</p>
    <%}%>
    <% for (int i = 1 ; i <=10 ; i++){%>
    <% String[] color = {"Pink","Fuchsia","Cyan","Green","Coral"};
    %>
    <p style="font-size:<%=i*10%>px;color: <%= color[i% color.length]%>">造梦论坛</p>
    <%}%>
    <% for (int i = 1 ; i <=10 ; i++){%> <% String[] color = {"Pink","Fuchsia","Cyan","Green","Coral"}; %> <p style="font-size:<%=i*10%>px;color: <%= color[i% color.length]%>">造梦论坛</p> <%}%>
  3. 随机颜色的九九乘法表:
    <style>
    table{
    border-collapse: collapse;
    }
    td{
    border: 1px black dashed;
    background-color: #e7e7e7;
    padding: 5px;
    margin: 0;
    }
    </style>
    <table>
    <% for (int i = 1; i <= 9 ; i++) { %>
    <tr>
    <% for (int j = 1; j <=i ; j++) { %>
    <% String[] color = {"Pink","Fuchsia","Cyan","Green","Coral","Gold"}; %>
    <td class="box" style="color: <%= color[(j+i)% color.length]%>"> <%= i %>X<%= j%>=<%= i*j %></td>
    <% } %>
    </tr>
    <% } %>
    </table>
    <style>
        table{
            border-collapse: collapse;
        }
        td{
            border: 1px black dashed;
            background-color: #e7e7e7;
            padding: 5px;
            margin: 0;
        }
    </style>
    <table>
        <% for (int i = 1; i <= 9 ; i++) { %>
        <tr>
    <% for (int j = 1; j <=i ; j++) { %>
            <% String[] color = {"Pink","Fuchsia","Cyan","Green","Coral","Gold"}; %>
    <td class="box" style="color: <%= color[(j+i)% color.length]%>"> <%= i %>X<%= j%>=<%= i*j %></td>
    <% } %>
        </tr>
        <% } %>
    </table>
    <style> table{ border-collapse: collapse; } td{ border: 1px black dashed; background-color: #e7e7e7; padding: 5px; margin: 0; } </style> <table> <% for (int i = 1; i <= 9 ; i++) { %> <tr> <% for (int j = 1; j <=i ; j++) { %> <% String[] color = {"Pink","Fuchsia","Cyan","Green","Coral","Gold"}; %> <td class="box" style="color: <%= color[(j+i)% color.length]%>"> <%= i %>X<%= j%>=<%= i*j %></td> <% } %> </tr> <% } %> </table>

    5f90f5693a104110

请登录后发表评论

    没有回复内容

© 造梦空间论坛
❤富强❤