使用maven命令创建web项目

1.创建项目

mvn archetype:generate -DgroupId=cn.dinfo.boc.bocsearch  -DartifactId=bocsearch -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

2.项目的目录结构:

 

.|____bocsearch
||____pom.xml
||____src
|||____main
||||____resources
||||____webapp
|||||____index.jsp
|||||____WEB-INF
||||||____web.xml

3.修改配置文件:

   1.修改pom.xml:

<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.0http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>cn.dinfo.boc.bocsearch</groupId>
  <artifactId>bocsearch</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>bocsearch Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <properties>
  <jdk.version>1.7</jdk.version>
  <spring.version>4.1.1.RELEASE</spring.version>
  <jstl.version>1.2</jstl.version>
  <junit.version>4.11</junit.version>
  <logback.version>1.0.13</logback.version>
  <jcl-over-slf4j.version>1.7.5</jcl-over-slf4j.version>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><!-- 解决打包中文乱码 -->
 </properties>
  <dependencies>
  <!-- Unit Test -->
  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>${junit.version}</version>
  </dependency>

  <!-- Spring Core -->
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-core</artifactId>
   <version>${spring.version}</version>
   <exclusions>
    <exclusion>
     <groupId>commons-logging</groupId>
     <artifactId>commons-logging</artifactId>
    </exclusion>
   </exclusions>
  </dependency>

  <dependency>
   <groupId>org.slf4j</groupId>
   <artifactId>jcl-over-slf4j</artifactId>
   <version>${jcl-over-slf4j.version}</version>
  </dependency>
  <dependency>
   <groupId>ch.qos.logback</groupId>
   <artifactId>logback-classic</artifactId>
   <version>${logback.version}</version>
  </dependency>

  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-web</artifactId>
   <version>${spring.version}</version>
  </dependency>

  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-webmvc</artifactId>
   <version>${spring.version}</version>
  </dependency>
  <!-- jstl -->
  <dependency>
   <groupId>jstl</groupId>
   <artifactId>jstl</artifactId>
   <version>${jstl.version}</version>
  </dependency>
    <!-- Servlet -->
  <dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>jstl</artifactId>
   <version>1.2</version>
  </dependency>
  <dependency>
   <groupId>javax.el</groupId>
   <artifactId>el-api</artifactId>
   <version>1.0</version>
   <scope>provided</scope>
  </dependency>
  <dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>servlet-api</artifactId>
   <version>2.5</version>
   <scope>provided</scope>
  </dependency>
  <dependency>
   <groupId>javax.servlet.jsp</groupId>
   <artifactId>jsp-api</artifactId>
   <version>2.1</version>
   <scope>provided</scope>
  </dependency>
  
  
  
  
 </dependencies>
  <build>
  <finalName>BocWebApp</finalName><!-- 设置war包的名称 -->

  <plugins>
      <!-- Eclipse project -->
    <plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-eclipse-plugin</artifactId>
   <version>2.9</version>
   <configuration>
           <!-- Always download and attach dependencies source code -->
    <downloadSources>true</downloadSources>
    <downloadJavadocs>false</downloadJavadocs>
    <!-- Avoid type mvn eclipse:eclipse -Dwtpversion=2.0 -->
    <wtpversion>2.0</wtpversion>
   </configuration>
    </plugin>

    <!-- Set JDK Compiler Level -->
    <plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-compiler-plugin</artifactId>
   <version>2.3.2</version>
   <configuration>
    <source>${jdk.version}</source>
    <target>${jdk.version}</target>
   </configuration>
    </plugin>

    <!-- For Maven Tomcat Plugin -->
    <plugin>
   <groupId>org.apache.tomcat.maven</groupId>
   <artifactId>tomcat7-maven-plugin</artifactId>
   <version>2.2</version>
   <configuration>
    <path>/CounterWebApp2</path>
   </configuration>
    </plugin>
  </plugins>
 </build>
</project>

 

2.修改web.xml

<?xml version="1.0" encoding="UTF-8"?>
 <web-app 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_2_5.xsd"
 version="2.5">

<display-name>Boc Web Application</display-name>

 <servlet>
  <servlet-name>mvc-dispatcher</servlet-name>
  <servlet-class>
                        org.springframework.web.servlet.DispatcherServlet
                </servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>

 <servlet-mapping>
  <servlet-name>mvc-dispatcher</servlet-name>
  <url-pattern>/</url-pattern>
 </servlet-mapping>

 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
 </context-param>

 <listener>
  <listener-class>
                   org.springframework.web.context.ContextLoaderListener
                </listener-class>
 </listener>
</web-app>

 3. eclipse ide支持:进入bocsearch所在目录执行以下命令:

     mvn eclipse:eclipse -Dwtpversion=2.0

注意,此选项 -Dwtpversion=2.0 告诉 Maven 将项目转换到 Eclipse 的 Web 项目(WAR),而不是默认的Java项目(JAR)。为方便起见,以后我们会告诉你如何配置 pom.xml 中的这个 WTP 选项。

3.2 导入到 Eclipse IDE – File -> Import… -> General -> Existing Projects into workspace.将项目导入到eclipse中

4.修改pom配置文件:

 

<plugins>
		    <!-- Eclipse project -->
		  <plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-eclipse-plugin</artifactId>
			<version>2.9</version>
			<configuration>
			        <!-- Always download and attach dependencies source code -->
				<downloadSources>true</downloadSources>
				<downloadJavadocs>false</downloadJavadocs>
				<!-- Avoid type mvn eclipse:eclipse -Dwtpversion=2.0 -->
				<wtpversion>2.0</wtpversion>
			</configuration>
		  </plugin>

