二、Spring MVC的核心类和常用注解
2.1 Spring MVC的核心类—DispatcherServlet
在前面的Spring MVC的工作流程中我们知道前端控制器(DispatcherServlet)在整个开发过成中发挥了不可或缺的作用。
DispatcherServlet的全名是org.springframework.web.servlet.DispatcherServlet,它在程序中充当这前端控制器的角色,在使用时只需将其配置在项目的web.xml文件中即可:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>Archetype Created Web Application</display-name>
<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-springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
2.2 @Controller注解类型
org.springframework.stereotype.Controller注解类型用于指示Spring类的实例是一个控制器,其注解形式为@Controller。该注解在使用时不需要再实现Controller接口,只需要将@Controller注解加入到控制器类上,然后通过Sprring的扫描机制找到标注了该注解的控制器即可。
在配置文件中引入注解和注解要扫描的包:
<?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:mvc="http://www.springframework.org/schema/mvc"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="cn.controller"/>
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="suffix" value=".jsp"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
</bean>
控制器类HelloController.java
package cn.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloController {
@RequestMapping("/showHello")
public String showHello(){
return "Hello";
}
}
2.3 @RequestMapping注解类型
-
@RequestMapping注解的使用 Spring通过@Controller注解找到相应的控制器类后,还需要知道控制器内部对每一个请求时如何处理的,这就需要使用@RequestMapping类型的注解。 RequestMapping注解类型用于一个请求或一个方法,其注解形式为@RequestMapping,可以使用该注解标注在一个方法或一个类上。 1. 标注在方法上 当标注在一个方法上时,该方法将成为一个请求处理方法,它会在程序接收到对应的URL请求时被调用。 package cn.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloController {
@RequestMapping("/showHello")
public String showHello(){
return "Hello";
}
}
2. 标注在类上 当标注在一个类上时,该类的所有方法都将映射为相对于类级别的请求,表示该控制器所处理的所有请求都被映射到value属性值所指定的路径下。 package cn.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/showHello2")
public class HelloController {
@RequestMapping("/showHello")
public String showHello(){
return "Hello";
}
}
-
@RequestMapping注解的属性 这些属性都是可选的,但其默认属性是value,当value是其唯一属性时,可以省略属性名。 -
组合注解
-
请求处理方法的参数类型和返回类型 话不多说,直接上代码: package cn.controller;
import cn.pojo.SystemUser;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Controller
public class SystemUserController {
@RequestMapping(method = RequestMethod.GET,value = {"/toLogin"})
public String toLogin(){
System.out.println(1/0);
return "login";
}
@RequestMapping(method = RequestMethod.POST,value = {"/doLogin1"})
public String doLogin1(String loginId, String loginPwd, HttpServletRequest request, HttpServletResponse response){
System.out.println("用户名:"+loginId);
System.out.println("密码:"+loginPwd);
return "index";
}
@RequestMapping(method = RequestMethod.POST,value = {"/doLogin"})
public String doLogin(@RequestParam(value = "loginId",required = true)String loginId,
@RequestParam(value = "loginPwd",required = true)String loginPwd){
System.out.println("用户名:"+loginId);
System.out.println("密码:"+loginPwd);
return "index";
}
@RequestMapping(method = RequestMethod.POST,value = {"/doLogin2"})
public String doLogin2(SystemUser systemUser){
System.out.println(systemUser);
return "index";
}
@RequestMapping(method = RequestMethod.POST,value = {"/doLogin3"})
public ModelAndView doLogin3(SystemUser systemUser){
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("myIndex");
modelAndView.addObject("USERS",systemUser.getLoginId());
return modelAndView;
}
@RequestMapping(method = RequestMethod.GET,value = {"/myIndex"})
public String myIndex(){
return "index";
}
除了上述的返回值类型之外,还可以进行重定向和请求转发:
-
redirect重定向
@RequestMapping(method = RequestMethod.GET,value = {"/doLogin4"})
public String doLogin4(String loginId,String loginPwd){
System.out.println("用户名:"+loginId);
System.out.println("密码:"+loginPwd);
return "redirect:/myIndex";
}
-
forward请求转发
@RequestMapping(method = RequestMethod.GET,value = {"/doLogin5"})
public String doLogin5(String loginId,String loginPwd){
System.out.println("用户名:"+loginId);
System.out.println("密码:"+loginPwd);
return "forward:/myindex";
}
|