專注成就輝煌

          專注java

          SpringMvc初級篇

          目標:使用SpringMvc框架,開發HelloWorld項目

          步驟:
          1.準備SpringMvc相關jar包
          commons-io-1.2.jar
          commons-logging-1.1.1.jar
          freemarker.jar
          hibernate-validator-4.0.2.GA.jar
          jstl-1.2.jar
          log4j-1.2.14.jar
          org.springframework.context.support-3.1.0.M2.jar
          servlet-2.3.jar
          slf4j-api-1.5.6.jar
          slf4j-log4j12-1.5.6.jar
          spring-asm-3.0.3.RELEASE.jar
          spring-beans-3.0.3.RELEASE.jar
          spring-context-3.0.3.RELEASE.jar
          spring-core-3.0.3.RELEASE.jar
          spring-expression-3.0.3.RELEASE.jar
          spring-web-3.0.3.RELEASE.jar
          spring-webmvc-3.0.3.RELEASE.jar
          validation-api-1.0.0.GA.jar
          commons-fileupload-1.1.1.jar
          2.創建Dynamic Web Project項目,名稱為helloworld,將SpringMvc相關jar包復制到WEB-INF/lib文件夾下
          3.配置SpringMvc相關文件
          ①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>

          ②在WEB-INF文件夾下新建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>

           

          ③新建包: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");
           }

          }



          ⑤在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>


          ⑥接著在WebContent下新建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>
           
          <href="helloworld.html">hello,world(htm)</a>
          </body>
          </html>


          ⑦最后,部署項目,然后啟動tomcat服務器,輸入http://localhost:8080/helloworld/index.htm ,點擊鏈接進行測試。這樣簡單的Hello,world入門就完成了。
          如果需要jar包,請發郵件到:357228560@qq.com 索取


          posted on 2012-06-01 22:28 一江東水 閱讀(1516) 評論(0)  編輯  收藏


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


          網站導航:
           
          主站蜘蛛池模板: 达尔| 东平县| 庄浪县| 阿鲁科尔沁旗| 含山县| 长宁区| 大关县| 历史| 永昌县| 扬州市| 昆明市| 朔州市| 肇州县| 岱山县| 抚顺县| 绵竹市| 东安县| 莒南县| 三台县| 滨州市| 汾西县| 台南市| 榆中县| 平舆县| 葵青区| 江孜县| 颍上县| 关岭| 罗平县| 涿州市| 安西县| 泗洪县| 张掖市| 韶山市| 特克斯县| 友谊县| 临颍县| 永川市| 沙坪坝区| 正宁县| 吴忠市|