java之spring mvc之Controller配置的几种方式

时间:2021-01-05 20:33:32

这篇主要讲解 controller配置的几种方式。

1. URL对应 Bean

如果要使用此类配置方式,需要在XML中做如下样式配置

<!-- 配置handlerMapping -->
    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
    <!-- 配置Controller -->
    <bean name="/hello.do" class="cn.sxt.controller.HelloController"/>

2. 为 URL 分配 Bean

使用一个统一配置集合,对各个 URL 对应的 Controller 做关系映射

<!-- 配置handlerMapping -->   
      <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
          <property name="mappings">
              <props>
                  <prop key="/hello.do">helloController</prop>
              </props>
          </property>
      </bean>
     <bean id="helloController" class="cn.sxt.controller.HelloController"></bean>

该配置可以使用通配符

3. URL 匹配 Bean

如果定义的 Controller 名称规范,也可以使用如下配置

将 hello*.do 交给 helloController 处理

<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"></bean>
<bean id="helloController" class="cn.sxt.controller.HelloController"></bean>

4.使用注解进行开发

需要导入 aop.jar 包

Controller 的 开发:

/**
 * @Controller 注解一个控制器 需要扫描
 */
@Controller public class HelloController{
    /**
     * 注解请求的url
     */
    @RequestMapping("/hello.do")
    public ModelAndView hello(HttpServletRequest req){
        System.out.println("使用注解进行开发:"+req.getRemoteHost());
        ModelAndView mv = new ModelAndView("hello");
        mv.addObject("msg", "使用注解开发Controller");
        return mv;
    }
}

配置文件

<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">
    <!-- 注解开发适配器 -->
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> 
    <!-- 配置视图解析器 -->
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
           <!-- 为响应的视图名称加上前缀  -->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <!-- 为响应的视图名称加上后缀  -->
        <property name="suffix" value=".jsp"/>
    </bean>
    <!-- 扫描注解类 -->
    <context:component-scan base-package="cn.sxt.controller"/>
</beans>

 

附录:

这里附上上面配置的完整配置信息

附一

<?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">
  
  <!-- 配置handlerMapping -->
    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
    <!-- 配置handlerAdapter -->
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
    <!-- 配置Controller -->
    <bean name="/hello.do" class="cn.vincent.controller.HelloController"/>
    <!-- 配置视图解析器 -->
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
           <!-- 为响应的视图名称加上前缀  -->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <!-- 为响应的视图名称加上后缀  -->
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

附二

<?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">
     <!-- 配置handlerMapping -->   
      <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
          <property name="mappings">
              <props>
                  <prop key="/hello.do">helloController</prop>
              </props>
          </property>
      </bean>
     <bean id="helloController" class="cn.sxt.controller.HelloController"></bean>
    <!-- 配置handlerAdapter -->
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
    <!-- 配置视图解析器 -->
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
           <!-- 为响应的视图名称加上前缀  -->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <!-- 为响应的视图名称加上后缀  -->
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

附三

<?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">
     <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"></bean>
     <bean id="helloController" class="cn.sxt.controller.HelloController"></bean>
    <!-- 配置handlerAdapter -->
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
    <!-- 配置视图解析器 -->
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
           <!-- 为响应的视图名称加上前缀  -->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <!-- 为响应的视图名称加上后缀  -->
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

附四

<?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">
    <!-- 注解开发适配器 -->
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> 
    <!-- 配置视图解析器 -->
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
           <!-- 为响应的视图名称加上前缀  -->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <!-- 为响应的视图名称加上后缀  -->
        <property name="suffix" value=".jsp"/>
    </bean>
    <!-- 扫描注解类 -->
    <context:component-scan base-package="cn.sxt.controller"/>
</beans>