mvc html 注解,【spring springmvc】springmvc使用注解声明控制器与请求映射

概述

注解: 在Spring中尽管使用XML配置文件可以实现Bean的装配工作,但如果应用中Bean的数量较多,会导致XML配置文件过于臃肿,从而给维护和升级带来一定的困难。

从JDK 5开始提供了名为Annotation(注解)的功能,Spring正是利用这一特性,Spring逐步完善对Annotation注解技术的全面支持,使XML配置文件不再臃肿,向“零配置”迈进。

Spring框架也为表示层提供了一个优秀的Web框架,即Spring MVC。由于Spring MVC采用了松耦合可插拔组件结构,比其他MVC框架具有更大的扩展性和灵活性。通过注解,Spring MVC使得POJO成为处理用户请求的控制器,无需实现任何接口。

壹:注解说明

Spring中定义了一系列的Annotation注解,如下所示:

注解名称

说明

@Component注解

@Component 是一个泛化的概念,仅仅表示一个组件(Bean),可以作用在任何层次。

@Repository注解

@Repository 注解用于将数据访问层(DAO 层)的类标识为Spring的Bean。

@Service注解

@Service 通常作用在业务层,但是目前该功能与@Component相同。

@Controller注解

@Controller标识表示层组件,但是目前该功能与@Component相同

@Autowired注解

用于对Bean的属性变量、属性的set方法及构造函数进行标注,配合对应的注解处理器完成Bean的自动配置工作。@Autowired注解默认按照Bean类型进行装配。@Autowired注解加上@Qualifier注解,可直接指定一个Bean实例名称来进行装配。

@Resource注解

作用相当于@Autowired,配置对应的注解处理器完成Bean的自动配置工作。区别在于:①:@Autowired默认按照Bean类型进行装配,②:@Resource默认按照Bean实例名称进行装配。

贰:实现注解声明控制器与请求映射

一:使用controller

org.springframework.stereotype.Controller注解类型用于指示Spring类的实例是一个控制器,其注解形式为@Controller。该注解在使用时不需要再实现Controller接口,只需要将@Controller注解加入到控制器类上,然后通过Spring的扫描机制找到标注了该注解的控制器即可。

@Controller

public class SpringController {

@GetMapping("/helloWorld")

public String hello(){

System.out.println("hello.....");

return "hello";

}

}

我们常用的rest 风格请求(REST : 即 Representational State Transfer 。(资源)表现层状态转化):

请求

说明

用于

@GetMapping

匹配GET方式的请求;

一般读取数据

@PostMapping

匹配POST方式的请求;

一般用于插入数据

@PutMapping

匹配PUT方式的请求;

一般用于更新数据

@DeleteMapping

匹配DELETE方式的请求;

一般用于删除数据

二:配置包扫描与视图解析器

1、配置包扫描

虽然哦我们已经i邪恶好了controller,但是直接这样写我们是不能用的,还需要在spring-mvc.xml配置文件中,用spring的包扫描将他注入到容器中,我们才能实现调用。

当然,spring提供了很多种方法,我们是用最简单实现的就可以。

2、配置试图解析器

SpringMVC中的视图解析器的主要作用就是将逻辑视图转换成用户可以看到的物理视图。

在spring-mvc.xml加入试图解析器,其中的前缀就是根据自己的文件存放目录来写,后缀就是你的文件的后缀名,你可以是.jsp、.html等等。

三:配置部署描述符

Deployment Descriptors(描述符)是一个xml文件,用来描述如何部署一个模块或者应用(根据描述符中定义的配置和容器选项)。

在这里简单来说就是我们的web.xml。

1、读取spring-mvc.xml文件

虽然,我们已经把controller通过spring-mvc.xml注入到容器中,相信这时你启动项目时,是访问不了的controller的请求的,也就是说,我们的我没在配置该文件,这时候在你的web.xml中加入。

如果我们不指定SpringMVC配置文件的路径,则会自动到WEB-INF下找 ‘‘前端控制器名-servlet.xml’’ 这个文件,如果找不到则会报错。

contextConfigLocation

classpath:spring-mvc.xml

2、配置匹配映射

servlet: 它提供静态资源。它处理所有未映射到其他带有servlet映射的servlet(在这里或在您自己的-web.xml文件)。

servlet-mapping: 当为静态资源提供服务时,Tomcat将自动生成基于资源文件扩展名的“Content Type”头,基于这些映射。可以在此处添加其他映射(到应用于所有web应用程序),或在您自己的应用程序的web.xml中部署描述符。

