SpringMvc必会知识-你不会请来砍我

2次阅读

共计 3016 个字符,预计需要花费 8 分钟才能阅读完成。

前言

整顿了一些必会的常识, 你要是不会,就来 砍死博主

罕用注解

@RequestParam
将申请参数绑定到你控制器的办法参数上(是 springmvc 中接管一般参数的注解)
案例:

语法:@RequestParam(value=”参数名”,required=”true/false”,defaultValue=””)
 
value:参数名
 
required:是否蕴含该参数,默认为 true,示意该申请门路中必须蕴含该参数,如果不蕴含就报错。defaultValue:默认参数值,如果设置了该值,required=true 将生效,主动为 false, 如果没有传该参数,就应用默认值
http://localhost:8080/upmovie/movie/getbyid?name=xiaoming
 @RequestMapping("show16")
    public ModelAndView test16(@RequestParam("name")String name){ModelAndView mv = new ModelAndView();
        mv.addObject("msg", "接管一般的申请参数:" + name);
        return mv;
    }

@PathVariable
从一个 URI 模板外面取值来填充

http://localhost:8080/upmovie/movie/getbyid/19
@RequestMapping(value = "/getbyid", method = RequestMethod.GET)
    @ResponseBody
    private Map<String, Object> getbyid(HttpServletRequest request,@RequestParam(value="id", required=true)int idnum){Map<String, Object> modelMap = new HashMap<String, Object>();
        modelMap.put("idnum", idnum);
        return modelMap;
    }

@RequestParam 和 @PathVariable 注解是用于从 request 中接管申请的,两个都能够接管参数,关键点不同的是 @RequestParam 是从 request 外面拿取值 ,而 @PathVariable 是从 一个 URI 模板外面来填充

@RequestMapping是一个用来解决申请地址映射的注解,它能够用于类上,也能够用于办法上。用于类上的注解会将一个特定申请或者申请模式映射到一个控制器之上,示意类中的所有响应申请的办法都是以该地址作为父门路;办法的级别上注解示意进一步指定到解决办法的映射关系。

@RequestMapping("/Student");// 也相当于 @RequestMapping(value="/Student");

罕用有三种属性:
value:指定申请的理论地址,value 能够省略不写;

method 属性:指定申请的类型,次要有 GET、PUT、POST、DELETE,默认为 GET。

produces 属性:指定返回内容类型,如 produces =“application/json; charset=UTF-8”。

主动扫描组件

手动配置 Bean

在 xml 配置文件中,申明一个 bean 或者 component,而后 Spring 容器会检查和注册你的 bean 或 component

<bean id="customerService" class="com.lei.customer.services.CustomerService">
        <property name="customerDAO" ref="customerDAO" />
</bean>

<bean id="customerDAO" class="com.lei.customer.dao.CustomerDAO" />
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"Spring-Customer.xml"});
 
CustomerService cust = (CustomerService)context.getBean("customerService");

应用主动扫描组件

应用 @Component @Controller @Service @Repository

 <context:component-scan base-package="com.lei.customer" />

自定义扫描组件名称

@Service("AAA")
public class CustomerService 
...
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"Spring-Customer.xml"});
CustomerService cust = (CustomerService)context.getBean("AAA");

在扫描中过滤组件

1. 在很多配置中个别都会吧 Spring-common.xml 和 Spring-MVC.xml 进行离开配置,这种配置就行各施其职一样,显得特地清晰。

在 Spring-MVC.xml 中 只对 @Controller 进行扫描就可,作为一个控制器,其余的事件不做。

在 Spring-common.xml 中 只对一些事务逻辑的注解扫描

2. 当初给定一个我的项目包的机构:

com.fq.controlller

com.fq.service

就先给定这两个包机构

Spring-MVC.xml 中有以下配置:

<!-- 扫描 @Controller 注解 -->
<context:component-scan base-package="com.fq.controller">
    <context:include-filter type="annotation"
        expression="org.springframework.stereotype.Controller" />
</context:component-scan>

能够看出要把最终的包写上,而不能这样写 base-package=”com.fq”。这种写法对于 <cide>include-filter 来讲它都会扫描,而不是仅仅扫描 @Controller。哈哈哈,这点须要留神。他个别会导致一个常见的谬误,那就是事务不起作用,补救的办法是增加 use-default-filters=”false”。

在 Spring-common.xml 中有如下配置:

<!-- 配置扫描注解, 不扫描 @Controller 注解 -->
<context:component-scan base-package="com.fq">
    <context:exclude-filter type="annotation"
        expression="org.springframework.stereotype.Controller" />
</context:component-scan>

能够看到,他是要扫描 com.fq 包下的所有子类,不蕴含 @Controller。对于 exculude-filter 不存在包不准确后都进行扫描的问题。

创作不易, 如果本篇文章能帮忙到你, 请给予反对, 赠人玫瑰, 手有余香,虫虫蟹蟹观众姥爷了

正文完
 0