久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

使用Maven創(chuàng)建Web項(xiàng)目

 bylele 2016-03-08

使用Maven創(chuàng)建Web項(xiàng)目

碧遠(yuǎn)|bystander 10189 閱讀

本文通過Maven完成一個(gè)簡(jiǎn)單的Web項(xiàng)目(注意,,Spring配置不是重點(diǎn),,看看就行)

1.從Maven模板創(chuàng)建Web應(yīng)用程序

命令格式如下:

mvn archetype:generate -DgroupId={project-packaging} -DartifactId={project-name} -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

這就告訴Maven從maven-archetype-webapp 這個(gè)模板來創(chuàng)建

友情提示:是不是太難記了..好吧,,直接輸入

mvn archetype:generate

根據(jù)向?qū)韯?chuàng)建把。,。

比如我這樣寫:

> mvn archetype:generate -DgroupId=com.mkyong -DartifactId=CounterWebApp -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] Generating project in Batch mode
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-webapp:1.0
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.mkyong
[INFO] Parameter: packageName, Value: com.mkyong
[INFO] Parameter: package, Value: com.mkyong
[INFO] Parameter: artifactId, Value: CounterWebApp
[INFO] Parameter: basedir, Value: /Users/mkyong/Documents/workspace
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: /Users/mkyong/Documents/workspace/CounterWebApp
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.147s
[INFO] Finished at: Thu Dec 20 20:35:19 MYT 2012
[INFO] Final Memory: 12M/128M
[INFO] ------------------------------------------------------------------------

就創(chuàng)建了一個(gè)包名為com.mkyong,類名為CounterWebApp的項(xiàng)目了

2.Maven的Web程序目錄結(jié)構(gòu)

標(biāo)準(zhǔn)的web.xml部署描述文件生成了

CounterWebApp
   |-src
   |---main
   |-----resources
   |-----webapp
   |-------index.jsp
   |-------WEB-INF
   |---------web.xml
   |-pom.xml

對(duì)結(jié)構(gòu)有疑問的去這里

生成的pom.xml文件如下

<project xmlns="http://maven./POM/4.0.0" 
	xmlns:xsi="http://www./2001/XMLSchema-instance"
  	xsi:schemaLocation="http://maven./POM/4.0.0 
	http://maven./maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mkyong</groupId>
  <artifactId>CounterWebApp</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>CounterWebApp Maven Webapp</name>
  <url>http://maven.</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>CounterWebApp</finalName>
  </build>
</project>

沒啥疑問的,。web.xml文件則如下:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java./dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
</web-app>

index.jsp內(nèi)容是:

<html>
<body>
<h2>Hello World!</h2>
</body>
</html>

 3.轉(zhuǎn)換以支持Eclipse

為了使Maven的web項(xiàng)目支持Eclipse,在命令行下,,cd到CounterWebApp目錄下,,然后輸入這個(gè)命令

mvn eclipse:eclipse -Dwtpversion=2.0

-Dwtpversion=2.0參數(shù)是必需的,這個(gè)參數(shù)指明了是web項(xiàng)目,,然后導(dǎo)入到eclipse里就行了,。從導(dǎo)入的工程ico可以看出是個(gè)web項(xiàng)目

圖一

eclipse-tomcat-server final-folder project-structure

不要迷惑,如果我們只輸入

mvn eclipse:eclipse

會(huì)轉(zhuǎn)換成java項(xiàng)目,,而不是web項(xiàng)目的,。

好了,現(xiàn)在項(xiàng)目已經(jīng)準(zhǔn)備好部署了,,eclipse里的tomcat server插件裝好,,然后啟動(dòng)項(xiàng)目

圖二

eclipse-tomcat-server

可以通過http://localhost:8080/CounterWebApp/地址來訪問

4.更新pom文件

為了使Maven項(xiàng)目支持Spring MVC框架,我們需要修改pom.xml文件

1.添加編譯插件指定jdk6來編譯

2.添加Spring框架依賴

3.升級(jí)JUnit到4.1.1

