A. 採用SSM框架的javaweb工程目錄結構是怎麼樣
借鑒github某項目的目錄
├── SSM-API // common API
│ ├── src/main
│ ├── ├──java/com/crossoverJie // specific code。
│ ├── ├──resources
├── SSM-BOOT // Available for internal use of bbo dependencies
│ ├── ├──resources/spring // bbo consumer configuration
├── SSM-SERVICE // The service implementation of the bbo application
│ ├── src/main
│ ├── ├──java/com/crossoverJie/api // specific code
│ ├── ├──├──controller // Heartbeat detection interface
│ ├── ├──├──bbo // Dubbo related code
│ ├── ├──├──├── // package
│ ├── ├──├──├──pojo // pojo package
│ ├── ├──├──├──service // service package
│ ├── ├──├──├──util // Toolkit
│ ├── ├──├──impl // implement bbo API
│ ├── ├──resources // configuration file
│ ├── ├──├──mapping // *.mapper configuration file
│ ├── ├──├──spring // Spring related configuration file
├── SSM-WEB // web application
│ ├── src/main
│ ├── ├──java/com/crossoverJie // specific code
│ ├── ├──├──controller // controller package
│ ├── ├──├──cxf // CXF related code
│ ├── ├──├── // package
│ ├── ├──├──enums // enum package
│ ├── ├──├──intercept // Interceptor
│ ├── ├──├──lucene // Lucene related code
│ ├── ├──├──pojo // pojo package
│ ├── ├──├──req // request package
│ ├── ├──├──res // response package
│ ├── ├──├──service // service pachage
│ ├── ├──├──shiro // shiro related code
│ ├── ├──├──util // Toolkit
│ ├── ├──├──vo // vo package
│ ├── ├──resources
│ ├── ├──├──mapping // *.mapper configuration file
│ ├── ├──webapp // front code
├── doc
│ ├──lucene // lucene related code
│ ├──sql // sql scripts
├── .gitignore // gitignore
├── pom.xml // parent pom
├── LICENSE
├── README.md
B. java中獲取工程中res目錄路徑的方法
第一種:
File f = new File(this.getClass().getResource("/").getPath());
System.out.println(f);
結果:
C:\Documents%20and%20Settings\Administrator\workspace\projectName\bin
獲取當前類的所在工程路徑;
如果不加「/」
File f = new File(this.getClass().getResource("").getPath());
System.out.println(f);
結果:
C:\Documents%20and%20Settings\Administrator\workspace\projectName\bin\com\test
獲取當前類的絕對路徑;
第二種:
File directory = new File("");//參數為空
String courseFile = directory.getCanonicalPath() ;
System.out.println(courseFile);
結果:
C:\Documents and Settings\Administrator\workspace\projectName
獲取當前類的所在工程路徑;
第三種:
URL xmlpath = this.getClass().getClassLoader().getResource("selected.txt");
System.out.println(xmlpath);
結果:
file:/C:/Documents%20and%20Settings/Administrator/workspace/projectName/bin/selected.txt
獲取當前工程src目錄下selected.txt文件的路徑
第四種:
System.out.println(System.getProperty("user.dir"));
結果:
C:\Documents and Settings\Administrator\workspace\projectName
獲取當前工程路徑
第五種:
System.out.println( System.getProperty("java.class.path"));
結果:
C:\Documents and Settings\Administrator\workspace\projectName\bin
獲取當前工程路徑
C. java開發項目,目錄結構大家一般怎麼劃分
ssh框架,mvc模式
(持久層介面),impl(持久層實現),filter(過濾器),action(action),util(工具類包),bean(實體類包),service(業務層介面),serviceimpl(業務層實現),interceptor(攔截器)
每個公司要求都不一樣,有搭建好的項目框架給你,我是做web的,大多數都是這么幾個包,只是名不同
D. 在java類中怎麼獲得java項目的目錄
一 相對路徑的獲得
說明:相對路徑(即不寫明時候到底相對誰)均可通過以下方式獲得(不論是一般的項目還是web項目)
String relativelyPath=System.getProperty("user.dir");
上述相對路徑中,java項目中的文件是相對於項目的根目錄
web項目中的文件路徑視不同的web伺服器不同而不同(tomcat是相對於 tomcat安裝目錄\bin)
二 類載入目錄的獲得(即當運行時某一類時獲得其裝載目錄)
1.1)通用的方法一(不論是一般的java項目還是web項目,先定位到能看到包路徑的第一級目錄)
InputStream is=TestAction.class.getClassLoader().getResourceAsStream("test.txt");
(test.txt文件的路徑為 項目名\src\test.txt;類TestAction所在包的第一級目錄位於src目錄下)
上式中將TestAction,test.txt替換成對應成相應的類名和文件名字即可
1.2)通用方法二 (此方法和1.1中的方法類似,不同的是此方法必須以'/'開頭,參考http://riddickbryant.iteye.com/blog/436693)
InputStream is=Test1.class.getResourceAsStream("/test.txt");
(test.txt文件的路徑為 項目名\src\test.txt,類Test1所在包的第一級目錄位於src目錄下)
三 web項目根目錄的獲得(發布之後)
1 從servlet出發
可建立一個servlet在其的init方法中寫入如下語句
ServletContext s1=this.getServletContext();
String temp=s1.getRealPath("/"); (關鍵)
結果形如:D:\工具\Tomcat-6.0\webapps\002_ext\ (002_ext為項目名字)
如果是調用了s1.getRealPath("")則輸出D:\工具\Tomcat-6.0\webapps\002_ext(少了一個"\")
2 從httpServletRequest出發
String cp11111=request.getSession().getServletContext().getRealPath("/");
結果形如:D:\工具\Tomcat-6.0\webapps\002_ext\
四 classpath的獲取(在Eclipse中為獲得src或者classes目錄的路徑)
方法一 Thread.currentThread().getContextClassLoader().getResource("").getPath()
eg: String t=Thread.currentThread().getContextClassLoader().getResource("").getPath();
System.out.println("t---"+t);
輸出:t---/E:/order/002_ext/WebRoot/WEB-INF/classes/
方法二 JdomParse.class.getClassLoader().getResource("").getPath() (JdomParse為src某一個包中的類,下同)
eg:String p1=JdomParse.class.getClassLoader().getResource("").getPath();
System.out.println("JdomParse.class.getClassLoader().getResource--"+p1);
輸出: JdomParse.class.getClassLoader().getResource--/E:/order/002_ext/WebRoot/WEB-INF/classes/
另外,如果想把文件放在某一包中,則可以 通過以下方式獲得到文件(先定位到該包的最後一級目錄)
eg String p2=JdomParse.class.getResource("").getPath();
System.out.println("JdomParse.class.getResource---"+p2);
輸出: JdomParse.class.getResource---/E:/order/002_ext/WebRoot/WEB-INF/classes/jdom/ (JdomParse為src目錄下jdom包中的類)
四 屬性文件的讀取:
方法 一
InputStream in = lnew BufferedInputStream( new FileInputStream(name)); Properties p = new Properties(); p.load(in);
注意路徑的問題,做執行之後就可以調用p.getProperty("name")得到對應屬性的值
方法二
Locale locale = Locale.getDefault();
ResourceBundle localResource = ResourceBundle.getBundle("test/propertiesTest", locale);
String value = localResource.getString("test");
System.out.println("ResourceBundle: " + value);
工程src目錄下propertiesTest.properties(名字後綴必須為properties)文件內容如下:
test=hello word
E. java項目目錄(src/main/java; src/main/resources)怎麼建立
eclipse新建maven webapp後無法添加src/main/java和src/main/test