配置
1. Web.xml
a. ContextLoaderListener:
配置该选项后,服务器将会自动去WEB-INF目录下面查找applicationContext.xml文件来加载Spring的相关配置;也可以使用context-param来指定其它位置,如classpath,表示的是在源文件目录下去查找applicationContext.xml文件。当然也可以指定其它名称的配置文件,甚至可以同时加载多个配置文件(当配置的Bean很多的时候,很有必要将不同作用的Bean分别在不同的配置文件中进行配置,在这种情况下多配置文件就很有用处)。
b. Context-param:
在指定的位置去查找Spring的相关配置
c. Servlet与Servlet-mapping:
众所周知,在WEB开发中,Servlet用于处理浏览器的请求;因此,如果需要Spring来处理Servlet请求,就需要进行相关的配置,来让Spring加载与MVC相关的Bean。
当配置完这两个选项后,有新的浏览器请求到来时,如果发现请求格式符合要求(如接下来的配置文件中配置的以.do结束的请求,当然也可以配置成其它名称),就会跳转到Spring的dispatcherServlet中进行统一处理,然后由其寻找到合适的Controller中进行处理。也就是说,dispatcherServlet相当于一个转发器,将所有交给Spring处理的请求正确的分发到不同的Controller中去。
当配置springmvc的servlet及servlet-mapping后,服务器启动时将会去WEB-INF目录下寻找MVC的配置文件进行加载。寻找的规则就是配置的servlet的名称 + -servlet.xml,如配置的名称一般是springmvc,则查找的配置文件的名称就会是springmvc-servlet.xml。
具体的配置文件如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
当然,也可以将所有的MVC配置项都配置在同一个配置文件applicationContext.xml中去:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
2. ApplicationContext.xml
a. Context:annotation-config
表明Spring是基于注解来进行配置的
b. Context:cpmponent-scan
指定需要扫描的包,也可以指定相关的过滤条件来指定需要装载的或者不需要装载的Bean。
Spring将会去所配置的包中扫描那些被注解的Bean,如@Controller/@Service等注解的Class。注意一般在applicationContext.xml中将Controller的注解忽略,而是在MVC的配置文件中单独进行Controller相关的扫描装配。
这样做的好处就是,用户可以配置多个DispatcherServlet,每个对应于一个配置文件,相互之间配置的Controller不被干扰。这样可以方便地将不同作用的Controller分组存放,修改与更新不影响其它组的Controller。
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <context:annotation-config/> <context:component-scan base-package="com.liuqi.web.webtest"> <context:exclude-filter type="annotation" expression="org.springframework.web.servlet.mvc.Controller"/> </context:component-scan> </beans>
3. Springmvc-servlet.xml
a. Context:annotation-config与applicationContext.xml中的配置意义是一样的。
b. viewResolver
指定视图渲染器,以一个示例来说明其作用:
@RequestMapping("/aaa") public String test() { System.out.println("The test controller!"); return "test"; }
如上述代码,其返回的是一个test的字符串,Spring将会根据viewResolver中配置的相关项来进行组合,加上前缀与后缀,最终得到/WEB-INF/jsp/test.jsp这样一个地址,因此,该Servlet返回后浏览器将会跳转到该页面去。
因此,viewResolver的作用就是指定Servlet返回时跳转到的页面的前缀与后缀。
需要注意的是,在配置文件中指定的前缀是/web-inf/jsp/,我们知道这个目录下的东西是无法直接通过输入地址来访问的,这样也能够在一定的程度上保证网站的安全性:在这个目录下的页面只能通过Servlet来进行访问,这就保证了所有对页面的该都能在我们的监控下进行,当然再需要添加其它的功能如访问日志的记录等就要简单的多。
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <context:annotation-config/> <context:component-scan base-package="com.liuqi.web.webtest"> <context:include-filter type="annotation" expression="org.springframework.web.servlet.mvc.Controller"/> </context:component-scan> <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/WEB-INF/jsp/"></property> <property name="suffix" value=".jsp"/> </bean> </beans>
4. 完整的配置文件就这样,以下是一个相关的Controller
package com.liuqi.web.webtest; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("test") public class ControllerTest { @RequestMapping("/aaa") public String test() { System.out.println("The test controller!"); return "test"; } }
以下对Controller的编写进行说明:
1) requestMapping:用于指定该Controller的请求路径;有两种用法:
a. 用于类文件头
b. 用于各个方法上
用于方法上时,如果此时未在类上进行相关指定,表明这个方法将会处理指定名称+.do的请求,如aaa.do的请求;如类上指定的RequestMapping,如指定的名称是test,那么这个方法将会处理/test/aaa.do的请求。
因此,用于类上时,将会为该类里面所有处理请求的Controller方法添加一个共同的前缀——这跟文件夹类似,所有同一类的Controller,都可以包含一个相同的目录与一个不同的名称,这样这一类的Controller就可以与其它类的Controller区别开来。
2) 在方法中指定参数
在方法中可以指定多个参数,如HttpServletRequest、HttpServletResponse、HttpSession等。可以指定其中一个或者多个或者0个。
@RequestMapping("/bbb") public String bbb(HttpServletRequest request, HttpServletResponse response, PrintWriter out, HttpSession session) { System.out.println("The test of bbb!"); return "bbb"; }
3) 建立Ajax的Controller
那么怎么处理Ajax请求呢?使用返回类型为void类型的方法即可:
@RequestMapping("/c") public void cajax(PrintWriter out) { }
4) 传递参数
另外,还有一点很重要的,如何向Controller传递参数?主要有两种方式:
a. 比较传统的方式:使用HttpServletRequest来传递参数,也就是说在URL中直接附带参数:
@RequestMapping("/bbb") public String bbb(HttpServletRequest request, HttpServletResponse response, PrintWriter out, HttpSession session) { String temp = request.getParameter("a"); System.out.println(temp); System.out.println("The test of bbb!"); return "bbb"; }
访问时,可以使用以下地址进行访问:
http://localhost:8080/webtest/test/bbb.do?a=test
b. RequestParam
@RequestMapping("/aaa") public String test(@RequestParam String test) { System.out.println("The test controller!"); System.out.println(test); return "test"; }
访问地址:
http://localhost:8080/webtest/test/aaa.do?test=test
c. PathVariable
@RequestMapping("/{id}/{p1}/{p2}/c") public void cajax(PrintWriter out, @PathVariable String id, @PathVariable String p1, @PathVariable String p2) { System.out.println(id); System.out.println(p1); System.out.println(p2); }
访问地址:
http://localhost:8080/webtest/test/test/test1/test2/c.do
d. 直接使用参数:该方式是最为简单的一种方式,但缺点就是不能获取get方式传递的参数。
@RequestMapping("/d") public void test(int id, String name) { System.out.println(id); }
完整示例见附件,运行时需要自己添加上Spring所使用的那些包。