<project xmlns="http://maven./POM/4.0.0" 
	xmlns:xsi="http://www./2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven./POM/4.0.0 
	http://maven./maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.mkyong</groupId>
	<artifactId>CounterWebApp</artifactId>
	<packaging>war</packaging>
	<version>1.0-SNAPSHOT</version>
	<name>CounterWebApp Maven Webapp</name>
	<url>http://maven.</url>

	<properties>
		<spring.version>3.0.5.RELEASE</spring.version>
		<junit.version>4.11</junit.version>
		<jdk.version>1.6</jdk.version>
	</properties>

	<dependencies>

		<!-- Spring 3 dependencies -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${spring.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>

		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>${junit.version}</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
	<build>
		<finalName>CounterWebApp</finalName>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.0</version>
				<configuration>
					<source>${jdk.version}</source>
					<target>${jdk.version}</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

 5.Spring MVC 松耦合

創(chuàng)建Spring MVC控制器類,,有兩個(gè)方法,,打印信息

package com.mkyong.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/")
public class BaseController {

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

		model.addAttribute("message", "Maven Web Project + Spring 3 MVC - welcome()");

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

	}

	@RequestMapping(value="/welcome/{name}", method = RequestMethod.GET)
	public String welcomeName(@PathVariable String name, ModelMap model) {

		model.addAttribute("message", "Maven Web Project + Spring 3 MVC - " + name);
		return "index";

	}

}

然后,創(chuàng)建Spring配置文件,,定義視圖解析器

<beans xmlns="http://www./schema/beans"
	xmlns:context="http://www./schema/context"
	xmlns:xsi="http://www./2001/XMLSchema-instance"
	xsi:schemaLocation="

http://www./schema/beans


http://www./schema/beans/spring-beans-3.0.xsd


http://www./schema/context

        http://www./schema/context/spring-context-3.0.xsd">

	<context:component-scan base-package="com.mkyong.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>

然后更新一下web.xml文件,,通過Spring的監(jiān)聽器ContextLoaderListener把Spring框架集成進(jìn)去

<web-app xmlns="http://java./xml/ns/javaee"
	      xmlns:xsi="http://www./2001/XMLSchema-instance"
	      xsi:schemaLocation="http://java./xml/ns/javaee 
	      http://java./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>

然移動(dòng)index.jsp到WEB-INF目錄里,以防用戶直接訪問他,,同時(shí)我們編輯,,增加${message}變量,也就是controller控制器傳過來的值到頁(yè)面

最終的頁(yè)面結(jié)構(gòu)是:

圖三

final-folder

6.Eclipse+Tomcat

為了通過Eclipse的Tomcat插件開始/調(diào)試項(xiàng)目,,需要再執(zhí)行一次下面的命令,,使得所有的依賴附加完成

mvn eclipse:eclipse -Dwtpversion=2.0

在沒執(zhí)行以前,項(xiàng)目依賴是空的

圖四

web-deployment-assembly

執(zhí)行完以后,,就都有了

圖五

web-deployment-assembly-2

重要,!許多人就是沒有第二次執(zhí)行這個(gè)命令導(dǎo)致無(wú)法開始/調(diào)試,如果有問題,,右擊你的項(xiàng)目屬性,,看看依賴是否為空

7.Maven打包

<project ...>
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.mkyong</groupId>
	<artifactId>CounterWebApp</artifactId>

	<packaging>war</packaging>
	<version>1.0-SNAPSHOT</version>

web項(xiàng)目打包成war。放在target目錄

進(jìn)入項(xiàng)目目錄,,執(zhí)行一下就可以了

mvn package

然后把target目錄的CountWebApp.war文件拷貝,,并部署到你的web容器里就行了

8.Demo演示

打開http://localhost:8080/CounterWebApp/welcome

可以看到

圖六

demo1

我們傳個(gè)參數(shù)進(jìn)去

http://localhost:8080/CounterWebApp/welcome/mkyong

就可以看到效果了

圖七

demo2

9.Demo下載

CounterWebApp.zip

作者:碧遠(yuǎn)|bystander
尋找窄門
原文地址:使用Maven創(chuàng)建Web項(xiàng)目, 感謝原作者分享。

發(fā)表評(píng)論

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,,不代表本站觀點(diǎn),。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,,謹(jǐn)防詐騙,。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào),。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多