2

私は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>

グローバル クラス:

import user.Customer;


public class Globals {

public static 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;
}


}

ユーザーがログインすると、まずユーザー名、電子メールなどを設定して Customer オブジェクトを作成し、それを現在のセッションに追加します。しかし、ラインで

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());

誰でもこれで私を助けることができますか?ありがとう

4

2 に答える 2

1

page ディレクティブを使用するか、その JSP ページを Globals クラスで拡張します。
つまり<%@page import="package.Globals" %>
、package はパッケージの名前です。

于 2013-07-02T06:59:36.250 に答える
1

Globalsクラスもインポートします

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

または、両方のインポートに単一のページディレクティブを使用します

<%@ page import="user.Customer,path.to.Globals" %>
于 2013-07-02T07:00:04.317 に答える