基本上我有我的项目的模型建立这样:调用java属性文件时应使用哪个路径?
ModelFolder
----- SRC
----- 斌等
--- - PropertiesFolder1
-------- File1.properties
-------- File2.properties等
----- PropertiesFolder2
-------- File1.properties
-------- File2.properties等
---- - MainPropertiesFile1.properties
----- MainPropertiesFile2.properties
我想我的观点,这是一个动态的Web项目中使用它,我得到了属性文件终于加载我的Web项目改变
foo.load(new FileInputStream("foo.properties"));
后
foo.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("foo.properties"));
和出口项目的JAR文件,然后我包含在WEB-INF/lib目录下。但是,我不得不向模型中添加另一种方法,并且当我尝试测试该方法时,模型无法读取我的属性文件。我知道我可以使用FileInputStream和完整路径来获取在Model和View中工作的属性文件,但是有没有其他选择?
我不想每次切换计算机时都改变完整路径(我在工作时使用H:\ username \ ... \ Java \ Workspace,而在家里只是C:\ Java \ Workspace )。
我也不想将我的属性文件移动到不同的文件夹;最后我不想改变每次测试我的模型或视图时加载属性文件的方式。
有没有办法做到这一点?
这是推动我疯了,我已经试过所有的以下内容:
try
{
foo.load(this.getClass().getResourceAsStream("foo.properties"));
//foo.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("foo.properties"));
//foo.getClass().getResourceAsStream("foo.properties");
//foo.load(new FileInputStream("foo.properties"));
} catch (IOException ex)
{
al.logIntoProgrammerLog(ex);
}
模型中的所有或视图的那些行中进行工作的。有什么办法可以通过模型中的相对路径调用这些属性文件,然后以某种方式将模型与视图正确连接,以便找到并加载所有文件?
任何帮助将不胜感激;我是Java新手,所以我可能会错过一些非常简单的东西。谢谢。
编辑:
对不起,没有说明这一点,模型是一个Java项目,而视图是一个动态Web项目运行本地Tomcat服务器v6.0。
更好的(我希望)解释:
我查看有以下doPost方法一LoginServlet:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
String username = request.getParameter("usernameField");
String password = request.getParameter("passwordField");
ActivityLogger al = new ActivityLogger();
LoginController l_c = new LoginController();
//loginUser method will call my UserStorage class and
//will return true if UserStorage finds the User with those credentials in the db
//UserStorage is using a prepared sql statement stored in a properties file to find the User
//That properties file is not being read unless I specify the full path to it.
//Both Login and UserStorage are in the Model
if(l_c.loginUser(username, password))
{
//take user to welcome page
}
else
//display error
}
再次感谢
但是,我将如何从模型本身访问这些文件呢?基本上我的模型将用户存储在数据库中,并让他们登录和注销。我的观点是一个简单的登录页面。当用户尝试登录时,视图将从模型中调用LoginController。 LoginController加载包含SQL查询的.properties文件,以在数据库中查找用户。 – Bahrom 2013-05-01 21:54:36