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!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,949评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 47,317评论 6 342
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 35,093评论 18 399
  • 一. Java基础部分.................................................
    wy_sure阅读 4,085评论 0 11
  • 茶叶因沸水,才能释放出深蕴的清香,生命也只有遭遇一次次挫折,才能留下人生的幽香…… 。懂得时时感恩的人 …是最幸福...
    精神灿烂阅读 454评论 0 2

友情链接更多精彩内容