第一个springMVC应用——基于注解

项目目录截图

在这里插入图片描述

web.xml文件

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <servlet>
    <!-- servlet名称 -->
    <servlet-name>springMVC</servlet-name>
    <!-- servlet 对应的java类 -->
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- 参数设置 -->
    <init-param>
      <!-- 设置springmvc配置文件的路径 -->
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/springmvc-config.xml</param-value>
    </init-param>
    <!-- 设置web应用启动时,该servlet的启动优先级,越小越高-->
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <!-- 请求对应的servlet名称 -->
    <servlet-name>springMVC</servlet-name>
    <!-- 监听的范围 -->
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

springmvc-config.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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd ">
    <!-- 指定扫描的包 -->
    <context:component-scan base-package="com.controller"/>
    <mvc:annotation-driven/>
</beans>

Controller文件

package com.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * @Author guozhi
 * @Date 2019/6/24 22:04
 * @Description TODO
 */
@Controller
public class Hello {
    @RequestMapping("/")
    public ModelAndView hello(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/WEB-INF/welcome.jsp");
        return mv;
    }
}

welcome.jsp 文件

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE HTML>
<html>
<head>
    <title>Title</title>
</head>
<body>
hello,world!
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值