手动配置Controller和基于注解实现Controller


一、

1、编写测试类并实现controller接口

public class TestController implements Controller {
    
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        ModelAndView mv = new ModelAndView();
        mv.addObject("msg", "TestController");
        mv.setViewName("test");
        return mv;
    }

}

2、配置spring-mvc.xml

<?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"
       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">

    <!--处理映射器-->
    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>

    <!--处理适配器-->
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>

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

    <bean id="/test" class="com.hrms.controller.TestController"/>

</beans>

3、要跳转的页面test.jsp(放在/WEB-INF/jsp/目录下)

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
Hello,,,,测试成功了
${msg}
</body>
</html>

4、结果

5、缺点

一个控制器中只有一个方法,如果要多个方法则需要定义多个Controller。定义的·方式比较麻烦

二、

1、给controller类加注解@Controller, 并使用@RequestMapping("/getEmpById")注解处理getEmpById请求。

@Controller
public class EmployeeController {

    @RequestMapping("/getEmpById")
    public String getEmpById(Integer empId){
        System.out.println("正在测试中。。。。");
        return "test";
    }
}

2、在spring-mvc.xml中使用注解实现dispatcherServlet三要素中的处理请求器和处理适配器,

由于使用了注解,不需要在spring-mvc.xml中注册bean,使用 <context:component-scan base-package=" "/>指定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
       http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<!--    &lt;!&ndash;处理映射器&ndash;&gt;-->
<!--    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>-->

<!--    &lt;!&ndash;处理适配器&ndash;&gt;-->
<!--    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>-->

    <!--把以上两个配置使用注解来实现-->
    <mvc:annotation-driven/>

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

<!--    <bean id="/test" class="com.hrms.controller.TestController"/>-->
    <!--自动扫描controller,将controller注册到bean中-->
    <context:component-scan base-package="com.hrms.controller"/>

</beans>

3、测试

经验分享 程序员 微信小程序 职场和发展