架構探險筆記1
新建Maven項目
IDEA新建Maven項目,選擇導包方式,Import Changes為手動導包,Enable Auto-Import為自動導包。註意Maven配置(全局配置File->Other Setting->Default Settings)。
在pom.xml中設置maven編譯編碼及編譯JDK
<!--編碼方式UTF-8--> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <build> <plugins> <!--編譯Compile,使用JDK1.7開發--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.3</version> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> </plugins> </build>
Maven中央倉庫http://search.maven.org/
設置打包時跳過測試
<!--打包時跳過測試--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.18.1</version> <configuration> <skipTests>true</skipTests> </configuration> </plugin>
新建項目完成
將項目轉為Java Web項目
在main下面新建webapp,在webapp下面新建WEB-INF,在WEB-INF下面新建web.xml
新建好web.xml文件之後,彈出檢測到項目為web,點擊Configure配置。
在web.xml中添加代碼
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> </web-app>
添加Maven依賴
web項目是要打包成war包的,所以要在pom.xml文件裏設置packaging為war(默認為jar)
<packaging>war</packaging>
添加servlet、jsp、jstl等依賴
<dependencies> <!--servlet--> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <!--jsp--> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> <scope>provided</scope> </dependency> <!--jstl--> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> <scope>runtime</scope> </dependency> </dependencies>
Maven依賴三坐標(groupId、artifactId、version)必須提供
某些依賴只需編譯,無需參與打包(例如,Tomcat自帶了servlet與jsp所對應的jar包),可將其scope設置為provided。
某些依賴只在運行時需要,無須參與編譯(JSTL的jar包),可將其scope設置為runtime。
完整版的pom.xml配置
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.smart4j</groupId> <artifactId>chapter1</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <dependencies> <!--servlet--> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <!--jsp--> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> <scope>provided</scope> </dependency> <!--jsp--> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> <scope>runtime</scope> </dependency> </dependencies> <!--編碼方式UTF-8--> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <build> <plugins> <!--編譯Compile,使用JDK1.7開發--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.3</version> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> <!--打包時跳過測試--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.18.1</version> <configuration> <skipTests>true</skipTests> </configuration> </plugin> <!--Tomcat--> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <path>/${project.artifactId}</path> <!--最後面不能多個/,不能寫成/${project.artifactId}/,否則會找不到--> </configuration> </plugin> </plugins> </build> </project>
編寫web應用
新建servlet,因為是servlet3.0所以可以用註解
@WebServlet("/hello") public class HelloServlet extends HttpServlet{ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String currentTime = dateFormat.format(new Date()); req.setAttribute("currentTime",currentTime); req.getRequestDispatcher("/WEB-INF/JSP/Hello.jsp").forward(req,resp); /*註意url大小寫,不要寫錯,否則找不到,比較坑*/ } }
新建jsp,這裏新建在WEB-INF下面,WEB-INF下面的資源是不可以直接請求的
IDEA中配置Tomcat
點擊Edit Configuration,點擊左上角+號,選擇Tomcat Server->Local。
輸入Name,取消勾選After launch選項(為了不自動打開界面)。
點擊Application server右側的Configuration,配置Tomcat環境。
切換到Deploment選項卡,點擊+(alt+insert),選擇Artifact選項,彈出Select Artifact to Deploy對話框,選擇chapter1:war exploded,在Application contect中輸入/chapter1。
在server選項卡中,在On frame deactivation下拉框中選擇“Update resources”選項。這樣配置可以進行熱部署(自動部署),只需使用Ctrl+F9鍵手工編譯即可,註意此種方式熱部署要使用Debug模式。不可進行熱部署情況:修改了類名、方法名、成員變量。
Tomcat插件Debug模式
選擇Edit Configurations添加Maven,輸入Name和Command line。然後選擇這個配置,以Debug模式運行。即可實現tomcat插件熱部署。
Git倉庫
新建.gitignore文件,用來忽略不不需要放入到Git中的文件,例如Maven的target目錄、IDEA/Eclipse的工程文件。文件內容如下
# Maven #
target/
# IDEA #
.idea/
.iml
# Eclipse #
.settings/
.metadata/
.classpath
.project
Servers/
windows系統下新建.gitignore文件是不允許的,創建方法如下。
用git bash
cd 到 相應目錄 touch .gitignore
用dos 命令 ren
在相應目錄先建一個t.txt文件,然後切換到cmd命令行模式裏,ren t.txt .gitignore
本地Git倉庫
VCS->Import into Version Control->Create Git Repository,選擇項目文件夾,創建本地倉庫。如果報錯,要先安裝Git,並在Setting->Version Control->Git中配置Path to Git executable為Git安裝Path/bin/git.exe。
右擊項目Git->add,可以將.gitignore文件中忽略的所有文件添加到本地Git倉庫(快捷鍵Ctrl+Alt+A)。
然後再Git->Commit Directory提交到本地倉庫(快捷鍵Ctrl+K)。
提示:提交代碼時,建議勾選Optimize imports,可以優化import語句,去掉沒有使用的包。
遠程Git倉庫
可以隨時將Git本地倉庫推送到遠程倉庫,只需在IDEA中的VCS菜單中點擊Git/Push即可。
建立本地倉庫與遠程倉庫之間的連接
在GitHub或者開源中國上新建一個項目
git remote add origin <Git倉庫地址> git push -u origin master
git remote add origin <Git倉庫地址> 意思是添加遠程倉庫地址命名為orign
git push -u origin master 將本地的master分支推送到origin主機,同時指定origin為默認主機
第一次git push -u origin master報錯,意思是遠程倉庫的內容沒有本地化,執行下git pull origin master更新一下
再一次push依然報錯,這次是報當前分支落後於遠程倉庫當前版本,因為是第一次push,所以直接強行push執行git push -u origin master -f,但是在開發中不可取。
項目源碼
架構探險筆記1