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


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

1. URL对应 Bean

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


    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
    
    <bean name="/hello.do" class="cn.sxt.controller.HelloController"/>

2. 为 URL 分配 Bean

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

   
      <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
          
              
                  helloController
              
          
      bean>
     <bean id="helloController" class="cn.sxt.controller.HelloController">

该配置可以使用通配符

3. URL 匹配 Bean

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

将 hello*.do 交给 helloController 处理

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

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;
    }
}

配置文件


    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"/> 
    
    class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        
           
        
        
        
    
    
    package="cn.sxt.controller"/>

附录:

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

附一

<?xml version="1.0" encoding="UTF-8"?>

    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">
  
  
    class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
    
    class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
    
    class="cn.vincent.controller.HelloController"/>
    
    class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        
           
        
        
        
    

附二

<?xml version="1.0" encoding="UTF-8"?>

    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">
        
      class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
          
              
                  helloController
              
          
      
     class="cn.sxt.controller.HelloController">
    
    class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
    
    class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        
           
        
        
        
    

附三

<?xml version="1.0" encoding="UTF-8"?>

    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">
     class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
     class="cn.sxt.controller.HelloController">
    
    class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
    
    class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        
           
        
        
        
    

附四

<?xml version="1.0" encoding="UTF-8"?>

    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">
    
    class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> 
    
    class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        
           
        
        
        
    
    
    package="cn.sxt.controller"/>

相关