2013-07-02 51 views
2

我有一個jsp頁面和一個類。我正在嘗試在jsp頁面中使用該類的信息。下面是代碼:類無法在jsp頁面解析

<?xml version="1.0" encoding="ISO-8859-1" ?> 
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
pageEncoding="ISO-8859-1"%> 
<%@ page import="user.Customer" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> 
<title>Insert title here</title> 
</head> 
<body> 

<h1>Main Menu</h1> 
<% session.setAttribute("username", Globals.customer.getUsername()); 
    session.setAttribute("password", Globals.customer.getPassword()); 
    session.setAttribute("name", Globals.customer.getName()); 
    session.setAttribute("surname", Globals.customer.getSurname()); 
    session.setAttribute("phone", Globals.customer.getPhone()); 
    session.setAttribute("address", Globals.customer.getAddress()); 
    session.setAttribute("email", Globals.customer.getEmail());%> 
<a href="editinfo.jsp">Edit your personal information</a> 
</body> 
</html> 

Globals類:

import user.Customer; 


public class Globals { 

public static Customer customer; 

} 

Customer類:

package user; 

public class Customer { 

public Customer(){} 

public Customer(String username,String password,String name,String surname,String phone,String address,String email){ 
    this.username=username; 
    this.password=password; 
    this.name=name; 
    this.surname=surname; 
    this.phone=phone; 
    this.address=address; 
    this.email=email; 
} 

private String username; 
private String password; 
private String name; 
private String surname; 
private String phone; 
private String address; 
private String email; 


public String getName(){ 
    return name; 
} 
public String getSurname(){ 
    return surname; 
} 
public String getUsername(){ 
    return username; 
} 
public String getPassword(){ 
    return password; 
} 
public String getEmail(){ 
    return email; 
} 
public String getPhone(){ 
    return phone; 
} 
public String getAddress(){ 
    return address; 
} 


} 

這簡直是我做的:當用戶登錄時,我首先創建一個客戶對象通過設置其用戶名,電子郵件等,然後我將它添加到當前會話。但在該行

session.setAttribute("username", Globals.customer.getUsername() 

它提供了一個錯誤,說

An error occurred at line: 14 in the jsp file: /main.jsp 
Globals cannot be resolved 
11: <body> 
12: 
13:  <h1>Main Menu</h1> 
14:  <% session.setAttribute("username", Globals.customer.getUsername()); 
15:  session.setAttribute("password", Globals.customer.getPassword()); 
16:  session.setAttribute("name", Globals.customer.getName()); 
17:  session.setAttribute("surname", Globals.customer.getSurname()); 

誰能幫助我?謝謝

+0

Globals類中的包聲明在哪裏? – Uooo

+0

我不明白你的代碼塊和'Global Class'的用法。爲什麼不直接使用'Customer class'中的'''''',因爲你想要訪問的所有'methods'都是在那裏定義的。 – Smit

+0

我解決了問題,謝謝你們 – yrazlik

回答

1

導入您Globals類以及

<%@ page import="user.Customer" %> 
<%@ page import="path.to.Globals" %> 

,或者使用頁面指令兩者的進口喜歡

<%@ page import="user.Customer,path.to.Globals" %> 
+0

謝謝你的作品! – yrazlik

+0

我會在1分鐘內:) – yrazlik

1

使用頁面指令或通過Globals類擴展該jsp頁面。
<%@page import="package.Globals" %>
其中包是你的名包..