SpringMVC学习指南【笔记3】基于注解的控制器

Spring MVC基于注解的控制器的优点:

1、一个控制器类可以处理多个动作;

2、基于注解的控制器的请求映射不需要存储在配置文件中。使用RequestMapping注释类型可以对一个方法进行请求处理。

 

Controller注解类型,org.springframework.stereotype.Controller注解类型用于指示Spring类的实例是一个控制器。

Spring使用扫描机制来找到应用程序中所有基于注解的控制器类。为了保证Spring能找到控制器,首先要在Spring MVC的配置文件中声明spring-context,然后要应用<componet-scan/>元素。

例如:

<?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
    ">
    <context:component-scan base-package="com.example.controller" />
</beans>

要确保所有控制器类都在基本包下,并且不要指定一个太广泛的基本包,这样会使Spring MVC扫描了无关的包。

 

RequestMapping注解类型,映射一个请求和一种方法。使用org.springframework.web.bind.annotation.RequestMapping注解类型映射的URI与方法。

value属性是RequestMapping的默认属性,若只有唯一的属性,则可以省略属性名称,但如果有超过一个属性时就必须写上value属性名称。

@RequestMapping(value="/companySales")

@RequestMapping("/companySales")

method属性用来知识该方法仅处理哪些HTTP方法,若method属性只有一个HTTP方法值,则无需花括号。

@RequestMapping(value="/companySales",method={RequestMethod.POST,RequestMethod.PUT})

@RequestMapping(value="/companySales",method=RequestMethod.POST)

 

每个请求方法可以有多个不同类型的参数,以及一个多种类型的返回结果。例如,如果在请求处理方法中需要访问HttpSession对象,则可以添加的HttpSession作为参数,Spring会将对象正确地传递给方法。

 

可以在请求处理方法中出现的参数类型有:

ServletRequest、HttpServletRequest、ServletResponse、HttpServletResponse、HttpSession、WebRequest、NativeWebRequest、Locale、Reader、Writer、InputStream、OutputStream、Principal、Model、ModelMap、RedirectAttributes、Errors、BindingResult、SessionState、UriComponentsBuilder等等。

请求处理方法可以返回的类型对象有:

ModelAndView、Model、Map包含模型的属性、View、代表逻辑视图名的String、void、Callable、DeferredResult、提供对Servlet的访问,以响应HTTP头部和内容HttpEntity或ResonseEntity对象、其它任意类型,Spring将其视作输出给View的对象模型。

 

springmvc-config.xml文件

<?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/springcontext.xsd
    ">

    <!-- 扫描目标包中的类 -->
    <context:component-scan base-package="com.example.controller" />
    <!-- 注册用于支持基于注解的控制器的请求处理方法的bean对象 -->
    <mvc:annotation-driven />
    <!-- 指示Spring MVC哪些静态资源需要单独处理,不需要dispatcher servlet -->
    <mvc:resources mapping="/css/**" location="/css/" />
    <mvc:resources mapping="/*.html" location="/" />

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

</beans>

以上两个<resources/>元素,第一个确保在/css目录下所有文件可见,第二个允许显示所有的.html文件。

注意:如果没有<annotation-driven />,<resources/>元素会阻止任意控制器被调用。若不使用<resources/>,则不需要<annotation-driven />。

 

将依赖注入到Spring MVC控制器的最简单的方法是通过注解@Autowired到字段或方法。

Autowired注解类型属于org.springframework.beans.factory.annotation

为了能被作为依赖注入,类必须要注明为@Service。

Service注解类型是org.springframework.stereotype的成员

Spring MVC配置文件有两个<component-scan/>元素:一个用于扫描控制器类,另一个用于扫描服务类。

<context:component-scan base-package="com.xsl.controller" />
<context:component-scan base-package="com.xsl.service" />

 

转发和重定向的区别

转发比重定向快,因为重定向经过客户端,而转发没有。但有时采用重定向更好,若需要重定向到一个外部网站则无法使用转发。另一个重定向的使用场景是避免用户重新加载页面时再次调用同样的动作。

要使用flash属性,必须在spring mvc配置文件中添加<annotation-driven/>元素,还必须在方法上添加一个新的参数类型org.springframework.web.servlet.mvc.support.RedirectAttributes

 

@RequestParam

@PathVariable

@ModelAttribute

 

posted @ 2018-12-23 16:31  HelloWorld1815  阅读(252)  评论(0编辑  收藏  举报