dispatcherServlet

org.springframework.web.servlet.DispatcherServlet

dispatcherServlet

/

四:建立html文件

新建两个文件,用于访问测试,一个index.html,写一个连接指向href=“helloWorld”,写另一个hello.html,用于访问成功后,跳转到该页面。

叁:配置tomcat

在这里,作者摸索到了两种配置tomcat的方法,一种就是本地自个的tomcat,还有一种就是maven提供的tomcat容器。

一:配置本地tomcat

基本上就是这几步,其中的选择tomcat目录省略了,不是很难,添加服务就可以,找不到入口就算了,请不要打我。如果你配置tomcat也不会,那么,现在放下电脑去打把王者荣耀吧,妲己可能会告诉你。

5401539cf979

tomcat

二:配置maven内置tomcat

配置maven的tomcat相对会麻烦一点,不过也不是很麻烦,在你的pom.xml文件中加入一下插件依赖。

org.apache.tomcat.maven

tomcat7-maven-plugin

2.2

然后,添加配置,需要注意的是,你需要配置你的maven,在下图的General里面,如果你已经使用了maven,就不用配了,可以查看General下配置是否正确。

安装与配置maven:传送门

5401539cf979

maven-tomcat

接下来你就可以启动你的项目了,祝你能够一步成功,哈哈哈哈。

肆:结果及问题

一:tomcat启动示意图:

本地tomcat:

5401539cf979

image

maven内置tomcat:

5401539cf979

image

二:结果

首页:

5401539cf979

首页

死案及后跳转:

5401539cf979

hello

三:问题

1、解决SpringMVC不能访问html页面

default

*.html

2、使用maven内置tomcat有时能跳转,有时不能跳转,不能跳转的时候他会卡在读取文件这里,这里对不起了,笔者没找到解决方法,如果你找到了,欢迎告诉笔者。

三月 21, 2020 1:02:09 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions

信息: Loading XML bean definitions from class path resource [spring-mvc.xml]

该问题追加:

根据csdn评论中大佬的解决方法,我重新试了一下,后来发现问题根源,发现项目根本找不到spring-context.xsd,导致spring-mvc.xml文件加载出错,并且造成通配符的匹配很全面, 但无法找到元素 'context:annotation-config' 的声明的错误,所以项目一直在加载spring-mvc.xml文件,这时我就意识到是我的spring-mvc.xml中的引入出问题了,经过修改,问题解决了。

修改前:

https://www.springframework.org/schema/context/spring-context.xsd

修改后:

http://www.springframework.org/schema/context/spring-context.xsd

伍:结构及源码

源码都放出来了,还不是ctrl+c,ctrl+v一顿乱搞。

一:目录结构

5401539cf979

目录结构

二:源码

1、controller

package com.lomtom.controller;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.GetMapping;

/**

* @Author: LOMTOM

* @Date: 2020/3/20

* @Time: 18:40

* @Email: lomtom@qq.com

*/

@Controller

public class SpringController {

@GetMapping("/helloWorld")

public String hello(){

System.out.println("hello.....");

return "hello";

}

}

2、spring-mvc.xml

xmlns:mvc="http://www.springframework.org/schema/mvc"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context"

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

https://www.springframework.org/schema/context/spring-context.xsd">

3、web.xml

/p>

"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"

"http://java.sun.com/dtd/web-app_2_3.dtd" >

Archetype Created Web Application

dispatcherServlet

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:spring-mvc.xml

dispatcherServlet

/

default

*.html

4、pom.xml

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

spring

spring

1.0-SNAPSHOT

4.0.0

spring-8

war

spring-8 Maven Webapp

http://www.example.com

UTF-8

1.8

1.8

junit

junit

4.11

test

org.springframework

spring-web

4.3.1.RELEASE

org.springframework

spring-webmvc

4.3.1.RELEASE

spring-8

org.apache.tomcat.maven

tomcat7-maven-plugin

2.2

maven-clean-plugin

3.1.0

maven-resources-plugin

3.0.2

maven-compiler-plugin

3.8.0

maven-surefire-plugin

2.22.1

maven-war-plugin

3.2.2

maven-install-plugin

2.5.2

maven-deploy-plugin

2.8.2

5、html

1、index.html

Hello World!

hello

2、hello.html

hello

你好啊,你成功了

作者有话

这篇文章,作者已经肝了很久了,如果对你有用的话,点个赞再走吧,不说了,作者要交作业去了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值