Eclipse應用maven搭建spring mvc圖文教程。本站提示廣大學習愛好者:(Eclipse應用maven搭建spring mvc圖文教程)文章只能為提供參考,不一定能成為您想要的結果。以下是Eclipse應用maven搭建spring mvc圖文教程正文
本文為年夜家引見了Eclipse應用maven搭建spring mvc的具體步調,供年夜家參考,詳細內容以下
1、 情況設置裝備擺設
a). Java 1.7
b). Eclipse luna
c). Maven3.2.5
d). Spring 4.1.4
2、 創立maven工程
a). 翻開eclipse,file->new->project->Maven->Maven Project
b). 下一步
c). 選擇創立的工程為webapp,下一步
d). 填寫項目標group id和artifact id。普通情形下,group id寫域名的倒序,artifact id寫項目稱號便可。最初點完成。
e). 最後建好後,項目目次構造以下
f). 普通的項目目次中,在java Resources目次下,還有src/main/java,src/main/test/java,src/main/test/resources這三個source folder,須要手動創立。鄙人面的步調中會講到若何補齊這三個目次。
3、 修正項目根本設置
a). 右鍵此項目稱號->Properties->Java Build path,點擊source標簽。
b). 提醒 hello/src/main/java (missing)和hello/src/test/java (missing)。普通的項目目次中,在java Resources目次下,還會有src/main/test/resources這個source folder。將missing的先刪除,再從新創立,缺乏的直接創立。點右鍵操作按鍵停止刪除和添加。
c). 修正完全,後果以下圖
d). 接上去再修正libraries的設置裝備擺設,jre應用1.7版本。選中JRE System Library->edit ,改換版本。
e). 再修正一下order and export裡的設置裝備擺設,重要是調劑這四個目次的顯示次序,調為本身愛好的次序便可
f). 接上去再修正project facets,先將java修正為1.7。
Dynamic Web Module沒法在這裡直接修正為3.0,須要翻開工程目次下有一個.settings文件夾,翻開org.eclipse.wst.common.project.facet.core.xml,做以下修正:
<installed facet="jst.web" version="3.0"/>
重啟eclipe便可以看到更改失效了。
4、 Eclipse中maven的設置裝備擺設
a). window->properties->maven,勾選 download repository index updates on startup
5、 簡略Spring mvc的設置裝備擺設
a). 翻開項目中的pom.xml文件,並點擊Dependencies標簽,點擊add添加新的依附
b). 假如曉得依附的group id和artifact id,可以直接填寫,假如不清晰,可以輸出症結字停止查詢,或是到http://search.maven.org網站查詢
c). 須要添加的依附有:spring-webmvc,版本為4.1.4. RELEASE。完全的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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.springstudy</groupId>
<artifactId>study</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>study Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<spring.version>4.1.4.RELEASE</spring.version>
</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-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
<build>
<finalName>study</finalName>
</build>
</project>
d). 翻開src/main/webapp/WEB-INF/web.xml文件,終究修正如以下內容:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="study" version="2.5">
<display-name>Archetype Created Web Application</display-name>
<description>sprintMVC情況搭建</description>
<!-- 加載Spring設置裝備擺設文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/configs/spring-*.xml</param-value>
</context-param>
<!-- Spring監聽 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Spring MVC設置裝備擺設 -->
<servlet>
<servlet-name>Dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 自界說spring mvc的設置裝備擺設文件稱號和途徑 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:configs/spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- spring mvc 要求後綴 -->
<servlet-mapping>
<servlet-name>Dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
e). 在Java Resources/scr/main/resources目次下,創立configs文件夾,以便寄存在web.xml中聲明的設置裝備擺設途徑
f). 在Java Resources/scr/main/resources/configs目次下,創立spring-servlet.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:jee="http://www.springframework.org/schema/jee"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.1.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.springstudy.controller" />
<mvc:annotation-driven />
<mvc:resources mapping="/styles/**" location="/styles/" />
<mvc:resources mapping="/scripts/**" location="/scripts/" />
<mvc:resources mapping="/images/**" location="/images/" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
g). 創立controller包,在spring-servlet.xml文件中,<context:component-scan base-package="com.springstudy.controller" />曾經指定了途徑
h). 在src/main/webapp/WEB-INF目次下,創立views文件,在spring-servlet.xml文件中,<property name="prefix" value="/WEB-INF/views/" />已指定了視圖文件途徑
i). 創立第一個controller文件HelloController.java,完全的文件內容以下:
package com.springstudy.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HelloController {
@RequestMapping("/hello")
public ModelAndView hello(){
ModelAndView mv =new ModelAndView();
mv.addObject("spring", "spring mvc");
mv.setViewName("hello");
return mv;
}
}
j). 添加src/main/webapp/WEB-INF/views/hello.jsp文件,內容以下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>sprint hello</title>
</head>
<body>hello ${spring}!
</body>
</html>
6、將項目宣布到tomcat
a). 在eclipse中添加tomcat 7;
b). 在tomcat添加完成後,雙擊,設置overview選項卡中Server Locations的設置;
i. 將 Use Tomcat installation(takes control of Tomcat installation)選中
ii. 將Deploy path的內容改成:webapps
iii. 保留
c). 右鍵tomcat,Add and Remove… ,添加study
d). 啟動tomcat;
e). 閱讀器翻開http://localhost:8080/study/hello,拜訪勝利!以下圖:
操作停止!
以上就是Eclipse應用maven搭建spring mvc的全體內容,願望能給年夜家一個參考,也願望年夜家多多支撐。