SpringMVC中@Controller和@RequestMapping

Controller控制器提供访问应用程序的行为,通常通过两种方式实现,分别是接口和注解

Controller接口实现

首先写一个实现Controller接口,只要是实现了该接口,那么该类就是一个控制器

package com.zhiying.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 ControllerTest implements Controller {
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        ModelAndView modelAndView = new ModelAndView();

        modelAndView.addObject("msg","Controller");
        modelAndView.setViewName("test");

        return modelAndView;
    }
}

写完这个类之后,该写一个配置文件了,并且把这个类注入里面

<?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
        https://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">

<!--        视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--        前缀匹配-->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <!--        后缀匹配-->
        <property name="suffix" value=".jsp"/>
    </bean>

    <bean id="/hello" class="com.zhiying.controller.ControllerTest"/>

</beans>

然后是web.xml了

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

<!--    配置DispatchServlet,其本质是一个Servlet-->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc-servlet.xml</param-value>
        </init-param>

        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

最后是jsp文件了


<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
${msg}
</body>
</html>

Controller注解实现

注解实现,在开发中比较常用,在上面的基础上首先改一下Controller类,这里的@RequestMapping默认请求方式是Get请求,对应于@GetMapping,还有一个是Post请求@PostMapping

package com.zhiying.controller2;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller //加了该注解,说明这个类就是一个控制器了,可以被Spring接管
//该类中的所有方法如果返回值为Spring,并且有具体的页面可以跳转,那么就会被试图器解析
public class ControllerTest2 {

    @RequestMapping("/hello2")
    public String Test1 (Model model) {
        model.addAttribute("msg", "Controller2");
        return "test";//这里被解析为/WEB-INF/jsp/test.jsp
    }

    //这里可以写多个,视图可以被复用
    @RequestMapping("/hello3")
    public String Test2 (Model model) {
        model.addAttribute("msg", "Controller3");
        return "test";
    }

}

然后在配置文件中加上对注解的支持,用了注解之后,就不用对类进行bean的注册了

<?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
        https://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.zhiying"/>

<!--        视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--        前缀匹配-->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <!--        后缀匹配-->
        <property name="suffix" value=".jsp"/>
    </bean>
<!--    <bean id="/hello" class="com.zhiying.controller.ControllerTest"/>-->

</beans>

其他的不动,直接运行即可

如果有多个类和多个方法,想要定位到名字相同的方法的类,可以用下面的方法,修改Controller类

package com.zhiying.controller3;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/test3")
public class ControllerTest3 {

    @RequestMapping("/hello1")
    public String test1(Model model) {
        model.addAttribute("msg", "ControllerTest3");
        return "test";
    }
}

其他不变,这里访问就是要/test3/hello1

发布了376 篇原创文章 · 获赞 242 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/HeZhiYing_/article/details/103991355