曾经的你我高攀不起,现在的我只想注解和配置(下)

简介: 四、Controller配置总结实现Controller接口使用注解开发@Controller

四、Controller配置总结


控制器Controller


  • 控制器复杂体统访问应用程序的行为,通常通过接口定义或注解定义两种方法实现。


  • 控制器负责解析用户的请求,并将其转换为一个模型


  • 在SpringMVC中一个控制器类可以包含多个方法


  • 在SpringMVC中,对于Controller的配置方法由很多种


实现Controller接口

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//只要实现了Controller接口的类,说明着就是一个控制器
public class ControllerTest1 implements Controller {
    public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("msg","ControllerTest1");
        modelAndView.setViewName("test");
        return modelAndView;
    }
}

>在springmvc-servlet.xml,spring配置文件中注册bean;name对应请i求路径,class对应处理请求的类


<?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">
    <!--视图解析器:模板引擎 Thymeleaf Freemarker-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
        <!--前缀,后缀-->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    <bean name="/t1" class="com.hxl.controller.ControllerTest1"/>
</beans>

微信图片_20211230182846.png

>此处实现controller接口有缺点:一个控制器中只有一个方法,如果要多个方法则需要定义多个Controller;定义的方式比较麻烦;


使用注解开发@Controller


  • @Controller注解类型用于声明Spring类的实例是一个控制器(@Component(组件),@Service(servie)、@Controller(controller)、@Respository(dao))


  • Spring可以使用扫描机制来找到应用程序中所有基于注解的控制器类,为了保证Spring能找到你的控制器,需要在配置文件中声明组件扫描。


<?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">
    <!--自动扫描包,让指定包下的注解生效,由IOC容器统一管理-->
    <context:component-scan base-package="com.hxl.controller"/>
    <!--视图解析器:模板引擎 Thymeleaf Freemarker-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
        <!--前缀,后缀-->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

>ControllerTest2


package com.hxl.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
//代表这个类会被spring接管,这个注解的类种的所有方法,如果返回值是String并且有具体的页面可以跳转,那么就会被视图解析器解析
@Controller 
public class ControllerTest2 {
  @RequestMapping("/t2")
  public String test(Model model){
      model.addAttribute("msg", "ControllerTest2");
      return "test";
  }
}

微信图片_20211230183008.png


小技巧:


  • 只要改了java的代码就要reload一下


  • 只要改了配置文件,就要重新启动tomcat


  • 如果只改了前端页面,刷新一下就欧克


注意: 两个请求都可以指向一个视图,但是页面结果的结果是不一样的,从这里可以看出视图是被复用的,而控制器与视图之间是弱偶合关系。


比说说:点击一个多选(有增加有修改),有很多不同的页面,但是不一定有这么多的jsp页面,只是换了个模板,把里面的数据交换一下。如果有相关的开发经验会

相关文章
|
Linux Shell 网络安全
云原生的高效生产工具 ---vagrant
vagrant是一个构件虚拟开发环境的工具。他用于创建部署虚拟的开发环境,说白了就是使用vagrant装虚拟机,而装虚拟机的文件由运维组对开发环境做的一个镜像。
375 0
|
机器学习/深度学习 计算机视觉 Python
用Python写一份独特的元宵节祝福
首先是点阵字的概念:点阵字体是把每一个字符都分成 n * n 个点,然后用每个点的虚实来表示字符的轮廓。点阵字体也叫位图字体,其中每个字形都以一组二维像素信息表示。
|
监控 测试技术 开发者
入门总结 | 学习笔记
快速学习入门总结
116 0
|
缓存 安全 数据格式
springboot中使用缓存shiro-ehcache
在pom.xml中注入缓存依赖,版本(Sep 09, 2016)spring-context-support 包含支持UI模版(Velocity,FreeMarker,JasperReports), 邮件服务, 脚本服务(JRuby), 缓存Cache(EHCache), 任务计划Scheduling(uartz)。
967 0
|
大数据
某团外卖的启示,企业如何避免舆情损失?
最近,某外卖平台分箱事件让“公众舆情”再次进入企业视野。在本次事件中,外卖平台并未即时监控到公众舆情并采取措施,错失黄金公关时间,使公司品牌形象和业务遭受损失。那么企业如何避免舆情损失?
23903 0
|
3天前
|
弹性计算 人工智能 安全
云上十五年——「弹性计算十五周年」系列客户故事(第二期)
阿里云弹性计算十五年深耕,以第九代ECS g9i实例引领算力革新。携手海尔三翼鸟、小鹏汽车、微帧科技等企业,实现性能跃升与成本优化,赋能AI、物联网、智能驾驶等前沿场景,共绘云端增长新图景。
|
9天前
|
存储 弹性计算 人工智能
【2025云栖精华内容】 打造持续领先,全球覆盖的澎湃算力底座——通用计算产品发布与行业实践专场回顾
2025年9月24日,阿里云弹性计算团队多位产品、技术专家及服务器团队技术专家共同在【2025云栖大会】现场带来了《通用计算产品发布与行业实践》的专场论坛,本论坛聚焦弹性计算多款通用算力产品发布。同时,ECS云服务器安全能力、资源售卖模式、计算AI助手等用户体验关键环节也宣布升级,让用云更简单、更智能。海尔三翼鸟云服务负责人刘建锋先生作为特邀嘉宾,莅临现场分享了关于阿里云ECS g9i推动AIoT平台的场景落地实践。
【2025云栖精华内容】 打造持续领先,全球覆盖的澎湃算力底座——通用计算产品发布与行业实践专场回顾
|
8天前
|
人工智能 自然语言处理 自动驾驶
关于举办首届全国大学生“启真问智”人工智能模型&智能体大赛决赛的通知
关于举办首届全国大学生“启真问智”人工智能模型&智能体大赛决赛的通知