Java---學習(3)

        我們這節看下怎麼創建spring MVC項目,通過MAVEN來創建項目,Spring MVC項目的創建需要引用第三方的組件,包括Spring-MVC,配置Tomact服務器等。

      1.Spring MVC項目創建

        打開esclipse,選擇菜單 File–>New–Project,選擇maven,創建項目,如下圖

       

        點擊下一步,選擇創建目錄

       

       點擊下一步

      

       點擊下一步,輸入項目名稱,GroupId表示項目的組織名稱,Artifact Id表示項目名稱,一個GroupId下面可以有多個項目

      

        點擊完成後,項目創建成功,目錄結構如下圖

      

       默認情況下 src/main/Java目錄結構可能不存在,可以通過New–>Source Folder創建,

       如果創建不成功,右鍵選中項目–屬性,找到java-bulid-path面板,選擇Source選項卡,如果選項卡中已經存在,並且顯示紅色叉號,可以先刪除,然後就可以創建了。

       如下圖:

         

        2.項目文件配置

          POM.XML文件

          POM.XML文件是MAVEN的項目管理文件,通過在文件中配置可以自動引用第三方jar包。我們創建Spring MVC,需要引用spring相關組件,我們可以添加如下 spring組件配置信息。如下:

       

  1. <project xmlns=“http://maven.apache.org/POM/4.0.0” xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”  
  2.   xsi:schemaLocation=“http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd”>  
  3.   <modelVersion>4.0.0</modelVersion>  
  4.   <groupId>com.springmvc</groupId>  
  5.   <artifactId>springmvcfirst</artifactId>  
  6.   <packaging>war</packaging>  
  7.   <version>0.0.1-SNAPSHOT</version>  
  8.   <name>springmvcfirst Maven Webapp</name>  
  9.   <url>http://maven.apache.org</url>  
  10.   <dependencies>  
  11. <!– http servlet配置 –>  
  12.   <dependency>  
  13.     <groupId>javax.servlet</groupId>  
  14.     <artifactId>javax.servlet-api</artifactId>  
  15.     <version>3.1.0</version>  
  16. </dependency>  
  17.   <!– spring mvc web組件配置 –>  
  18.   <dependency>  
  19.     <groupId>org.springframework</groupId>  
  20.     <artifactId>spring-webmvc</artifactId>  
  21.     <version>4.2.5.RELEASE</version>  
  22. </dependency>  
  23.   <!– spring 組件配置 –>  
  24.   <dependency>  
  25.     <groupId>org.springframework</groupId>  
  26.     <artifactId>spring-context</artifactId>  
  27.     <version>4.2.5.RELEASE</version>  
  28. </dependency>  
  29.     
  30.     
  31.     <dependency>  
  32.       <groupId>junit</groupId>  
  33.       <artifactId>junit</artifactId>  
  34.       <version>3.8.1</version>  
  35.       <scope>test</scope>  
  36.     </dependency>  
  37.   </dependencies>  
  38.   <build>  
  39.     <finalName>springmvcfirst</finalName>  
  40.   </build>  
  41. </project>  
<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.springmvc</groupId>
  <artifactId>springmvcfirst</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>springmvcfirst Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
<!-- http servlet配置 -->
  <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
</dependency>
  <!-- spring mvc web組件配置 -->
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>4.2.5.RELEASE</version>
</dependency>
  <!-- spring 組件配置 -->
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.2.5.RELEASE</version>
</dependency>


    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>springmvcfirst</finalName>
  </build>
