SpringMVC的两种实现方式详解,哪种更高效?

2026-05-25 23:121阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计653个文字,预计阅读时间需要3分钟。

SpringMVC的两种实现方式详解,哪种更高效?

目录

一、方法一:实现Controller接口

二、方法二:使用注解开发

一、方法一:实现Controller接口

这个方法是通过实现Controller接口来创建控制器。这种方式较为传统,需要在Java类中直接实现接口的方法。

二、方法二:使用注解开发使用注解是SpringMVC推荐的方式,它简化了代码,提高了开发效率。通过在类和方法上添加注解,可以轻松定义控制器和映射的URL。

目录
  • 一、方法一:实现Controller接口
  • 二、方法二:使用注解开发

一、方法一:实现Controller接口

这个在我的第一个SpringMVC程序中已经学习过了,在此不作赘述,现在主要来学习第二种方法,使用注解开发;

二、方法二:使用注解开发

1.导包

2.在web.xml中配置DispatcherServlet

3.建立一个Spring配置文件springmvc-servlet.xml

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="www.springframework.org/schema/beans" xmlns:xsi="www.w3.org/2001/XMLSchema-instance" xmlns:context="www.springframework.org/schema/context" xmlns:mvc="www.springframework.org/schema/mvc" xsi:schemaLocation="www.springframework.org/schema/beans www.springframework.org/schema/beans/spring-beans.xsd www.springframework.org/schema/context www.springframework.org/schema/context/spring-context.xsd www.springframework.org/schema/mvc www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--扫描包,使其下的注解生效--> <context:component-scan base-package="com.jms.controller"/> <!--让springMVC不处理静态资源--> <mvc:default-servlet-handler/> <!-- 支持MVC注解驱动 能够帮助我们完成BeanNameUrlHandlerMapping和SimpleControllerHandlerAdapter注入 --> <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>

4.建立一个HelloController类

package com.jms.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("message", "欢迎来到SpringMVC"); return "hello"; } }

可以看到,在这个类中,我们使用到了两种注解。

第一个是@Controller,使用这个注解就说明这个类是一个Handler;第二个是@RequestMapping,看名字就知道这是请求的映射,也就是我们需要请求的路径,这里是请求.../hello。

可以看见一个返回String的方法,返回的这个hello就说明跳转的路径是视图解析器中的“前缀”+hello+“后缀”,在这里也就是/WEB-INF/jsp/hello.jsp。

这里我们用一个model来存储数据。

5.启动tomcat测试

SpringMVC的两种实现方式详解,哪种更高效?

到此这篇关于SpringMVC的两种实现方式的文章就介绍到这了,更多相关SpringMVC实现方式内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!

本文共计653个文字,预计阅读时间需要3分钟。

SpringMVC的两种实现方式详解,哪种更高效?

目录

一、方法一:实现Controller接口

二、方法二:使用注解开发

一、方法一:实现Controller接口

这个方法是通过实现Controller接口来创建控制器。这种方式较为传统,需要在Java类中直接实现接口的方法。

二、方法二:使用注解开发使用注解是SpringMVC推荐的方式,它简化了代码,提高了开发效率。通过在类和方法上添加注解,可以轻松定义控制器和映射的URL。

目录
  • 一、方法一:实现Controller接口
  • 二、方法二:使用注解开发

一、方法一:实现Controller接口

这个在我的第一个SpringMVC程序中已经学习过了,在此不作赘述,现在主要来学习第二种方法,使用注解开发;

二、方法二:使用注解开发

1.导包

2.在web.xml中配置DispatcherServlet

3.建立一个Spring配置文件springmvc-servlet.xml

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="www.springframework.org/schema/beans" xmlns:xsi="www.w3.org/2001/XMLSchema-instance" xmlns:context="www.springframework.org/schema/context" xmlns:mvc="www.springframework.org/schema/mvc" xsi:schemaLocation="www.springframework.org/schema/beans www.springframework.org/schema/beans/spring-beans.xsd www.springframework.org/schema/context www.springframework.org/schema/context/spring-context.xsd www.springframework.org/schema/mvc www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--扫描包,使其下的注解生效--> <context:component-scan base-package="com.jms.controller"/> <!--让springMVC不处理静态资源--> <mvc:default-servlet-handler/> <!-- 支持MVC注解驱动 能够帮助我们完成BeanNameUrlHandlerMapping和SimpleControllerHandlerAdapter注入 --> <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>

4.建立一个HelloController类

package com.jms.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("message", "欢迎来到SpringMVC"); return "hello"; } }

可以看到,在这个类中,我们使用到了两种注解。

第一个是@Controller,使用这个注解就说明这个类是一个Handler;第二个是@RequestMapping,看名字就知道这是请求的映射,也就是我们需要请求的路径,这里是请求.../hello。

可以看见一个返回String的方法,返回的这个hello就说明跳转的路径是视图解析器中的“前缀”+hello+“后缀”,在这里也就是/WEB-INF/jsp/hello.jsp。

这里我们用一个model来存储数据。

5.启动tomcat测试

SpringMVC的两种实现方式详解,哪种更高效?

到此这篇关于SpringMVC的两种实现方式的文章就介绍到这了,更多相关SpringMVC实现方式内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!