SpringMVC之三:配置Spring MVC Controller

1.Controller配置方式

URL对应Bean

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

<!-- 表示将请求的URL和Bean名字映射-->  
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<bean name="/hello.do" class="test.HelloController"></bean> 

以上配置,访问/hello.do就会寻找ID为/hello.do的Bean,此类方式仅适用小型的应用系统

为URL分配Bean

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

<!-- 最常用的映射配置方式 -->  
<!-- <prop key="/hello*.do">helloController</prop>-->  
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">  
 <property name="mappings">  
  <props>  
   <prop key="/hello.do">helloController</prop>  
  </props>  
 </property>  
</bean>  
<bean name="helloController" class="test.HelloController"></bean>

此类配置还可以使用通配符,访问/hello.do时,Spring会把请求分配给helloController进行处理

URL匹配Bean

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

<!-- 将hello*.do交给helloController处理-->  
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"></bean>  
<bean name="helloController" class="test.HelloController"></bean> 

注解

首先在配置文件中开启注解

<!-- 启用 spring 注解 -->  
<context:component-scan base-package="test" />  
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<!--或者使用下面的标签来配置-->
<mvc:annotation-driven></mvc:annotation-driven>

在编写类上使用注解@org.springframework.stereotype.Controller标记这是个Controller对象
使用@RequestMapping("/hello.do")指定方法对应处理的路径,这里只是简单示例,会有更复杂配置

代码类如下:

@Controller  
public class HelloController{  
    @SuppressWarnings("deprecation")  
    @RequestMapping("/hello.do")  
    public String hello(HttpServletRequest request,HttpServletResponse response){  
        request.setAttribute("user", request.getParameter("user") + "-->" + new Date().toLocaleString());  
        return "hello";  
    }  
}

2.Controller详解

获取请求输入

Spring MVC 提供三种方式来获取 client 传输的数据:

  • 查询参数(Query parameters)
  • 表单参数(Form parameters)
  • 路径变量(Path variables)

路径变量(Path variables)

如果需要通过 ID 查询一个资源,我们可以把 ID 放在请求参数的位置上(/spittles/show?spittle_id=12345),也可以放在路径变量的位置上(/spittles/12345)。对于一个资源来说,后一种方式要更好,前一种方式表明一个动作带有请求参数,后一种就代表我是请求一个 ID 为 12345 的资源,更明确也更简单。

为了写一个面向资源的控制器,需要使用 { 和 } 把路径变量括起来,这样 Spring 才能解析。然后,使用 @PathVariable注解将路径变量的值赋值给一个变量,以便在控制器中使用。

@RequestMapping(value="/{spittleId}", method=RequestMethod.GET)
public String spittle(
    @PathVariable("spittleId") long spittleId, 
    Model model) {
  model.addAttribute(spittleRepository.findOne(spittleId));
  return "spittle";
}

在本例中,使用了 spittleId 作为 url 上的占位符,然后赋值给 spittleId。如果省略 @PathVariable 注解的 value 属性,那么必须保证占位符和变量名称匹配,这样才能正确解析。 

查询参数(Query parameters)

Spring MVC 中可以通过 @RequestParam 注解获取请求中的参数,还可以通过 defaultValue 属性设置默认值(只能使用 String 类型)。

@RequestMapping(method=RequestMethod.GET)
public List<Spittle> spittles(
    @RequestParam(value="max", defaultValue=MAX_LONG_AS_STRING) long max,
    @RequestParam(value="count", defaultValue="20") int count) {
  return spittleRepository.findSpittles(max, count);
}

表单参数

如果请求参数包含一个 bean(比如整个表单的提交),那么可以使用 Spring 自动将请求参数组合成一个 Bean。

@RequestMapping(method=RequestMethod.POST)
public String saveSpittle(SpittleForm form, Model model) throws Exception {
  spittleRepository.save(new Spittle(null, form.getMessage(), new Date(), 
      form.getLongitude(), form.getLatitude()));
  return "redirect:/spittles";
}

在 saveSpittle 方法的参数上,有个 SpittleForm 类型的参数。Spring 会用请求参数中和 SpittleForm 中成员变量相同名称的参数的值来填充 from 变量。

本例中,返回了一个 redirect: 作为前缀的字符串,当 InternalResourceViewResolver 看到这个前缀是,将会执行 redirect 动作,而不是渲染视图。当然,如果要执行 forward 只需要把前缀修改为 forward: 就行了。

猜你喜欢

转载自blog.csdn.net/u010277958/article/details/89290802