注意,为方便起见,声明 maven-eclipse-plugin,并配置wtpversion 来避免输入参数 -Dwtpversion=2.0。现在,每次使用 mvn eclipse:eclipse,Maven将这个项目导入转换为 Eclipse Web 项目。

 

#之前 
mvn eclipse:eclipse --> Eclipse Java project (JAR)
mvn eclipse:eclipse -Dwtpversion=2.0 --> Eclipse Java web project (WAR)

#之后
mvn eclipse:eclipse --> Eclipse Java web project (WAR)

 

5.更新代码:

在这一步中,在上一步配置完pom.xml后,重新执行 mvn eclipse:eclipse 这个命令,我们将创建Spring MVC的一些文件和logback日志框架的文件夹。


注意,如果它不存在,需要手动创建文件夹。

/src/main/java/cn.dinfo.boc.bocsearch.controller/LoginController.java

@Controller
public class LoginController {
 private static int counter = 0;
 private static final String VIEW_INDEX = "result";
 private final static org.slf4j.Logger logger = LoggerFactory.getLogger(LoginController.class);

 @RequestMapping(value = "/login", method = RequestMethod.GET)
 public String welcome(ModelMap model) {

  model.addAttribute("message", "Welcome ljf");
  model.addAttribute("counter", ++counter);
  logger.debug("[welcome] counter : {}", counter);

  // Spring uses InternalResourceViewResolver and return back index.jsp
  return VIEW_INDEX;

 }
}

 

5.2 创建Spring配置文件。

/src/main/webapp/WEB-INF/mvc-dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="
        http://www.springframework.org/schema/beans    
        http://www.springframework.org/schema/beans/spring-beans.xsd         http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

 <context:component-scan base-package="cn.dinfo.boc.bocsearch.controller" />
 <bean
  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix">
   <value>/WEB-INF/pages/</value>
  </property>
  <property name="suffix">
   <value>.jsp</value>
  </property>
 </bean>

</beans>

5.3 更新让现有的 web.xml 支持 Servlet 2.5(默认的Servlet2.3 太旧了), 并且还通过 Spring 监听器 ContextLoaderListener 集成了Spring框架。

/src/main/webapp/WEB-INF/web.xml

 

 

<web-app 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_2_5.xsd"
	version="2.5">

	<display-name>Counter Web Application</display-name>

	<servlet>
		<servlet-name>mvc-dispatcher</servlet-name>
		<servlet-class>
                        org.springframework.web.servlet.DispatcherServlet
                </servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>mvc-dispatcher</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
	</context-param>

	<listener>
		<listener-class>
                   org.springframework.web.context.ContextLoaderListener
                </listener-class>
	</listener>
</web-app>

 

5.4 移动文件 result.jsp 到 WEB-INF/pages 目录下, 为了保护直接访问。并更新内容:

5.5 在资源文件夹(resources)中创建 logback.xml 文件(将下面的语句复制到空的xml文件命名logback.xml)启动报错,之后删除了此文件

/src/main/resources/logback.xml

 

<?xml version="1.0" encoding="UTF-8"?> 

6.打包运行

 1.

6.1 要编译,测试和项目打包成一个WAR文件,输入:

 

mvn package

 

一个新的 WAR 文件将在 project/target/BocWebApp.war产生,只需复制并部署到Tomcat 发布的目录。

 

2.

如果想通过 Eclipse 服务器这个项目插件(Tomcat 或其它容器)调试,这里再输入:

 

mvn eclipse:eclipse

如果一切顺利,该项目的依赖将被装配附加到 Web部署项目。图片: 右键点击 project -> Properties -> Deployment Assembly

6.3 Maven 的 Tomcat 插件声明(加入到 pom.xml):

pom.xml

 

<!-- For Maven Tomcat Plugin --> 

  <!-- For Maven Tomcat Plugin -->
    <plugin>
   <groupId>org.apache.tomcat.maven</groupId>
   <artifactId>tomcat7-maven-plugin</artifactId>
   <version>2.2</version>
   <configuration>
    <path>/CounterWebApp</path>
   </configuration>
    </plugin>

输入命令:

 

mvn tomcat:run

 

13:59:18.538 [main] DEBUG o.s.c.e.PropertySourcesPropertyResolver - Searching for key 'spring.liveBeansView.mbeanDomain' in [systemEnvironment]
13:59:18.541 [main] DEBUG o.s.c.e.PropertySourcesPropertyResolver - Could not find key 'spring.liveBeansView.mbeanDomain' in any property source.
13:59:18.543 [main] DEBUG o.s.web.servlet.DispatcherServlet - Published WebApplicationContext of servlet 'mvc-dispatcher' as ServletContext attrib
13:59:18.545 [main] INFO  o.s.web.servlet.DispatcherServlet - FrameworkServlet 'mvc-dispatcher': initialization completed in 797 ms
13:59:18.548 [main] DEBUG o.s.web.servlet.DispatcherServlet - Servlet 'mvc-dispatcher' configured successfully
三月 11, 2016 1:59:18 下午 org.apache.coyote.http11.Http11Protocol init
INFO: Initializing Coyote HTTP/1.1 on http-8080
三月 11, 2016 1:59:18 下午 org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on http-8080

这将启动Tomcat,部署项目默认在端口8080。

详情参考网址:http://www.yiibai.com/maven/maven_environment_setup.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值