posts - 5, comments - 1, trackbacks - 0, articles - 2
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

关于Java 接口实现的一个问题

Posted on 2009-05-17 12:09 bitsun 阅读(93) 评论(0)  编辑  收藏
Struts2 Action的接口定义
 1 //The interface of Action
 2 
 3 package com.blackstone;
 4 
 5 public interface Action
 6 {
 7     public static final String SUCCESS="success";
 8     public static final String NONE="none";
 9     public static final String ERROR="error";
10     public static final String INPUT="input";
11     public static final String LOGIN="login";
12     
13     public String execute()throws Exception;
14 }
实现一个LoginAction
 1 //Struts 2的Action类就是一个普通的Java类
 2 package com.blackstone;
 3 
 4 public class LoginAction implements com.blackstone.Action
 5 {
 6     private String username;
 7     private String password;
 8     
 9     public String getUsername()
10     {
11         return username;
12     }
13     
14     public String getPassword()
15     {
16         return password;
17     }
18     
19     public void setUsername(String username)
20     {
21         this.username=username;
22     }
23     
24     public void setPassword(String password)
25     {
26         this.password=password;
27     }
28     
29     public String execute()throws Exception
30     {
31         if(getUsername().equals("peter")&&getPassword().equals("123"))
32         {
33             return SUCCESS;
34         }
35         else
36         {
37             return ERROR;
38         }
39     }
40 }

问题:
编译Action成功
编译LoginAction出错
LoginAction.java:4: 找不到符号
符号: 类 Action
位置: 软件包 com.blackstone
public class LoginAction implements com.blackstone.Action
                                                  ^
LoginAction.java:33: 找不到符号
符号: 变量 SUCCESS
位置: 类 com.blackstone.LoginAction
                        return SUCCESS;
                               ^
LoginAction.java:37: 找不到符号
符号: 变量 ERROR
位置: 类 com.blackstone.LoginAction
                        return ERROR;
                               ^
3 错误

解决方法:
配置CLASSPATH变量,加入com.blackstone包所在的路径,这样Java解释器才能够定位Action.class,编译LoginAction.java的时候才不会找不到其中的静态变量

只有注册用户登录后才能发表评论。


网站导航: