使用Intelij idea进行Web开发

intelij idea出名的好用,可是免费版的不带web插件。 可是能够用maven解决问题.
前奏:
1)安装好idea,maven,新建一个maven项目,选择一个archetype webapp(就是webap的代码模板).
2)点击执行按钮旁边的Edit Configuration->点击+号添加maven命令,在command line中写入命令:clean test (就是让maven执行下载jar包,编译代码和运行单元测试的任务),修改name为clean,
再添加一个maven命令,command line写入install cargo:run。就是要maven进行打包并部署到web容器,而后启动web容器。这个命令给个name叫runhtml

1:maven添加web相关的jar包,将项目打包war包
编写pom.xml,
添加javaweb方面jar的引用,也就是jsp,servlet的jar包java

...git

<packaging>war</packaging>

...
<dependencies>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
点击执行按钮,maven开始执行clean命令,会下载这些依赖的jar包。下载成功后在pom.xml文件上点右键->[maven]->[reimport],这样让idea知道引用了这些jar包,就能够提供代码提示纠错等功能
2:用maven的插件cargo将项目打包到web服务器
能够在项目跟目录运行命令:
1)mvn clean verify org.codehaus.cargo:cargo-maven2-plugin:run
这样能够直接安装cargo并在项目target目录下安装jetty的web容器
2)mvn clean verify org.codehaus.cargo:cargo-maven2-plugin:run
-Dcargo.maven.containerId=tomcat7x
-Dcargo.maven.containerUrl=http://repo1.maven.org/maven2/org/apache/tomcat/tomcat/7.0.68/tomcat-7.0.68.zip
这样能够安装tomcat的web容器
3)成功安装cargo和web容器后,能够开始配置项目使用哪一个web容器
<build>
...
<plugins>
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<!-- 配置使用tomcat7的web容器-->
<configuration>
<container>
<containerId>tomcat7x</containerId>
<home>项目的路径/target/cargo/installs/tomcat-7.0.68/apache-tomcat-7.0.68</home>
</container>
<configuration>
<home>${project.build.directory}/tomcat7x</home>
</configuration>
</configuration>
</plugin>
</plugins>
</build>
而后选择run命令,点击执行按钮,让maven下载安装插件和容器,并启动web容器
打开浏览器,输入localhost:8080/webappname,看web服务器能不能启动,若是一切ok就能够开心的用idea进行web开发了。github

相关网站:
cargo指南:https://codehaus-cargo.github.io/cargo/Maven2+plugin.html
Maven查看jar包依赖:http://mvnrepository.com/web

相关文章
相关标签/搜索