SpringMVC——helloword入门

参考 http://www.cnblogs.com/bigdataZJ/p/springmvc1.html

文章主要讲述以下内容:

搭建环境

静态请求拦截

动态请求拦截

补充:

1、Controller类上面需要增加注解,以便于在spring-mvc.xml中<context:component-scan>来扫描

2、Controller类上面可以加@RequestMapping注解,用来拦截相应的请求。如果不加此注解,则默认表示拦截 / 下的请求

3、方法上的@RequestMapping注解,同***Controller类上的注解。表示处理的请求路径。如:@RequestMapping{value="hello"},其中value可以省略

@RequestMapping前面加不加/都一样,但类上的RequestMapping后面可加也可不加/,方法名上一定不能加/。如方法上@RequestMapping{"/hello"}不报错,@RequestMapping{"/hello/"}就会走不进这个请求中来;而类上,@RequestMapping{"abc"}效果等同于@RequestMapping{"/abc/"}

 关于注解,在下一节做介绍

4、修改注解内容需要重启tomcat

5、web.xml中配置spring的servlet时,默认引用的spring-mvc配置文件为xxx-servlet.xml,xxx表示配置的servlet的名字。如果想自己指定配置文件的路径,可以在servlet配置中增加属性,

<init-param>

  <param-name>contextConfigLocation</param-name>

  <param-value>classpath:/xxx/xxx/myconfig.xml</param-value>

</init-param>

原文地址:https://www.cnblogs.com/aligege/p/6895844.html