SpringMVC入门HelloWorld

本文详细介绍了如何使用SpringMVC创建一个简单的HelloWorld程序。包括导入必要的jar包、配置web.xml和springmvc-servlet.xml文件、创建Controller以及启动服务器进行验证。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一,创建一个HelloWorld

1.首先,导入SpringMVC需要的jar包。

2.添加Web.xml配置文件中关于SpringMVC的配置
 <!--configure the setting of springmvcDispatcherServlet and configure the mapping-->
  <servlet>
      <servlet-name>springmvc</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc-servlet.xml</param-value>
        </init-param>
        <!--上下文启动时 加载此servlet -->
     <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
      <servlet-name>springmvc</servlet-name>
      <url-pattern>/</url-pattern>
  </servlet-mapping>
3.在src下添加springmvc-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: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.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">                    

    <!-- 扫描基包下的所有注解类 -->
    <context:component-scan base-package="test.SpringMVC"/>

    <!-- don't handle the static resource -->
    <mvc:default-servlet-handler />

    <!-- if you use annotation you must configure following setting -->
    <mvc:annotation-driven />
    
    <!-- configure the InternalResourceViewResolver -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
            id="internalResourceViewResolver">
        <!-- 前缀 -->
        <property name="prefix" value="/WEB-INF/jsp/" />
        <!-- 后缀 -->
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

4.在WEB-INF文件夹下创建名为jsp的文件夹,用来存放jsp视图。创建一个hello.jsp,在body中添加“Hello World”。

5.建立包及Controller,如下所示

6.编写Controller代码

@Controller
@RequestMapping("/mvc")
public class mvcController {

    @RequestMapping("/hello")
    public String hello(){        
        return "hello";
    }
}



7.启动服务器,键入 http://localhost:8080/项目名/mvc/hello


二、配置解析

1.Dispatcherservlet

  DispatcherServlet是前置控制器,配置在web.xml文件中的。拦截匹配的请求,Servlet拦截匹配规则要自已定义,把拦截下来的请求,依据相应的规则分发到目标Controller来处理,是配置spring MVC的第一步。

2.InternalResourceViewResolver

  视图名称解析器

3.以上出现的注解

@Controller 负责注册一个bean 到spring 上下文中

@RequestMapping 注解为控制器指定可以处理哪些 URL 请求


ml配置文件中关于SpringMVC的配置
作者: IT程序狮
链接:http://www.imooc.com/article/1392
来源:慕课网
ml配置文件中关于SpringMVC的配置
作者: IT程序狮
链接:http://www.imooc.com/article/1392
来源:慕课网
ml配置文件中关于SpringMVC的配置
作者: IT程序狮
链接:http://www.imooc.com/article/1392
来源:慕课网
ml配置文件中关于SpringMVC的配置
作者: IT程序狮
链接:http://www.imooc.com/article/1392
来源:慕课网
ml配置文件中关于SpringMVC的配置
作者: IT程序狮
链接:http://www.imooc.com/article/1392
来源:慕课网
ml配置文件中关于SpringMVC的配置
作者: IT程序狮
链接:http://www.imooc.com/article/1392
来源:慕课网
ml配置文件中关于SpringMVC的配置
作者: IT程序狮
链接:http://www.imooc.com/article/1392
来源:慕课网
一、SpringMVC基础入门,创建一个HelloWorld程序
作者: IT程序狮
链接:http://www.imooc.com/article/1392
来源:慕课网
一、SpringMVC基础入门,创建一个HelloWorld程序
作者: IT程序狮
链接:http://www.imooc.com/article/1392
来源:慕课网
一、SpringMVC基础入门,创建一个HelloWorld程序
作者: IT程序狮
链接:http://www.imooc.com/article/1392
来源:慕课网
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值