springMVC学习(三)controller

  • Controller方法的返回值有5种:

    • void

    • String

    • ModelAndView

    • redirect重定向

    • forward转发

使用注解开发

使用@Controller注解,就表明这是一个SpringMVC的控制器,使用了注解以后就不用显示地继承或实现任何类了

  • 在spring配置文件中扫描使用了注解的包

  • 使用@Controller,@RequestMapping开发

@Controller
public class HelloAction{
    /*
    * @RequestMapping 表示只要是/hello.action的请求,就交由该方法处理。.action可以去掉
    * @param Model 它和ModelAndView类似,Model就是把数据封装到request对象中,我们就可以获取出来
    * @return 返回跳转的页面【真实路径,就不用配置视图解析器了】
    * @throws Exception
    */
   @RequestMapping(value = "/hello.action")
   public String hello(Model model) throws Exception {
       System.out.println("hello:13213");
       model.addAttribute("message","你好");
       return "/jsp/hello.jsp";
  }
}

基于注解和基于XML来开发SpringMVC,都是通过映射器、适配器和视图解析器的。 只是映射器、适配器略有不同。但是都是可以省略的

<!-- 基于注解的映射器(可选) -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>

<!-- 基于注解的适配器(可选) -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>

<!-- 视图解析器(可选) -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"/>

上面两个配置也可以使用 注解驱动<mvc:annotation-driven> 替代注解处理器和适配器的配置

  • RequestMapping功能

    • 控制请求路径,使一个控制器可以写多个业务方法

        @RequestMapping(value="/hello.action")
       public String hello(Model model) throws Exception{
           return "/index.jsp";
      }
       @RequestMapping(value = "/bye.action")
       public String bye(Model model) throws Exception {  
           return "/index.jsp";
      }
    • 分模块开发,@RequestMapping注解写到类上面去,就代表了分模块

    @Controller
    @RequestMapping("/hjy")
    public class HelloAction{
       @RequestMapping(value = "/hello.action")
       public String hello(Model model) throws Exception {
           return "/jsp/hello.jsp";
      }
    }

    访问地址 http://localhost:8080/hjy/hello.action

    • 限制请求方法,使用method属性(也可以使用@GetMapping、@PostMapping直接限制请求方法)

        @RequestMapping(value = "/hello.action",method = RequestMethod.POST)
       public String hello(Model model) throws Exception {
           return "/jsp/hello.jsp";
      }

    该业务方法只能通过post请求访问

参数接收

  • 接收普通参数,直接在方法上写上与web端带过来名称相同的参数就行了

    @RequestMapping(value = "/hello.action")
   public String hello(Model model,String username,int id) throws Exception {
       System.out.println(id+"="+username);
       return "/jsp/hello.jsp";
  }
  • 接收JavaBean

    • 创建Javabean,javaBean属性与表单带过来的名称相同

    • 在业务方法上传入参数Javabean

  • 接收数组,对应前端传入单选框或多选框,直接在业务方法上传入数组,要求和前端的参数名称一样

  • 接收List<JavaBean>,使用一个JavaBean把集合封装起来,给出对应的set和get方法。那么我们在接收参数的时候,其实接收的是JavaBean,这个JavaBean是一个List【与接收普通的JavaBean类似的】

  • 接收多个不同的JavaBean,抽象出一个Bean包含这些JavaBean

字符串转日期

前端传入一个日期(1996-05-24),springmvc默认无法将日期字符串转化为Date,需要用到@InitBinder注解设置日期字符串的格式,就可以将前端传入的日期字符串转化为Date

//在Controller中使用此注解设置日期字符串格式
@InitBinder
   protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
       binder.registerCustomEditor(
               Date.class,
               new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));

  }

结果重定向和转发

实现重定向或转发,只需要请求对应的请求路径就行了: redirect:请求路径

    @RequestMapping(value = "/bye.action")
   public String bye(Model model) throws Exception {
       return "/jsp/bye.jsp";
  }

   @RequestMapping(value = "/date.action")
   public String dateTest(Model model, Date date) throws Exception {
       SimpleDateFormat sfd=new SimpleDateFormat("yyyy-MM-dd");
       System.out.println(sfd.format(date));
       return "redirect:/bye.action";
  }

 

原文地址:https://www.cnblogs.com/yjh1995/p/14164371.html