<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    專注成就輝煌

    專注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?;蛘咝枰诖舜翱诘腏ava的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 一江東水 閱讀(6115) 評論(0)  編輯  收藏


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


    網站導航:
     
    主站蜘蛛池模板: 亚洲精品无码少妇30P| 久久亚洲精品成人av无码网站| 中文字幕亚洲综合久久综合| 国产片免费在线观看| 亚洲综合精品伊人久久| 免费中文熟妇在线影片| 国产性爱在线观看亚洲黄色一级片| 久久精品国产亚洲AV未满十八| 男人的天堂网免费网站| 亚洲国产精品国自产拍AV| 无码人妻一区二区三区免费看| 亚洲图片在线观看| 久久精品网站免费观看 | 午夜免费福利在线观看| 亚洲国产成人久久精品软件 | 亚洲一区二区三区免费观看| 91亚洲导航深夜福利| 18勿入网站免费永久| 亚洲色偷偷色噜噜狠狠99| 四虎影视永久免费视频观看| 国产视频精品免费视频| 亚洲成人动漫在线观看| 在线免费观看一级毛片| 一级做a爰全过程免费视频毛片 | 在线亚洲午夜理论AV大片| 色欲国产麻豆一精品一AV一免费| 亚洲成a人不卡在线观看| 免费高清av一区二区三区| 污污污视频在线免费观看| 久久久久亚洲AV成人无码| 在线永久看片免费的视频| 亚洲成a人片在线不卡一二三区 | 黄在线观看www免费看| 亚洲精品蜜夜内射| 亚洲无线观看国产精品| 成人免费激情视频| 成在线人直播免费视频| 亚洲高清免费在线观看| 又大又黄又粗又爽的免费视频| 光棍天堂免费手机观看在线观看| 色偷偷女男人的天堂亚洲网|