</project>

            關於組件的配置信息,可以在 http://mvnrepository.com/這個地方搜索


 web.xml文件,web.xml是整個srping.mvc網站的全局配置文件,包括springmvc的配置,spring的具體配置,

       

  1. <?xml version=“1.0” encoding=“UTF-8”?>    
  2.   
  3. <web-app xmlns=“http://xmlns.jcp.org/xml/ns/javaee”  
  4.       xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”  
  5.       xsi:schemaLocation=”http://xmlns.jcp.org/xml/ns/javaee   
  6.       http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd”  
  7.       version=“3.1”>  
  8.         
  9.   <display-name>Spring MvC First</display-name>  
  10.   <description>Spring MvC First</description>  
  11.     
  12.    <!– Spring配置–>  
  13.          <context-param>  
  14.              <param-name>contextConfigLocation</param-name>  
  15.              <param-value>/WEB-INF/applicationContext.xml</param-value>  
  16.          </context-param>  
  17.            
  18.     
  19.   <!– 字符集 過濾器  –>    
  20.     <filter>    
  21.         <filter-name>CharacterEncodingFilter</filter-name>    
  22.         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>    
  23.         <init-param>    
  24.             <param-name>encoding</param-name>    
  25.             <param-value>UTF-8</param-value>    
  26.         </init-param>    
  27.         <init-param>    
  28.             <param-name>forceEncoding</param-name>    
  29.             <param-value>true</param-value>    
  30.         </init-param>    
  31.     </filter>    
  32.     <filter-mapping>    
  33.         <filter-name>CharacterEncodingFilter</filter-name>    
  34.         <url-pattern>/*</url-pattern>    
  35.     </filter-mapping>    
  36.       
  37.       <!– Spring監聽 –>  
  38.          <listener>  
  39.              <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  40.          </listener>  
  41.       
  42.       
  43.   <!– Spring view分發器 –>    
  44.     <servlet>    
  45.         <servlet-name>dispatcher</servlet-name>    
  46.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    
  47.         <init-param>    
  48.             <param-name>contextConfigLocation</param-name>    
  49.             <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>    
  50.         </init-param>    
  51.         <load-on-startup>1</load-on-startup>    
  52.     </servlet>    
  53.     <!– 請求後綴 –>  
  54.     <servlet-mapping>    
  55.         <servlet-name>dispatcher</servlet-name>    
  56.         <url-pattern>/</url-pattern>    
  57.     </servlet-mapping>    
  58. </web-app>  
<?xml version="1.0" encoding="UTF-8"?>  

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
      http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
      version="3.1">

  <display-name>Spring MvC First</display-name>
  <description>Spring MvC First</description>

   <!-- Spring配置-->
         <context-param>
             <param-name>contextConfigLocation</param-name>
             <param-value>/WEB-INF/applicationContext.xml</param-value>
         </context-param>


  <!-- 字符集 過濾器  -->  
    <filter>  
        <filter-name>CharacterEncodingFilter</filter-name>  
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
        <init-param>  
            <param-name>encoding</param-name>  
            <param-value>UTF-8</param-value>  
        </init-param>  
        <init-param>  
            <param-name>forceEncoding</param-name>  
            <param-value>true</param-value>  
        </init-param>  
    </filter>  
    <filter-mapping>  
        <filter-name>CharacterEncodingFilter</filter-name>  
        <url-pattern>/*</url-pattern>  
    </filter-mapping>  

      <!-- Spring監聽 -->
         <listener>
             <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
         </listener>


  <!-- Spring view分發器 -->  
    <servlet>  
        <servlet-name>dispatcher</servlet-name>  
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
        <init-param>  
            <param-name>contextConfigLocation</param-name>  
            <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>  
        </init-param>  
        <load-on-startup>1</load-on-startup>  
    </servlet>  
    <!-- 請求後綴 -->
    <servlet-mapping>  
        <servlet-name>dispatcher</servlet-name>  
        <url-pattern>/</url-pattern>  
    </servlet-mapping>  
</web-app>


   dispatcher-servlet.xml文件是專門針對spring mvc的配置,裏面有對頁面路徑,後綴訪問的配置,改文件的名稱是有<servlet-name>dispatcher</servlet-name>  節點的值加上-servlet.xml組成.

 

  1. <?xml version=“1.0” encoding=“UTF-8”?>  
  2. <beans xmlns=“http://www.springframework.org/schema/beans”     
  3.        xmlns:aop=“http://www.springframework.org/schema/aop”     
  4.        xmlns:context=“http://www.springframework.org/schema/context”    
  5.        xmlns:mvc=“http://www.springframework.org/schema/mvc”     
  6.        xmlns:tx=“http://www.springframework.org/schema/tx”     
  7.        xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”    
  8.        xsi:schemaLocation=”http://www.springframework.org/schema/aop     
  9.         http://www.springframework.org/schema/aop/spring-aop-3.0.xsd     
  10.         http://www.springframework.org/schema/beans     
  11.         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd     
  12.         http://www.springframework.org/schema/context     
  13.         http://www.springframework.org/schema/context/spring-context-3.0.xsd     
  14.         http://www.springframework.org/schema/mvc     
  15.         http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd     
  16.         http://www.springframework.org/schema/tx     
  17.         http://www.springframework.org/schema/tx/spring-tx-3.0.xsd”>    
  18.     
  19.     <mvc:annotation-driven />    
  20.     <context:component-scan base-package=“com.springfirst.Controller” />    
  21.     
  22.   <!– ②:啓動Spring MVC的註解功能,完成請求和註解POJO的映射 –>  
  23.     <bean class=“org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter”/>  
  24.       
  25.     <bean class=“org.springframework.web.servlet.view.InternalResourceViewResolver”>    
  26.         <property name=“prefix” value=“/WEB-INF/views/” />    
  27.         <property name=“suffix” value=“.jsp” />    
  28.     </bean>    
  29.     
  30. </beans>    
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"   
       xmlns:aop="http://www.springframework.org/schema/aop"   
       xmlns:context="http://www.springframework.org/schema/context"  
       xmlns:mvc="http://www.springframework.org/schema/mvc"   
       xmlns:tx="http://www.springframework.org/schema/tx"   
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
       xsi:schemaLocation="http://www.springframework.org/schema/aop   
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd   
        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   
        http://www.springframework.org/schema/tx   
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">  

    <mvc:annotation-driven />  
    <context:component-scan base-package="com.springfirst.Controller" />  

  <!-- ②:啓動Spring MVC的註解功能,完成請求和註解POJO的映射 -->
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
        <property name="prefix" value="/WEB-INF/views/" />  
        <property name="suffix" value=".jsp" />  
    </bean>  

</beans>  

applicationContext.xml

  1. <?xml version=“1.0” encoding=“UTF-8”?>  
  2. <beans xmlns=“http://www.springframework.org/schema/beans”  
  3.     xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” xmlns:context=“http://www.springframework.org/schema/context”  
  4.     xmlns:aop=“http://www.springframework.org/schema/aop” xmlns:p=“http://www.springframework.org/schema/p”  
  5.     xmlns:cache=“http://www.springframework.org/schema/cache” xmlns:repo=“http://www.springframework.org/schema/data/repository”  
  6.     xmlns:tx=“http://www.springframework.org/schema/tx” xmlns:jpa=“http://www.springframework.org/schema/data/jpa”  
  7.     xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
  8.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  
  9.         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd  
  10.         http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd  
  11.         http://www.springframework.org/schema/data/repository http://www.springframework.org/schema/data/repository/spring-repository-1.7.xsd  
  12.         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
  13.             http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd ”  
  14.     default-lazy-init=“true”>  
  15.   
  16.     <description>Spring配置</description>  
  17.   
  18.   
  19.     
  20. </beans>  
<?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:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:cache="http://www.springframework.org/schema/cache" xmlns:repo="http://www.springframework.org/schema/data/repository"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
        http://www.springframework.org/schema/data/repository http://www.springframework.org/schema/data/repository/spring-repository-1.7.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
            http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd "
    default-lazy-init="true">

    <description>Spring配置</description>



</beans>


      這幾個配置文件是spirng mvc的核心配置文件。以後我們需要增加更多配置可以在這幾個文件裏面添加。
       

    3.添加控制器和頁面

       控制器controller是對用戶處理請求的,控制器接受用戶的輸入並調用模型和視圖去完成用戶的需求,我們開看看怎樣添加控制器
       右鍵選中src/main/java,  new–>Class,如下圖
       
        代碼如下
       
  1. package com.springfirst.Controller;  
  2.   
  3. import org.springframework.stereotype.Controller;  
  4. import org.springframework.web.bind.annotation.RequestMapping;  
  5.   
  6. @Controller  
  7. @RequestMapping(“/Home”)  
  8. public class HomeController {  
  9.   
  10.     @RequestMapping(value=“index”)  
  11.     public String Index()  
  12.     {  
  13.         System.out.print(“123”);  
  14.         return “index”;  
  15.     }  
  16. }  
package com.springfirst.Controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/Home")
public class HomeController {

    @RequestMapping(value="index")
    public String Index()
    {
        System.out.print("123");
        return "index";
    }
}

      添加頁面,在WEB-INF文件夾下創建views文件夾,添加ndex.jsp頁面,,右鍵選中文件夾  new–>jsp..flle
  1. <%@ page language=“java” contentType=“text/html; charset=UTF-8”  
  2.     pageEncoding=“UTF-8”%>  
  3. <!DOCTYPE html PUBLIC ”-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”>  
  4. <html>  
  5. <head>  
  6. <meta http-equiv=“Content-Type” content=“text/html; charset=UTF-8”>  
  7. <title>Insert title here</title>  
  8. </head>  
  9. <body>  
  10.   Hello World,我的第一個spring mvc項目  
  11. </body>  
  12. </html>  
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
  Hello World,我的第一個spring mvc項目
</body>
</html>

   4.運行項目

          項目創建完成後,我們要看到預覽效果,這裏我們使用tomcat服務器運行頁面。首先去tomcat官網下載tomcat服務器,然後我們按如下步驟添加。
          找到菜單windows–>server->run environment,點擊add添加
          
                 

               添加完成後,在Servers,中添加tomcat服務器.如下圖
                 

   啓動服務器,右鍵選中服務器,點擊Start, 啓動,  然後在瀏覽器中輸入地址就可以
   
   項目結構圖
   




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章