jsp EL(3)

来源:互联网 发布:国家公派留学博士知乎 编辑:程序博客网 时间:2024/05/16 06:46

/**

 * File: Profile.java

 * Author: huaxuhe

 * Function: 用户详细信息类

 */

package com.huaxuhe.jspel;

 

import java.util.Map;

 

public class Profile {

    private Map<String, String> phone;// 电话

   

    private Address[] address;// Address instance

   

    private String birthDate;// 生日

   

    private String email;// 邮箱

 

    // default constructed function

    public Profile() {

 

    }

 

    // Getter and Setter method

    public Map<String, String> getPhone() {

       return phone;

    }

 

    public void setPhone(Map<String, String> phone) {

       this.phone = phone;

    }

 

    public Address[] getAddress() {

       return address;

    }

 

    public void setAddress(Address[] address) {

       this.address = address;

    }

 

    public String getBirthDate() {

       return birthDate;

    }

 

    public void setBirthDate(String birthDate) {

       this.birthDate = birthDate;

    }

 

    public String getEmail() {

       return email;

    }

 

    public void setEmail(String email) {

       this.email = email;

    }

}

 

/**

 * File: User.java

 * Author: huaxuhe

 * Function: 用户类

 */

package com.huaxuhe.jspel;

 

public class User {

 

    private int id;// 编号

 

    private String username;// 用户名

 

    private String password;// 密码

 

    private Profile profile;// Profile instance

 

    // default constructed function

    public User() {

 

    }

 

    // Getter and Setter method

    public int getId() {

       return id;

    }

 

    public void setId(int id) {

       this.id = id;

    }

 

    public String getUsername() {

       return username;

    }

 

    public void setUsername(String username) {

       this.username = username;

    }

 

    public String getPassword() {

       return password;

    }

 

    public void setPassword(String password) {

       this.password = password;

    }

 

    public Profile getProfile() {

       return profile;

    }

 

    public void setProfile(Profile profile) {

       this.profile = profile;

    }

 

}

 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%@ page import="com.huaxuhe.jspel.*"%>

<html>

    <head>

       <title>jspel</title>

    </head>

    <body>

       <jsp:useBean id="user" class="com.huaxuhe.jspel.User"/>

       <jsp:setProperty name="user" property="id" value="1"/>

       <jsp:setProperty name="user" property="username" value="huaxuhe"/>

       <jsp:setProperty name="user" property="password" value="java"/>

       <%

       Profile profile = new Profile();

       profile.setEmail("huaxuhe@126.com");

       profile.setBirthDate("11-13");

       Map<String, String> phone = new HashMap<String, String>();

       phone.put("handset", "20080808");

       profile.setPhone(phone);

       Address address = new Address();

       address.setAddr("WuChang");

       address.setCity("WuHan");

       address.setCountry("China");

       address.setPostCode("10000");

       Address[] addresses = {address};

       profile.setAddress(addresses);

       user.setProfile(profile);

       %>

       <%

       pageContext.setAttribute("username", null);

       pageContext.setAttribute("password", "");

       pageContext.setAttribute("title", "The Way Of Java");

       pageContext.setAttribute("time", new java.util.Date());

       %>

      

       ${user.id}<br><!-- 1 -->

       ${user.username}<br><!-- huaxuhe -->

       ${user.password}<br><!-- java -->

       ${user.profile.phone.handset}<br><!-- 20080808 -->

       ${user.profile.birthDate}<br><!-- 11-13 -->

       ${user.profile.email}<br><!-- huaxuhe@126.com -->

       ${user.profile.address[0].postCode}<br><!-- 10000 -->

       ${user.profile.address[0].country}<br><!-- China -->

       ${user.profile.address[0].city}<br><!-- WuHan -->

       ${user.profile.address[0].addr}<br><!-- WuChang -->

   

    </body>

</html>

 

 


5.JSP EL中的内置对象

web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.5"

    xmlns="http://java.sun.com/xml/ns/javaee"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <context-param>

    <param-name>name</param-name>

    <param-value>huali</param-value>

    </context-param>

  <welcome-file-list>

    <welcome-file>index.jsp</welcome-file>

  </welcome-file-list>

</web-app>

 

index.jsp


<%@ page language="java" pageEncoding="UTF-8"%>

<html>

    <head>

       <title>jspel</title>

    </head>

    <body>

    <form action="jspel.jsp" method="post">

    <select name="gander">

    <option>boy</option>

    <option>girl</option>

    </select>

    <br>

    Learnning language:

    <input type="checkbox" name="language" value="Java"/>Java

    <input type="checkbox" name="language" value="C"/>C

    <input type="checkbox" name="language" value="C#"/>C#

    <br>

    <input type="submit" value="submit"/>

    </form>

    </body>

</html>

 

jspel.jsp


<%@ page language="java" pageEncoding="UTF-8"%>

<html>

    <head>

       <title>jspel</title>

    </head>

    <body>

       <!-- JSP EL内置对象pageScope、requestScope、sessionScope、applicationScope -->

       <%

       pageContext.setAttribute("name", "huali");

       request.setAttribute("name", "huabang");

       session.setAttribute("name", "huajun");

       application.setAttribute("name" ,"huaxuhe");

       %>

       ${pageScope.name}<br><!-- huali -->

       ${requestScope.name}<br><!-- huabang -->

       ${sessionScope.name}<br><!-- huajun -->

       ${applicationScope.name}<br><!-- huaxuhe -->

       <!-- 按page、request、session、application依次查找name -->

       ${name}<br><!-- huali -->

      

       <!-- JSP EL内置对象cookie -->

       <%

       response.addCookie(new Cookie("space", "The Way Of Java"));

       %>

       ${cookie.space.value}<br><!-- The Way Of Java -->

      

       <!-- JSP EL内置对象header -->

       ${header.host}<br><!-- localhost:8888 -->

       ${header["user-agent"]}<br>

       <!-- Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; CIBA)-->

      

       <!-- JSP EL内置对象param、paramValues -->

       ${param.gander}<br><!-- boy or girl -->

       ${paramValues.language[0]}<br><!-- Java or C or C# or "" -->

      

       <!-- JSP EL内置对象initParam -->

       ${initParam.name}<br><!-- huali -->

      

    </body>

</html>

原创粉丝点击