SpringMvc 控制器实现的几种方式(重点理解注解方式@Controller 和RequestMapping())

1.实现Controller接口 (这种实现可以用 request 和Response来传参 和输入)

public class HelloAction implements Controller {


    @Override
    public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {

        ModelAndView modelAndView = new ModelAndView();
        System.out.println( httpServletRequest.getParameter("username"));

        modelAndView.addObject("message","helloSpring");
        modelAndView.setViewName("success");  //这样写相对路就需要配置视图解析器

        return modelAndView;
    }
}

2:继承 AbstractCommandController  类可以实现 参数Bean的封装 并且可以实现日期类型的转换功能

public class HelloAction2 extends AbstractCommandController {

    //编写一个构造器 在加载创建是会把所有的参数进行封装
    public HelloAction2(){
        //封装参数,并且会自动实现传递到 下面方法中的Object o对象中去;
        this.setCommandClass(Emp.class); //对象的字节码
    }

    @Override
    //时期类型转换器
    protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
        //参数类型:将String转成什么类型的字节;
        binder.registerCustomEditor(
                Date.class,
                new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),true));


    }


    @Override
    protected ModelAndView handle(
            HttpServletRequest request,
            HttpServletResponse response,
            Object o,  //表示封装后的实体,从上面的构造方法中自动传入
            BindException e) throws Exception {

        ModelAndView modelAndView = new ModelAndView();
        //默认是保存在request域中
        modelAndView.addObject("message","test");
        modelAndView.setViewName("success");
        Emp emp = (Emp) o;
        System.out.println(emp.getUsername()+emp.getHiredate());
        return modelAndView;
    }
}

jsp

 <body>
    <form action="${pageContext.request.contextPath}/hello2.action" method="post">
      username:<input type="text" name="username"/>
      <br/>
      pwd:<input type="text" name="pwd"/>
      <br/>
      qq:<input type="text" name="qq"/>
      <br/>
      <input type="text" name="hiredate" value="2015-5-10"/>
      <input type="submit" value="tijiao"/>
    </form>
  </body>

3:注解(重点掌握)直接写一个普通的java类通过的注解的方式来实现 (@Controller  和RequestMapping()注解来实现

springmvc中配置如下

<?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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
    ">
    <!--配置Controller扫描-->
    <context:component-scan base-package="com.cn.controllers"></context:component-scan>

    <!--释放静态支援-->
<!--    <mvc:default-servlet-handler/>-->

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

action中编写

@Controller
public class HelloAction {

    @RequestMapping("/hello.action")
    public String hello(Model model, HttpServletRequest request, HttpServletResponse response) throws  Exception{
        System.out.println("调用了我吗?");
        model.addAttribute("messsage","注解实现springmvc");

        System.out.println("hello world 我是通过注解来实现的");
        return "hello";
    }
}

扩展知识点:springmvc中的配置 有一个特殊的bean配置   ParameterizableViewController 

<!--   专用于jsp 到jsp的转发 不需要经过其他的过滤     就相当于 a.jsp 跳转到 b.jsp --> 
<?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:mvc="http://www.springframework.org/schema/mvc"
      xsi:schemaLocation="
      http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
      
        <!--    控制器-
        <bean name="/hello.action" class="com.cn.HelloAction"></bean>
        <bean name="/hello2.action" class="com.cn.HelloAction2"></bean>
        <bean name="/hello3.action" class="com.cn.HelloAction3"></bean>
     -->

<!-- 映射器 <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
     -->
<!-- 适配器 <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>      --> <!-- 专用于jsp 到jsp的转发 不需要经过其他的过滤 --> <bean name="/index.action" class="org.springframework.web.servlet.mvc.ParameterizableViewController"> <property name="viewName" value="success"></property> </bean> <!-- 视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>

jsp

<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
    <form action="${pageContext.request.contextPath}/index.action" method="post">
      <input type="submit" value="tijiao"/>
    </form>
  </body>
</html>