springmvc教程-第一个controller

概述

spring mvc框架中的 Controller,相当于增强的 servlet:

  1. 接收请求(get、post、ajax)
  2. 获取请求中的参数(url中的参数,post表单参数,ajax参数、上传文件)
  3. 做出响应。(转发页面、重定向页面、返回json)

用 Controller 代替 Servlet,不需要写 Servlet

优点:

更简单的代码,就可以实现更多的功能

创建 controller 类

package top.malaoshi.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

//说明该类是Controller
@Controller
public class HelloCtrl {
    //注解:映射路径,也就是说浏览器访问 `http://localhost:8080/上下文/hello` 时,就能够进入该方法
    @RequestMapping("/hello")
    public String hello(){
        //返回值是字符串类型 时,该字符串就表示 页面的 名字,表示 转发(forward)到 hello的页面
        return "hello";
    }
}

提出问题

并不知道 该 hello页面 是 hello.html 还是 hello.jsp,并且也不知道 所在位置

修改 springMVC.xml

增加注解扫描,扫描 某包下的 @Controller 注解

注意:不配置此项,@Controller 注解无效

<context:component-scan base-package="top.malaoshi.controller"/>

完整代码

<?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
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">


    <!-- 开启springmvc注解的支持。添加静态资源映射时,必须加此配置,否则Controller获取不到请求 -->
    <mvc:annotation-driven />
    <!-- 允许访问html、css、js静态资源 -->
    <mvc:default-servlet-handler/>


    <!-- 扫描top.malaoshi.controller包下的注解 -->
    <context:component-scan base-package="top.malaoshi.controller"/>
</beans>

说明:

  • <context:component-scan base-package="top.malaoshi.controller"/>
    扫描 top.malaoshi.controller包下的注解,否则Controller类和注解不起作用

  • <mvc:annotation-driven /> 开启springmvc注解的支持。添加静态资源映射时,必须加此配置,否则Controller获取不到请求

  • <mvc:default-servlet-handler/> 允许访问html、css、js静态资源

通过 视图解析器 解决问题

spring mvc是通过配置 视图解析器 ,才知道 该页面是 .html 还是 .jsp
所在位置

修改springMVC.xml

增加下面代码

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/"></property>
    <property name="suffix" value=".jsp"></property>
</bean>

说明:

  • prefix:前缀,表示文件所在的位置。/ 表示 webapp 目录
  • suffix:表示文件的后缀

联合起来,表示: /webapp/文件名.jsp,文件名 是 controller方法返回的 字符串

完整代码
<?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
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="top.malaoshi.controller"/>
    <!-- 开启springmvc注解的支持。一般情况可以不加此代码 -->
    <mvc:annotation-driven />

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>

测试

启动tomcat服务

访问: http://localhost:8080/hello ,就进入到 HelloCtrl 的 hello() 方法,该方法返回 hello 字符串,根据视图解析得配置,最终 转发 到 /webapp/hello.jsp


原文出处:/show_1IX4gKRZXD7.html