JSTL 标准标签库

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

<%@ 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">
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt"%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSTL IF 标签</title>
    </head>
    <body>
            <form action="JstlServlet" method="post">
            姓名:<input name="stuName"><br>
            年龄:<input name="age"><br>
            性别:<input name="sex" type="radio" value="m"><input name="sex" type="radio" value="f">女<br>
            学历:
            <input name="level" type="radio" value="zk">专科
            <input name="level" type="radio" value="bk" checked="checked">本科
            <input name="level" type="radio" value="ss">硕士
            <input name="level" type="radio" value="bs">博士
            <input name="level" type="radio" value="qt">其他<br>
            <input type="submit" value="提 交"><br>
            
        </form>
    </body>
</html>
package com.xzit.servlet;

import java.io.IOException;

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 com.xzit.domain.Student;

/**
 * Servlet implementation class JstlServlet
 */
@WebServlet("/JstlServlet")
public class JstlServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public JstlServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
     */
    protected void service(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        String name = request.getParameter("stuName");
        String sex = request.getParameter("sex");
        String age = request.getParameter("age");
        String lev = request.getParameter("level");
        int ageInt = 10;
        if(age != null) {
            ageInt = Integer.parseInt(age);
        }
        Student stu = new Student();
        stu.setAge(ageInt);
        stu.setStuName(name);
        stu.setSex(sex);
        stu.setLevel(lev);
        request.setAttribute("student", stu);
        request.getRequestDispatcher("iftag.jsp").forward(request, response);
    }
}
<%@ 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">
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt"%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSTL IF 标签</title>
    </head>
    <body>
        <!-- IF 标签的使用 -->
        <c:if test="${requestScope.student ne null}">
            <c:if test="${requestScope.student.sex eq 'm'}">
                <img src="imgs/m.png"/>
            </c:if>
            <c:if test="${requestScope.student.sex eq 'f'}">
                <img src="imgs/f.png"/>
            </c:if>
            <br>
            <!-- choose when 标签的使用 -->
            <c:choose>
                <c:when test="${requestScope.student.level eq 'zk'}">
                    学生:${requestScope.student.stuName}的学历是:<span style="color:red;font-size:24px">专科</span>
                </c:when>
                <c:when test="${requestScope.student.level eq 'bk'}">
                    学生:${requestScope.student.stuName}的学历是:<span style="color:red;font-size:24px">本科</span>
                </c:when>
                <c:when test="${requestScope.student.level eq 'ss'}">
                    学生:${requestScope.student.stuName}的学历是:<span style="color:red;font-size:24px">研究生</span>
                </c:when>
                <c:when test="${requestScope.student.level eq 'bs'}">
                    学生:${requestScope.student.stuName}的学历是:<span style="color:red;font-size:24px">博士</span>
                </c:when>
                <c:otherwise>
                    学生:${requestScope.student.stuName}的学历是:<span style="color:red;font-size:24px">其它学历水准</span>
                </c:otherwise>
            </c:choose>
        </c:if>
    </body>
</html>
package com.xzit.domain;

public class Student {

    private String stuName;
    
    private int age;
    
    private String sex;
    
    private String level; //学历
    
    public String getStuName() {
        return stuName;
    }
    public void setStuName(String stuName) {
        this.stuName = stuName;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public String getLevel() {
        return level;
    }
    public void setLevel(String level) {
        this.level = level;
    }
    public Student() {
        
    }
    public Student(String stuName, int age) {
        super();
        this.stuName = stuName;
        this.age = age;
    }
}

 

posted @ 2022-02-22 23:02  伊万  阅读(38)  评论(0编辑  收藏  举报