33、(知识篇)Maven的使用1 (Eclipse下Maven创建Web项目)

搞了一天Maven,发觉在网络差的情况底下会直接被搞死,下载jar包会损坏带来各种问题,

而且在网上找不到好的方法,暂时,只能建议用好的网络使用maven,同时,如果有网友见到此文

有较好的处理网络环境差情况下Maven使用的方法,麻烦分享留言一下,万分感谢~


本文主要讲述 Maven 在eclipse环境下创建web项目为例,建立SpringMVC项目。

希望大家花5分钟看下,一定能实现!



Maven的使用方法:

1、到官网下载Maven:http://maven.apache.org/

2、配置环境变量

2.1 M2_HOME: G:\JAVA\m2\apache-maven-3.3.9 (你的maven解压的地址:)

2.2 Path: %JAVA_HOME%\bin;

3、指定本地仓库:G:\JAVA\m2\apache-maven-3.3.9\conf\settings.xml

找到<localRepository>G:\JAVA\m2\repository</localRepository> 指定到你的仓库




4、指定maven使用的默认jdk版本:

在setting.xml找到profiles节点,加入如下代码(其中1.8为我的jdk版本,按照实际版本加入即可):

	<profile>  
		<id>jdk-1.8</id>  
		<activation>  
			<activeByDefault>true</activeByDefault>  
			<jdk>1.8</jdk>  
		</activation>  
		<properties>  
			<maven.compiler.source>1.8</maven.compiler.source>  
			<maven.compiler.target>1.8</maven.compiler.target>  
			<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>  
		</properties>   
	</profile>



5、eclipse中配置Maven相关信息:(eclipse版本为4.6.2)

5.1 eclipse指定我们的Maven/仓库:

eclipse-> windows -> preference -> Maven -Installations

指定我们的仓库

在Maven->User Settings -> Global Settings中指定我们maven的settings.xml



6、建立Maven的Web项目

与网上的创建方法不同,要选中Create a simple project:




创建完成后,目录是这样的:


可以看到pom.xml会有红叉,原因是创建的maven project并不是真正的Dynamic Web Project目录。

然后我们需要右键配置项目相关信息:

找到Project Facets,把默认勾选的Dynamaic Web Module去掉,然后点击Apply:


然后再次勾选回来,


我们可以看到Futher configuration available..

点击它


上图,我们要把Content directory改成maven的目录结构 src/main/webapp

然后确定,web项目就创建好了!!!

**需要注意下,jsp使用到servlet-api.jar ,需要依赖这个包,jsp才不会报错!



现在我们在pom.xml中加入需要的jar的dependency就可以了

但是很不幸地,下载的时候,有时会导致jar损坏的,我研究了很久,找不到解决办法

然后我将jar都删掉,重下就好了,自学就是麻烦,单纯的在网上找,基本是千遍一律的没什么变化,

天下文章一大抄,解决不了问题




以下是配置的maven的SpringMVC项目

最终目录是这样的:

首先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.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.spring.test</groupId>
	<artifactId>33Spring10</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>

	<!-- 统一配置版本号 -->
	<properties>
		<servlet.version>3.1.0</servlet.version>
		<spring.version>4.3.5.RELEASE</spring.version>
	</properties>

	<dependencies>
		<!-- Spring用到的包 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>4.3.5.RELEASE</version>
		</dependency>

		<!-- servlet用到的api -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>${servlet.version}</version>
			<scope>provided</scope>
		</dependency>
	</dependencies>
</project>


web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>34Spring11</display-name>
  <!-- The front controller of this Spring Web application, responsible for handling all application requests -->
	<servlet>
		<servlet-name>springDispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:springmvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<!-- Map all requests to the DispatcherServlet for handling -->
	<servlet-mapping>
		<servlet-name>springDispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>


springmvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
		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-4.3.xsd">
	
	
	<context:component-scan base-package="com.spring"></context:component-scan>
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
	
	<mvc:default-servlet-handler/>
	<mvc:annotation-driven></mvc:annotation-driven>
	
</beans>


测试类:

package com.spring.test;

import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class TestController {
	
	@RequestMapping("test")
	public String test(Map<String,Object> params) {
		params.put("msg", "传值");
		return "index";
	}
}


index.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	HelloWorld!
	<br>
	${msg }
</body>
</html>


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值