Spring MVC 注解开发

2021-01-15 03:15

阅读:573

标签:handler   springmvc   rop   博客   Servle   上下   必须   封装数据   依赖   

Spring MVC 注解开发

所需pom依赖:

 dependency>
      groupId>org.springframeworkgroupId>
      artifactId>spring-webmvcartifactId>
      version>5.2.0.RELEASEversion>
    dependency>

    dependency>
      groupId>javax.servletgroupId>
      artifactId>servlet-apiartifactId>
      version>2.5version>
    dependency>
    dependency>
      groupId>javax.servlet.jspgroupId>
      artifactId>jsp-apiartifactId>
      version>2.2version>
    dependency>
    dependency>
      groupId>javax.servletgroupId>
      artifactId>jstlartifactId>
      version>1.2version>
    dependency>

第一步配置 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>springmvcservlet-name>
    servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    init-param>

      param-name>contextConfigLocationparam-name>
      param-value>classpath:springmvc-servlet.xmlparam-value>
    init-param>

    load-on-startup>1load-on-startup>
  servlet>

  servlet-mapping>
    servlet-name>springmvcservlet-name>
    url-pattern>/url-pattern>
  servlet-mapping>

web-app>

第二步:编写DispatchServlet绑定的配置文件:

技术图片

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.xiaofu.controller"/>

    mvc:default-servlet-handler/>

    mvc:annotation-driven/>
    

    bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
        property name="prefix" value="/WEB-INF/jsp/"/> 
        property name="suffix" value=".jsp"/> 
    bean>
    
beans>

第三步: 创建Controller

技术图片

package com.xiaofu.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 hello(Model model){
        //封装数据
        model.addAttribute("msg","helloSpringMVC");
        return "text"; //会被视图解析器处理(相当于要跳转的页面)
    }

}
@Controller 这个注解会自动帮我们注册bean 相当于: 
@RequestMapping("")设置访问的路由 

第四步: 创建对应的返回页面
技术图片
%--
  Created by IntelliJ IDEA.
  User: 86176
  Date: 2021/1/12
  Time: 11:36
  To change this template use File | Settings | File Templates.
--%>
%@ page contentType="text/html;charset=UTF-8" language="java" %>
html>
head>
    title>Titletitle>
head>
body>

${msg}
body>
html>

第五步:测试:

先配置tomcat:博客:https://www.cnblogs.com/love2000/p/14164947.html

技术图片

 启动tomcat

技术图片

 输入地址访问成功!

 

 

Spring MVC 注解开发

标签:handler   springmvc   rop   博客   Servle   上下   必须   封装数据   依赖   

原文地址:https://www.cnblogs.com/love2000/p/14270801.html


评论


亲,登录后才可以留言!