專注成就輝煌

          專注java

          SpringMvc&Maven初級篇(一)

          開發工具:Eclipse IDE for Java EE Developers
          目標:使用SpringMvc框架和Maven配置,開發HelloWorld項目
          步驟:
          1.新建--Maven Project



          2.配置eclipse:
          右鍵項目,選擇Project Facets,點擊Convert to faceted from
          http://dl.iteye.com/upload/attachment/357986/90cb6af7-e66e-36f7-93dd-a8dbb809146e.png,
          更改Dynamic Web Module的Version為2.5。(3.0為Java7的)。如果提示錯誤,可能需要在Java Compiler設置Compiler compliance level 為1.6。或者需要在此窗口的Java的Version改成1.6。
          http://dl.iteye.com/upload/attachment/357990/7146caa8-1652-3fbd-8cf1-4eff8d13933a.png,
          點擊Further configuration available…,彈出Modify Faceted Project窗口此處是設置web.xml文件的路徑,我們輸入src/main/webapp。Generate web.xml deployment descriptor自動生成web.xml文件,推薦勾選。
          設置部署程序集(Web Deployment Assembly):
          http://dl.iteye.com/upload/attachment/357992/d8d75b79-e276-3e4b-a121-c172cb00f126.png
           Add -> Java Build Path Entries -> Maven Dependencies -> Finish

          設置完成效果圖
          http://dl.iteye.com/upload/attachment/357996/1def857e-b75a-35f3-ac0b-b7f1c42db7ec.png


          3.配置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/xsd/maven-4.0.0.xsd">
          <modelVersion>4.0.0</modelVersion>
          <groupId>helloworld</groupId>
          <artifactId>helloworld</artifactId>
          <version>0.0.1-SNAPSHOT</version>
          <packaging>war</packaging>
          <name>helloworld</name>
          <description>helloworld</description>

          <properties>
          <spring.version>3.0.5.RELEASE</spring.version>
          </properties>

          <dependencies>
          <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-core</artifactId>
          <version>${spring.version}</version>
          </dependency>
          <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-webmvc</artifactId>
          <version>${spring.version}</version>
          </dependency>
          <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-beans</artifactId>
          <version>${spring.version}</version>
          </dependency>
          <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>${spring.version}</version>
          </dependency>
          <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-aop</artifactId>
          <version>${spring.version}</version>
          </dependency>
          <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-tx</artifactId>
          <version>${spring.version}</version>
          </dependency>

          <!-- Other dependencies -->
          <dependency>
          <groupId>commons-logging</groupId>
          <artifactId>commons-logging</artifactId>
          <version>1.1.1</version>
          </dependency>
          <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>servlet-api</artifactId>
          <version>2.5</version>
          </dependency>
          <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.8.1</version>
          </dependency>
          </dependencies>
          </project>

          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_3_0.xsd"
              id
          ="WebApp_ID" version="3.0">
              
          <display-name>helloworld</display-name>
              
          <servlet>
                  
          <servlet-name>dispatcher</servlet-name>
                  
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
                  
          <load-on-startup>1</load-on-startup>
              
          </servlet>

              
          <servlet-mapping>
                  
          <servlet-name>dispatcher</servlet-name>
                  
          <url-pattern>*.html</url-pattern>
              
          </servlet-mapping>
              
          <welcome-file-list>
                  
          <welcome-file>index.html</welcome-file>
                  
          <welcome-file>index.htm</welcome-file>
                  
          <welcome-file>index.jsp</welcome-file>
                  
          <welcome-file>default.html</welcome-file>
                  
          <welcome-file>default.htm</welcome-file>
                  
          <welcome-file>default.jsp</welcome-file>
              
          </welcome-file-list>
          </web-app>

          dispatcher-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:p="http://www.springframework.org/schema/p"
              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-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
              "
          >
              
              
          <!-- 搜索的控制類路徑(C) -->
              
          <context:component-scan base-package="com.myapps.controllers" />
              
              
          <!-- 配置視圖路徑(V) -->
              
          <bean id="viewResolver"    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                  
          <property name="prefix" value="/WEB-INF/views/"/>
                  
          <property name="suffix" value=".jsp"/>
              
          </bean>
          </beans>

          在WEB-INF下新建文件夾:views
          在views文件夾下新建

          helloworldPage.jsp
          <%@ 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>helloworldPage.jsp</title>
          </head>
          <body>
          Hello,world.Welcome ${name} here.
          </body>
          </html>

          新建包:com.myapps.controllers

          在上面的包內新建類:HelloWorldController.java



          package com.myapps.controllers;

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

          @Controller
          public class
          HelloWorldController {


          @RequestMapping(value="/helloworld")

          public ModelAndView helloWord() {
          return new
          ModelAndView("helloworldPage", "name", "hdh");

          }

          }


           

          接著在webapp下新建index.htm(注意后綴是htm,因為如果是html的話,會和web.xml的<url-pattern>*.html</url-pattern>攔截的后綴名沖突,當然也可以新建index.jsp文件)

          <!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>index.htm</title>
          </head>
          <body>
          <a href="helloworld.html">hello,world(htm)</a>
          </body>
          </html>


          最后,部署項目,然后啟動tomcat服務器,輸入http://localhost:8080/helloworld/index.htm
          ,點擊鏈接進行測試。這樣簡單的Hello,world入門就完成了。
          源碼下載地址:/Files/svygh123/helloworld.rar
          有不足的地方,請指正,有不明白的地方可以交流,謝謝。

          posted on 2012-06-02 23:57 一江東水 閱讀(6118) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 徐水县| 麦盖提县| 蒲城县| 莲花县| 剑川县| 调兵山市| 高邮市| 彩票| 黑水县| 兴和县| 尉氏县| 定结县| 德保县| 中宁县| 嘉义县| 额敏县| 盐山县| 商河县| 梁山县| 冕宁县| 冷水江市| 通许县| 大同县| 霍城县| 九寨沟县| 池州市| 治多县| 马山县| 浮山县| 德令哈市| 长沙市| 鸡泽县| 炎陵县| 临洮县| 清水河县| 桃江县| 紫阳县| 侯马市| 曲沃县| 长海县| 冀州市|