zoukankan      html  css  js  c++  java
  • SpingMVC常用注解之@Controller注解的使用

    一、简介

    @Controller用于标记一个类,被标记的类就是一个控制类。Spring使用扫描机制查找所有基于注解的控制器类。

    分发处理器会扫描该类下的方法,并检测该方法是否使用@RequestMapping注解,使用了才是真正的处理请求的处理器。

    为保证Spring能找到控制器,SpringMVC的配置文件应如下配置

    <context:component-scan base-package="controller类所在的包名" />

    相应的配置头文件应引入  xmlns:context="http://www.springframework.org/schema/context"

    二、使用

    例:HelloWorld的实现

    1、控制层代码

    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    public class HelloWorldController {
        
        @RequestMapping("/helloWorld")
        public String helloWorld(Model model){
            model.addAttribute("message","SpringMVC你好啊");
        return "helloWorld";
        
        }
    }

    @Controller 基于注解的控制器,@RequestMapping注解用来映射一个请求,value="/helloWorld" 表示 前台的这个请求由对应的helloWorld方法处理

    helloWorld()方法接收一个org.springframework.ui.Model类型的参数 model ,model调用addAtrribute方法,添加一个字符串对象message,

    message对象可以在前台通过request对象获取。

    最后,返回helloWorld,这个返回的helloWorld 是前台的名称,因为SpringMVC配置文件中有视图解析器,相当于返回到helloWorld.jsp

    2、SpringMVC配置文件

    <?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:p="http://www.springframework.org/schema/p"
        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/context
            http://www.springframework.org/schema/context/spring-context.xsd">
    
        <!-- 使用注解的包,包括子集 -->
        <context:component-scan base-package="guo"/>
    
        <!-- 视图解析器 -->
        <bean id="viewResolver"
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsp/" />
            <property name="suffix" value=".jsp"></property>
        </bean>
    
    </beans>

    由于使用了注解,所以不需要在配置文件中使用XML描述Bean。

     <context:component-scan base-package=" guo"/> 指定Spring扫描guo包下所有java文件。

    视图解析器中  prefix属性表示视图前缀,suffix,表示视图后缀。

    控制层helloWorld方法最后返回的是helloWorld,经过视图解析器解析后(加上前缀后缀后)

    前台视图的完整路径为: /WEB-INF/jsp/helloWorld.jsp

    (没有配置处理映射器和处理适配器,Spring会使用默认的。除此之外 web.xml文件需要配置SpringMVC的DispatcherServlet)

    3、运行结果

    请求http://localhost:8080/SpringMVC01/helloWorld.do (SpringMVC01是我项目名,helloWorld是@RequestMapping的value属性值)

    参考书籍:《Spring+MyBatis企业应用实战》 疯狂软件编著,电子工业出版社。

    本博客为博主的学习笔记,不作任何商业用途。
  • 相关阅读:
    CentOS6.3搭建Nginx代理访问MongoDB GridFS图片资源
    PHP判断变量是否存在及函数isset() 、empty()与is_null的区别
    【摘】请问make -j8 和make -j4 是什么意思?什么作用?
    关于数字、数据处理的几个PHP函数汇总
    Windows下Nginx的启动、停止等基本命令
    Git 简明教程
    PHP函数preg_replace() 正则替换所有符合条件的字符串
    如何挂载阿里云Linux服务器的“数据盘”(新购买)
    ThinkPHP模板中JS等带花括号处会被解析错误的解决办法
    移动端与PHP服务端接口通信流程设计(增强版)
  • 原文地址:https://www.cnblogs.com/guo7533/p/9018774.html
Copyright © 2011-2022 走看看