一、配置好新建Maven项目后可更改Maven本地库的存放位置
setting_init.xml文件的内容为
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository>E:/maven/repository_init</localRepository>
<pluginGroups>
<pluginGroup>org.mortbay.jetty</pluginGroup>
</pluginGroups>
<proxies></proxies>
<servers>
<!--<server>
<id>Tomcat7</id>
<username>admin</username>
<password>admin</password>
</server>-->
</servers>
<mirrors></mirrors>
<profiles></profiles>
<activeProfiles>
<activeProfile>nexus</activeProfile>
</activeProfiles>
</settings>
二、添加SpringMvc相关jar包
选择pom.xml文件,并打开,界面如下所示:
增加Properties:展开Properties选项,然后点击Create…按钮,如下所示:然后Name字段填入springVersion,Value字段填入4.3.3.RELEASE。即在pom.xml中增加了一个属性springVersion,属性值为4.3.3.RELEASE。
选择Dependencies标签,打开Dependencies选项卡,并增加一个新的Dependency。
Group Id:org.springframework
Artifact Id:spring-web
Version:${springVersion}
点击ok按钮。
说明:该过程是加入springframe的spring-web依赖库,${springVersion}是之前设置的属性。
新建Dependency:
Group Id:org.springframework
Artifact Id:spring-webmvc
Version:${springVersion}
点击ok按钮。
说明:该过程是加入springframe的spring-webmvc依赖库,${springVersion}是之前设置的属性。
二、修改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_3_0.xsd"
version="3.0">
<!-- 配置springmvc DispatcherServlet -->
<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/ApplicationContext-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
三、新增ApplicationContext-mvc.xml文件
新增resource文件夹,并修改为源文件夹
新建spring文件夹和ApplicationContext-mvc.xml文件
ApplicationContext-mvc.xml文件内容为
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" 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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="com.winphone" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
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.winphone</groupId>
<artifactId>TestMaven</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>TestMaven</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<springVersion>4.3.3.RELEASE</springVersion>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${springVersion}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${springVersion}</version>
</dependency>
</dependencies>
<build>
<finalName>TestMaven</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source> <!-- 源代码使用的开发版本 -->
<target>1.7</target> <!-- 需要生成的目标class文件的编译版本 -->
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<uriEncoding>UTF-8</uriEncoding>
<port>8088</port>
<path>/TestMaven</path> <!-- 应用的部署位置 -->
</configuration>
</plugin>
</plugins>
</build>
</project>
五、新建控制文件HelloController
HelloController.java文件内容为:
package com.winphone.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloController {
@RequestMapping(value="/Hello")
public String HelloWorld(Model model){
model.addAttribute("message","Hello World1!!!");
return "HelloWorld";
}
}
六、新建文件夹view和页面文件HelloWorld.jsp
文件HelloWorld.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>
<h1>message:${message}</h1>
</body>
</html>
七、启动服务,输入网址:http://localhost:8088/TestMaven/Hello