当前位置: 移动技术网 > IT编程>开发语言>Java > 使用maven一步一步构建spring mvc项目(图文详解)

使用maven一步一步构建spring mvc项目(图文详解)

2019年07月19日  | 移动技术网IT编程  | 我要评论

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文件。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网