第一个SpringMVC程序(注解实现)

第一个SpringMVC程序(注解实现)

Spring MVC 的核心包是:

  • spring-web
  • spring-webmvc

目录结构:

1、配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <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:spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>
  • 在上述web.xml文件中主要配置了<servlet><servlet-mapping>
    • 配置了前端控制器DispatcherServlet
    • 绑定了springmvc配置文件的位置
    • load-on-startup元素中的 1 表示容器(Tomcat)在启动时立即加载这个 DispatcherServlet
    • url-pattern 元素的 /,会对所有 URL 拦截(不包括 jsp / html 等),并交由 DispatcherServlet 处理

2、编写Spring mvc的配置文件

<?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
            https://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/mvc
            https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="com.xu.controller"/><!--自动包扫描-->
    <mvc:default-servlet-handler/><!--让spring不处理请求静态资源的请求-->

    <mvc:annotation-driven/><!--代替了:HandlerMapping  和  HandlerAdapter-->

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/><!--前缀-->
        <property name="suffix" value=".jsp"/><!--后缀-->
        <!--到时候我们直接返回逻辑视图名就ok了!-->
    </bean>

</beans>

3、编写视图(hello.jsp)

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page isELIgnored="false" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>第一个MVC程序(注解实现)</title>
</head>
<body>
<h1>${msg}</h1>
</body>
</html>

4、编写Controller

package com.xu.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloController {

    @RequestMapping("/hello") //括号里写请求的路径
    public String hellomvc1(Model model ){
        model.addAttribute("msg","第一个Spring-MVC程序(注解实现)");
        return "hello"; //直接返回视图名
    }

5、测试

  • 请求路径:http://localhost:8080/springmvc_02_annotation_war_exploded/hello

  • 结果:

猜你喜欢

转载自www.cnblogs.com/whitespaces/p/12450348.html