SpringMVC注解实现-最简单的web框架搭建
前言:
 之前使用配置文件+controller接口的形式实现了,现在把之前的改成注解版本的,如果大家需要配置文件版本的请访问:下面链接https://kangshihang.blog.csdn.net/article/details/117650526
 404问题
 因为缺少lib依赖,所以出现404问题。请参考下面地址解决
 https://kangshihang.blog.csdn.net/article/details/117597184
web项目创建:
1,创建模块(maven工程)
 点击File——》New ——》Modeul 
 选择左边的maven,不点击create from archetype
 
 直接点击next
 
 写下项目的名字和存放位置
2,添加web模块(不使用maven创建默认项目)
 右击模块,点击AddFrameworkSupport
 
 勾选web Appliction
 
 配置文件的版本已经是4.0版本了
 <?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">
 
         </web-app>
model架构

代码实现:
配置文件
 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">
    <!--1.注册dispatcherServlet-->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--关联一个springmvc的配置文件-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc-servlet.xml</param-value>
        </init-param>
        <!--启动顺序,数字越小,启动越早 -->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <!--匹配url-->
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>
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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--添加视图解析器:DispatcherServlet给他的ModelAndView-->
 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
<!-- 前缀-->
     <property name="prefix" value="/WEB-INF/jsp/"/>
<!--后缀-->
     <property name="suffix" value=".jsp"/>
 </bean>
    <!-- 自动扫描包,让指定包下的注解生效,由IOC容器统一管理 -->
    <context:component-scan base-package="com.kuang.controller"/>
    <!-- 让Spring MVC不处理静态资源 -->
    <mvc:default-servlet-handler />
    <!--
    支持mvc注解驱动
    在spring中一般采用@RequestMapping注解来完成映射关系
    要想使@RequestMapping注解生效
    必须向上下文中注册DefaultAnnotationHandlerMapping
    和一个AnnotationMethodHandlerAdapter实例
    这两个实例分别在类级别和方法级别处理。
    而annotation-driven配置帮助我们自动完成上述两个实例的注入。
    -->
    <mvc:annotation-driven />
</beans>
controller代码
 package com.kuang.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
/**
 * @program: springMVC
 * @description: 注解测试springmvc
 * @author: 康世行
 * @create: 2021-06-02 10:20
 */
@Controller
@RequestMapping("/helloController")
public class HelloController {
    //真实的访问地址:项目名/helloController/hello
    @RequestMapping("/hello")
    public String sayHello(Model model){
        model.addAttribute("msg","hello,springmvc");//返回数据
        return "hello";//返回具体视图
    }
}
页面
 在WEB-INF下建一个jsp文件夹,因为视图解析器指向的是这个位置
 
 jsp文件
 <%--
  Created by IntelliJ IDEA.
  User: ksh
  Date: 2021-06-02
  Time: 10:31
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
  ${msg}
</body>
</html>
注意,添加完lib依赖一定要,从新打包Tomcat的包。
    欢迎访问csdn的博客:
https://blog.csdn.net/kangshihang1998?spm=1010.2135.3001.5343
 
                    
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号