1使用eclipse构建maven web项目
1.1新建Maven的web项目
打开菜单File –New-MavenProject。

点击Next

选择模板类型archtype——maven-archtype-webapp。然后点击Next。

输入Group Id和artifact Id。Group Id一般填入项目名称,Artifact Id一般填入子项目的名称。

生成的项目文件结构如下所示:

选择pom.xml文件,并打开,界面如下所示:

增加Properties:展开Properties选项,然后点击Create…按钮,如下所示:然后Name字段填入springVersion,Value字段填入3.2.5.RELEASE。即在pom.xml中增加了一个属性springVersion,属性值为3.2.5.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}是之前设置的属性。
依赖库设定完之后,如果本地不存在还需要从网络上下载相应的依赖库,选中pom.xml文件,右击鼠标选中Run AS – maven install,然后系统自动从网络上下载相应的依赖库。

依赖库下载完之后,可以在目录JavaResources – Liraries – Maven Dependencies中看到相应的库文件,如下图所示:

在src – main目录下新建文件夹Java。

在java中新建类Hello.java。包名为com.springmvc.controller。

Hello.java中的内容如下:
package com.springmvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class Hello {
@RequestMapping(value="/Hello")
public String HelloWorld(Model model){
model.addAttribute("message","Hello World!!!");
return "HelloWorld";
}
}
在src – main –webapp – WEB-INF目录下新建文件夹view,并新建文件HelloWorld.jsp。

Helloworld.jsp文件内容如下所示:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>message:${message}</h1>
</body>
</html>
选中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">
<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
在src – main –webapp – WEB-INF目录下新建文件spring-mvc-servlet.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.springmvc.controller" />
<bean id="viewResolver"
class="orgspringframeworkwebservletviewInternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
Ok,所有文件已经建立完毕,现在可以运行该项目,看一下效果如何了,选中该项目(点击com.liuht.springmvc,即该项目的最顶层),点击Run As – Run on Server。

出现一个界面,让你选中要使用的Web服务器,有两个选项,一个是已存在的服务器,另一个是重新定一个新的服务器,我选择已存在服务器,如果你没有,可以重新建立一个web服务器。

选中要运行的项目,点击Add>按钮,添加到右边的选择框中,如果右边有其他不需要的项目,可以选中,并点击< Remove按钮删除。配置完成之后,点击Finish按钮。

在Console窗口看到如下内容,说明项目启动成功:

Eclipse自动打开自己的浏览器,并显示如下内容:

你也可以打开浏览器输入http://localhost:8080/com.liuht.springmvc/

出现这个界面说明项目已经成功了,Hello World!这串字符来自控制器Hello.java文件。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持社区。
|