使用Maven快速創建一個SpringMVC工程步驟


第一步:創建maven工程,加入SpringMVC的maven依賴:

 1 <dependency>
 2   <groupId>org.springframework</groupId>
 3   <artifactId>spring-webmvc</artifactId>
 4   <version>4.3.0.RELEASE</version>
 5 </dependency>
 6 
 7 <dependency>
 8   <groupId>javax.servlet</groupId>
 9   <artifactId>jstl</artifactId>
10   <version>1.2</version>
11 </dependency>
12 
13 <dependency>
14   <groupId>taglibs</groupId>
15   <artifactId>standard</artifactId>
16   <version>1.1.2</version>
17 </dependency>

第二步:在pom.xml文件中加入tomcat7的插件;

 <pluginManagement>
        <plugins>
        <!-- 配置tomcat插件 -->
            <plugin>
             <groupId>org.apache.tomcat.maven</groupId>
             <artifactId>tomcat7-maven-plugin</artifactId>
              <version>2.2</version>
            
                <configuration>
                    <path>/tomcat-demo</path>
                    <port>8080</port>
                    <uriEncoding>UTF-8</uriEncoding>
                    <url>http://localhost:8080/manager/text</url>
                    <server>tomcat7</server>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>

第三步:配置SpringMVC配置文件;

<?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:p="http://www.springframework.org/schema/p" 
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:util="http://www.springframework.org/schema/util"
  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/util
  http://www.springframework.org/schema/util/spring-util.xsd
  http://www.springframework.org/schema/context 
  http://www.springframework.org/schema/context/spring-context.xsd
  http://www.springframework.org/schema/mvc
  http://www.springframework.org/schema/mvc/spring-mvc.xsd" >
    <!-- 默認的注解映射的支持 -->  
    <mvc:annotation-driven/>
 
    <!-- 如果當前請求為“/”時,則轉發到“/helloworld/index” -->
    <mvc:view-controller path="/" view-name="forward:/helloworld/index"/> 
    <!-- 靜態資源映射 -->
    <mvc:resources mapping="/js/**" location="/WEB-INF/js/" />
    <mvc:resources mapping="/css/**" location="/WEB-INF/css/" />
    <mvc:resources mapping="/fonts/**" location="/WEB-INF/fonts/" />
    <mvc:resources mapping="/plugins/**" location="/WEB-INF/plugins/" />
    <mvc:resources mapping="images/**" location="/WEB-INF/images/" />
    <!-- 當上面要訪問的靜態資源不包括在上面的配置中時,則根據此配置來訪問 -->
    <mvc:default-servlet-handler/>
    <!-- 開啟controller注解支持 -->
    <!-- use-default-filters="false" 只掃描指定的注解 -->
    <context:component-scan base-package="com.zte.ems.controllers" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
      
    <!-- 配置視圖解析器,並指定視圖所在的文件夾 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
       <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
       <property name="contentType" value="text/html"/>        
       <property name="prefix" value="/WEB-INF/views/"/>
       <property name="suffix" value=".jsp"/>  
    </bean> 
</beans>

 第四步:配置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="taotao" version="2.5">
  <display-name>Archetype Created Web Application</display-name>
  <welcome-file-list>
      <welcome-file>
          index.jsp
      </welcome-file>
  </welcome-file-list>
   <servlet>
        <servlet-name>SpringMVCLesson</servlet-name>
        <!-- DispatcherServlet主要就是攔截請求,然后調用對應的Controller和Action -->
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <!-- classpath:springservlet-config.xml指定具體的配置文件為springservlet-config.xml -->
            <param-value>classpath:springservlet-config.xml</param-value>
        </init-param>
        <!-- <load-on-startup>是啟動順序,讓這個Servlet隨Servletp容器一起啟動,必須放在<servlet> 配置的最后。 -->
        <load-on-startup>1</load-on-startup><!-- load-on-startup必須放在最后 -->
    </servlet>
    <!-- Spring MVC配置文件結束 -->
    <servlet-mapping>
        <!-- 指定配置的是哪個servlet -->
        <servlet-name>SpringMVCLesson</servlet-name>
        <!-- 指定攔截哪些請求到該servlet,這里配置的是攔截全部請求 -->
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

第五步:創建一個Contrller控制的Java類:

package com.zte.ems.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/user")
public class FormController {
    @RequestMapping("/success")
    public String getSuccess() {
        //跳轉頁面,默認為轉發
        return "success";

    }
}

其中@Controller注解表示,該類是一個受Spring管理的類,一個類使用了@Controller進行標記的都是Controller。

@RequestMapper注解表示,url路徑的映射,其實RequestMapping在Class上,可看做是父Request請求url,而RequestMapping在方法上的可看做是子Request請求url,父子請求url最終會拼起來與頁面請求url進行匹配。

最后通過clean tomcat7:run 來啟動tomcat,在瀏覽器輸入響應的url即可訪問。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM