eclipse的maven 搭建spring mvc

eclipse的maven 搭建spring mvc
1.構建maven工程,new–>other -->maven project --> 勾選 create a simple project -->填寫項目信息 (packaging 選擇war )
在這裏插入圖片描述

在這裏插入圖片描述

在這裏插入圖片描述
2. 等待項目的搭建。
3.pom.xml 報錯,修改 pom.xml 。
在 中 添加

<build>
    <plugins> 	
    <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.6</version>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>
 	<plugin> 
			<groupId>org.apache.maven.plugins</groupId> 
			<artifactId>maven-compiler-plugin</artifactId> 
			<version>2.3.2</version> 
			<configuration> 
			<source>1.7</source> 
			<target>1.7</target> 
			<encoding>UTF-8</encoding> 
			</configuration> 
	</plugin> 
	 </plugins> 	
    </build>
  1. java resource 報錯,點開子菜單,無錯。
    4.1 build path --> configure build path ,刪除jre 運行環境,添加新jre ,Java 6 及以上
    在這裏插入圖片描述

右擊項目,選擇propertie。
4.2 java compiler 選擇1.7 。(1.6 不知道是否滿足,可以嘗試)
在出現的內容中,
Java 1.5 —> 1.7
dynamic web module 選擇 3.0 ,
右側的 runtime 頁,勾選 tomcat
在這裏插入圖片描述
在這裏插入圖片描述

4.3 右擊項目
maven --update project
(此時 紅叉 應該消失了)
5. src/main/webapp 下 新建 WEB-INF文件夾以及 在WEB-INF 下新建 web.xml

在這裏插入圖片描述
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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

<!--configure the setting of springmvcDispatcherServlet and configure the mapping-->
  <servlet>
      <servlet-name>springmvc</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/springmvc-servlet.xml</param-value>
        </init-param>
        <!-- <load-on-startup>1</load-on-startup> -->
  </servlet>

  <servlet-mapping>
      <servlet-name>springmvc</servlet-name>
      <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

注意 /WEB-INF/springmvc-servlet.xml
6. 在web-inf 下新建 springmvc-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:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="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.1.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">                    

    <!-- scan the package and the sub package -->
    <context:component-scan base-package="spring"/>

    <!-- don't handle the static resource -->
    <mvc:default-servlet-handler />

    <!-- if you use annotation you must configure following setting -->
    <mvc:annotation-driven />

    <!-- configure the InternalResourceViewResolver -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
            id="internalResourceViewResolver">
        <!-- 前綴 -->
        <property name="prefix" value="/WEB-INF/jsp/" />
        <!-- 後綴 -->
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

注意 <context:component-scan base-package=“spring”/> 是你的代碼內容包

  1. 在 src/main/java 下建包,建 java 類,Test
package spring;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/mvc")
public class Test {

    @RequestMapping("/hello")
    public String hello(){        
        return "hello";
    }
}
  1. 在 WEB-INF 下新建 jsp 文件夾,以及在該文件下新建 hello.jsp (文件名 與 Test 類方法的返回值 一致。)
  2. tomcat 添加項目。 啓動tomcat ,訪問http://localhost:8080/springmvc/mvc/hello
    (springmvc 是項目名,mvc 是Test類映射 /hello 是hello方法映射 ) 可以正常顯示 hello.jsp 的內容。
    至此 完成 demo。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章