SpringMVC 基礎及應用(一)--HelloWorld

準備工具:
eclipse-jee-mars-2-win32-x86_64
apache-tomcat-6.0.48
SpringMVC需要的jar包

SpringMVC簡介

1、SpringMVC和Struts一樣是一個MVC框架,和Spring無縫連接。和Struts2有點相似。
2、SpringMVC屬于SpringFrameWork的后續產品,Spring框架提供了構建Web應用程序的全功能MVC模塊。
3、使用Spring可插入的MVC架構,可以選擇是使用的內置的Spring web框架還可以是Struts這樣的Web框架。

新建一個SpringMVC項目

1、file-->new-->Dynamic Web Project-->SpringMVC1
2、選擇Apache Tomcat v6.0,配置好服務。

image.png

3、Finish
4、生成一個SpringMVC1 Project

image.png

5、添加 lib

image.png

6、web.xml 配置

<servlet>
    <servlet-name>springMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  
    <load-on-startup>1</load-on-startup>
  </servlet>
  
  <servlet-mapping>
    <servlet-name>springMVC</servlet-name> 
    <url-pattern>/</url-pattern> 
  </servlet-mapping>
image.png

7、control類:HelloWorldController.java

package com.tiany.web.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class HelloWorldController implements Controller {
    
    
    

    @Override
    public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
        
        System.out.println("----hello SpringMVC------");
        // TODO Auto-generated method stub 
        return new ModelAndView("/welcome");
    }
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub

    }

}
image.png

8、spring-servlet.xml 配置

<?xml version="1.0" encoding="UTF-8"?>  
<!-- Bean頭部 -->  
<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:mvc="http://www.springframework.org/schema/mvc"  
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:util="http://www.springframework.org/schema/util"  
    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  
            http://www.springframework.org/schema/util 
            http://www.springframework.org/schema/util/spring-util-3.0.xsd">  
        
        <bean name="/test1/helloworld" class="com.tiany.web.controller.HelloWorldController"></bean>
        
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <!-- <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>-->
            <property name="prefix" value="/" />
            <property name="suffix" value=".jsp" />
        </bean>

</beans>
image.png

詳細解析:http://www.cnblogs.com/yw0219/p/6086571.html

9、apache 配置

image.png
image.png

啟動后報錯:

image.png

重命名下名稱
springMVC-servlet.xml

image.png

查看頁面
http://localhost:8080/SpringMVC1/test1/helloworld

image.png

千里之行始于足下,just hello world!

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,973評論 19 139
  • Spring Boot 參考指南 介紹 轉載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 46,970評論 6 342
  • 1. Java基礎部分 基礎部分的順序:基本語法,類相關的語法,內部類的語法,繼承相關的語法,異常的語法,線程的語...
    子非魚_t_閱讀 31,767評論 18 399
  • 一. Java基礎部分.................................................
    wy_sure閱讀 3,837評論 0 11
  • 茶葉因沸水,才能釋放出深蘊的清香,生命也只有遭遇一次次挫折,才能留下人生的幽香…… 。懂得時時感恩的人 …是最幸福...
    精神燦爛閱讀 366評論 0 2